I have created a maven plugin, the plugin will parse java source code and submit the results to my console.
But now I have a problem, some java classes are written by a third-party or by other groups at my company, then my plugin can't get these classes' structure, fields, method parameter type, method return type etc.
At last, I modified my plugin to run at the package
phase, while maven packages the source code, then the plugin can load the jar package and load all the classes in the jar.
But now, the classloader doesn't work.
My plugin code can be seen below:
@Mojo(name = "report", threadSafe = true,defaultPhase= LifecyclePhase.PACKAGE)
@Execute(phase =LifecyclePhase.PACKAGE )
public class JavadocMojo extends AbstractMojo {
@Component
private MavenProject mavenProject;
@Override
public void execute() {
try {
File targetDir = new File(mavenProject.getCompileClasspathElements().get(0)).getParentFile();
Optional<File> optional = Arrays.stream(targetDir.listFiles()).filter(t->t.getName().endsWith(".jar")).findFirst();
File file = optional.get();
URLClassLoader urlClassLoader=new URLClassLoader(new URL[]{file.toURI().toURL()},mavenProject.getClass().getClassLoader());
// the full class name is other project's class of mine
// this classload cann't load the target class and throws ClassNotFoundException while I'm debug the plugin
urlClassLoader.loadClass("com.x.y.z.MyClass");
} catch (Exception e) {
e.printStackTrace();
}
}
}