Piotrek De Piotrek De - 4 months ago 26
Java Question

scanning java classpath in the maven plugin

What I'm trying to do is to write some Maven plugin which scans application classes looking for implementation of a particular interface (it might be classes with some annotation as well) and basis on the result generating some code. I've successfully implemented plugin running in the generate-sources phase and writing source code to the generated-sources directory.

The problem is with scanning classpath for the particular interface implementations/classes with some annotation.
I am using the Reflections library to scan classes in the following way:

private Set<Class< ? extends MyInterface >> scan(final String packageName) {
final Reflections reflections = new Reflections(packageName);
return reflections.getSubTypesOf(MyInterface.class);
}


Unfortunately, this method returns empty set. When I print my classpath in the class extending
org.apache.maven.plugin.AbstractMojo
(the same within which I'm using Reflections) I get the following result:


/home/pd5108/apache-maven-2.2.1/boot/classworlds-1.1.jar


The classes I want to find using Reflections exists in dependend JARs as well as in the module within which plugin is configured. Looking at the classpath printed out it seems that at this point (generate-sources phase) dependencies defined in maven all not available on classpath yet - probably they are added in the next phases. Is that true? Is there any other approach I can use?

Here is the way how classpath is printed out:

URL[] urls = ((URLClassLoader)sysClassLoader).getURLs();

for(int i=0; i< urls.length; i++) {
System.out.println(urls[i].getFile());
}

Answer

Required MOJO class fields:

    /**
    * The project currently being built.
    *
    * @parameter expression="${project}"
    * @readonly
    * @required
    */
    private MavenProject project;

    /** @parameter expression="${localRepository}" */
    protected ArtifactRepository m_localRepository;

    /**@parameter default-value="${localRepository}" */
    private org.apache.maven.artifact.repository.ArtifactRepository
        localRepository;

    /** @parameter default-value="${project.remoteArtifactRepositories}" */
    private java.util.List remoteRepositories;

    /** @component */
    private org.apache.maven.artifact.factory.ArtifactFactory artifactFactory;

    /** @component */
    private org.apache.maven.artifact.resolver.ArtifactResolver resolver;

Resolution of all dependencies JARs:

 final List<Dependency> dependencies = project.getDependencies();

    for (Dependency d : dependencies) {

        final Artifact artifact =
            artifactFactory.createArtifactWithClassifier(d.getGroupId(),
                d.getArtifactId(), d.getVersion(), d.getType(),
                d.getClassifier());

        try {
            resolver.resolve(artifact, remoteRepositories,
                    localRepository );
        } catch (ArtifactResolutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ArtifactNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        File artifactFile = artifact.getFile();
        System.out.println(artifactFile.getAbsolutePath());
    }

And now we need to scan these JARs using reflection API looking for the appropriate classes. At this point I think that there's no other way, since I work in generate-sources phase and artifact values for the next phases are not computed yet.