How do I obtain classpath of another java project?
I am writing a maven plugin now and need to obtain th开发者_StackOverflow社区e 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
精彩评论