'How to read project classes from Maven plugin?
I am writing a Maven plugin that needs to access the classes of a project in which it is being executed. This plugin uses Javassist library, and it has to be aware of the classpath to grab the classes (to make a CtClass instance, for example), so I have to insert a classpath from the plugin dynamically, using the insertClassPath() method. The plugin is able to read classes inside its own project, but on any other project. I am getting a ClassNotFoundExpection like so:
java.lang.ClassNotFoundException: temp.RestAssuredHW01.RJsonTest at java.lang.Class.forNameImpl(Native Method) at java.lang.Class.forName(Class.java:333) at ExamplePlugin.exposeFindAllClasses(ExamplePlugin.java:311) at ExamplePlugin.examineProject(ExamplePlugin.java:335) at ExamplePlugin.execute(ExamplePlugin.java:100) at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:210) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:156) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:148) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81) at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128) at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:305) at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192) at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105) at org.apache.maven.cli.MavenCli.execute(MavenCli.java:972) at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:293) at org.apache.maven.cli.MavenCli.main(MavenCli.java:196) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:282) at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:225) at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:406) at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:347)
temp.RestAssuredHW01.RJsonTest is the fully qualified name of the first class my plugin has to access.
The part that is meant to access a project class:
Class<?> dynamicClass = Class.forName(testClassIdentifiers.get(0));
cp.insertClassPath(new ClassClassPath(dynamicClass));
dynamicClassis supposed to be the first class the plugin encounters, you can see it being logged astemp.RestAssuredHW01.RJsonTestin the stacktrace, as planned.cpis an instance of Javassist's ClassPool.- I am doing this to make Javassist aware of its surrounding, so to speak.
As you can see, I do not know what the className will be, I am getting them dynamically from the outside, using Files.walk and some simple string formatting. testClassIdentifiers contains a list of valid fully qualified class names. Keep in mind, this code works when executed within the plugin's project itself, but not on any other project.
I believe this has something to do with the classloader. The real question is: how do I read/load/access the classes of a project in which my Maven plugin is being executed?
pom.xml of the project in which I want the plugin executed:
<build>
<plugins>
<plugin>
<groupId>com.example</groupId>
<artifactId>example-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<executions>
<execution>
<goals>
<goal>executeAlgorithm</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
</plugins>
</build>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
