1

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();
        }
    }
}
jerry.yang
  • 11
  • 1
  • What happens when you put your entire project on the classpath of the plugin by adding the following dependency to the `plugin.dependencies` list in `pom.xml`? Then you should be able to load all classes: `${project.groupId}${project.artifactId}${project.version}` – slindenau Sep 16 '22 at 08:13
  • well , the maven plugin maybe run everywhere, it cann't dependent any customize project. – jerry.yang Sep 16 '22 at 08:36
  • True, but you can add to the instructions of the plugin (in the documentation) that the end user must always add that dependency? Not very pretty, but maybe someone else knows a better solution. – slindenau Sep 16 '22 at 08:41
  • emm,I used the plugin load the .class file in PROJECT_HOME/target/classes/.... directory, it works well , but these .class files don't contain any 2nd or 3rd class. Maybe I should handle these in other way. Thank you for your help . – jerry.yang Sep 16 '22 at 10:18
  • Maybe this can help to navigate the current project and its dependencies: https://medium.com/@viqueen/building-a-maven-plugin-to-explore-your-code-base-fe309ce56eb6 – slindenau Sep 17 '22 at 14:41

1 Answers1

0

finally,I resolve the question,this maven plugin can load the classes while running in compile stage or others, thanks.

@Mojo(name = "report", threadSafe = true,defaultPhase= LifecyclePhase.COMPILE,requiresDependencyResolution = ResolutionScope.COMPILE)
@Execute(phase=LifecyclePhase.COMPILE)
public class JavadocMojo extends AbstractMojo {

    @Override
    public void execute() {
            List<String> list = mavenProject.getCompileClasspathElements();
            JarClassLoader jarClassLoader=new JarClassLoader(this.getClass().getClassLoader());
            for(String item: list){
                jarClassLoader.add(new File(item).toURI().toURL());
            }
}
}
jerry.yang
  • 11
  • 1