0

I have created a custom Maven plugin like so:

@Mojo(name="generate", defaultPhase = LifecyclePhase.PREPARE_PACKAGE) 
public class HandlerGeneratorMojo extends AbstractMojo {
...
}

And I am using it like this:

            <!-- GENERATE EVENT HANDLERS (POC) -->
        <plugin>
            <artifactId>handler-generator-maven-plugin</artifactId>
            <groupId>my.group.id</groupId>
            <version>${revision}</version>
            <executions>
                <execution>
                    <id>generate handlers</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                    ...
                    </configuration>
                </execution>
            </executions>
        </plugin>

In the execute method of the Mojo I am trying to retrieve all interfaces that extend interface CdsData, using the Reflections api.

        final Reflections reflections = new Reflections();
    
     final Set<Class<? extends CdsData>> allClasses = 
         reflections.getSubTypesOf(CdsData.class);

     log.info("Number of results: " + allClasses.size());
     
     for (final Class<? extends CdsData> cdsDataClass: allClasses) {

         log.info("Result: " + cdsDataClass);
     }

I get five results, that are in a dependency of the pom where I execute the plugin. So that is ok. However I have also generated some implementations using another plugin (cds4j-maven-plugin) that has set

<phase>generate-sources</phase>

And I have tried to set the phase of my custom plugin to prepare-package so it is executed way after the above plugin has generated the sources. However I get no more results than the five I mentioned before. I would expect to see those generated interfaces as well.

Might this be a class path, or class loader problem? Or the phases maybe?

I have also tried to put the package of the generated classes in the Reflections constructor, and I have tried to configure Reflections class loading as described here at line 8: https://www.hellojava.com/a/80798.html

Any help is appreciated! Thank you!

Cheers, Kjeld

Kjeld
  • 444
  • 4
  • 14

1 Answers1

1

Ok solved it, finally. It was definitely a class loading problem. I needed to add a classloader that points to classpath elements, as explained here: http://blog.chalda.cz/2018/02/17/Maven-plugin-and-fight-with-classloading.html

I changed the scope to process-classes, basically changed everything as descibed in the blog, and used:

        Reflections reflections = new Reflections(classLoader, sourcePackage);

Where classloader is the classloader that is put together as explained in that blog, and sourcePackage is a string containing the package prefix of the resulting types I want.

Hope this helps anyone else.

Thanks.

Kjeld
  • 444
  • 4
  • 14