Piotrek De Piotrek De - 1 year ago 101
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
(the same within which I'm using Reflections) I get the following result:


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++) {

Answer Source

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

    /** @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 =
                d.getArtifactId(), d.getVersion(), d.getType(),

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

        File artifactFile = artifact.getFile();

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download