简体   繁体   English

如何获取另一个Java项目的类路径?

[英]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. 我现在正在编写一个maven插件,需要获取另一个Java项目的类路径。 I would like to know if it is possible to get the classpath of another java project from my current java project? 我想知道是否可以从当前的Java项目中获取另一个Java项目的类路径? Thanks in advance 提前致谢

if "another java project" means "the project that declears your plugin", here is my answer : 如果“另一个Java项目”的意思是“清除您插件的项目”,这是我的答案:

you need to create new classloader from plugin : 您需要从plugin创建新的classloader:

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 hibernate3-maven-plugin使用相同的技巧从项目的带注释的类生成映射

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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