Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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());
}
like image 999
omnomnom Avatar asked Jan 26 '12 16:01

omnomnom


People also ask

How do I find my classpath in Maven?

If you would like to know the classpath required for running the project's application, the includeScope option has to be set as System property like follows: $ mvn dependency:build-classpath -DincludeScope=compile [INFO] Scanning for projects...

Does Maven add dependencies to classpath?

Maven does set the classpath to the dependencies correctly, but not prefixed with repository location. It will look like this in your Manifest file. It is upto you to place the dependant jars in the same folder as the jar which you are running.


2 Answers

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.

like image 80
omnomnom Avatar answered Oct 11 '22 08:10

omnomnom


There are artifact dependencies defined in <dependencies> section and plugin dependencies defined under <plugin><dependencies>.

Plugin dependencies are added to the classpath while I am not sure about the artifact dependencies. Did you try to add your plugin dependencies under the <plugin><dependencies>?

like image 35
Sasha O Avatar answered Oct 11 '22 08:10

Sasha O