Ste*_*ong 15 maven-2 maven-plugin
我正在尝试制作一个需要使用反射的maven插件.我想要一个项目来运行插件,并在项目中为它提供类的全名,插件将通过反射加载它以从中获取信息.
但是类加载器有些奇怪,因为它在我使用时找不到类
Class.forName("package.MyClass");
Run Code Online (Sandbox Code Playgroud)
看看这里,我无法弄清楚我的插件的类加载器,当在不同的项目中运行时,是否可以访问该项目的类.
Ste*_*ong 17
我确信有更好的方法,但这是我如何让它工作:
将以下内容添加到mojo顶部的javadoc:@requiresDependencyResolution运行时
添加MavenProject参数:
/**
* @parameter expression="${project}"
* @required
* @readonly
*/
private MavenProject project;
Run Code Online (Sandbox Code Playgroud)
然后,您可以在运行时获取依赖项,并创建自己的类加载器:
List runtimeClasspathElements = project.getRuntimeClasspathElements();
URL[] runtimeUrls = new URL[runtimeClasspathElements.size()];
for (int i = 0; i < runtimeClasspathElements.size(); i++) {
String element = (String) runtimeClasspathElements.get(i);
runtimeUrls[i] = new File(element).toURI().toURL();
}
URLClassLoader newLoader = new URLClassLoader(runtimeUrls,
Thread.currentThread().getContextClassLoader());
Run Code Online (Sandbox Code Playgroud)
然后你可以使用这个新的类加载器加载你的类:
Class bundle = newLoader.loadClass("package.MyClass");
Run Code Online (Sandbox Code Playgroud)
Chr*_*wes 11
您应该考虑使用它将运行时类路径元素添加到当前类领域.(您可以使用它PluginDescriptor来检索类领域.
List<String> runtimeClasspathElements = project.getRuntimeClasspathElements();
ClassRealm realm = descriptor.getClassRealm();
for (String element : runtimeClasspathElements)
{
File elementFile = new File(element);
realm.addURL(elementFile.toURI().toURL());
}
Run Code Online (Sandbox Code Playgroud)
这对我来说非常合适!
正如戴夫所问,这是获得PluginDescriptor:
/**
* The plugin descriptor
*
* @parameter default-value="${descriptor}"
*/
private PluginDescriptor descriptor;
Run Code Online (Sandbox Code Playgroud)
我今天遇到了这个确切的问题。上述建议对我不起作用,我想我会将我的解决方案提交到列表中。我使用了 HibernateExporter mojo 源代码,可以在以下位置查看:http://grepcode.com/file/repo1.maven.org/maven2/org.codehaus.mojo/hibernate3-maven-plugin/2.2/org/codehaus/mojo/ hibernate3/HibernateExporterMojo.java?av=f
/**
* @parameter expression="${project}"
* @required
* @readonly
*/
private MavenProject project;
private ClassLoader getClassLoader() throws MojoExecutionException
{
try
{
List<String> 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 ) ).toURI().toURL();
}
return new URLClassLoader(urls, getClass().getClassLoader() );
}
catch (Exception e)//gotta catch em all
{
throw new MojoExecutionException("Couldn't create a classloader.", e);
}
}
public void execute() throws MojoExecutionException
{
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(getClassLoader());
//... your code here ...
}
Run Code Online (Sandbox Code Playgroud)
还要确保您使用正确的 MavenProject 类。将其添加到您的 pom 中
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.0.3</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7397 次 |
| 最近记录: |