简体   繁体   中英

How do I obtain classpath of another java project?

I am writing a maven plugin now and need to obtain the classpath of another java project. I would like to know if it is possible to get the classpath of another java project from my current java project? Thanks in advance

if "another java project" means "the project that declears your plugin", here is my answer :

you need to create new classloader from plugin :

List classpathElements = project.getCompileClasspathElements();
classpathElements.add( project.getBuild().getOutputDirectory() );
classpathElements.add( project.getBuild().getTestOutputDirectory() );
URL urls[] = new URL[classpathElements.size()];
for ( int i = 0; i < classpathElements.size(); ++i ) {
  urls[i] = new File( (String) classpathElements.get( i ) ).toURL();
}
return new URLClassLoader( urls, this.getClass().getClassLoader() );

with new classloader, you can do something (loading class, reflection, generating code) with project's classes

hibernate3-maven-plugin use the same trick to generate mapping from project's annotated classes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM