ver*_*tti 9 java classloader maven reflections
我想使用Google Reflections从我的Maven插件中扫描已编译项目中的类.但默认情况下,插件不会看到项目的已编译类.从Maven 3文档我读到:
需要从项目的compile/runtime/test类路径加载类的插件需要与mojo注释@requiresDependencyResolution一起创建自定义URLClassLoader.
至少可以说这有点模糊.基本上我需要一个加载已编译项目类的类加载器的引用.我怎么做到的?
编辑:
好的,@Mojo注释有requiresDependencyResolution参数,所以这很容易,但仍然需要正确的方法来构建一个类加载器.
ver*_*tti 10
@Component
private MavenProject project;
@SuppressWarnings("unchecked")
@Override
public void execute() throws MojoExecutionException {
List<String> classpathElements = null;
try {
classpathElements = project.getCompileClasspathElements();
List<URL> projectClasspathList = new ArrayList<URL>();
for (String element : classpathElements) {
try {
projectClasspathList.add(new File(element).toURI().toURL());
} catch (MalformedURLException e) {
throw new MojoExecutionException(element + " is an invalid classpath element", e);
}
}
URLClassLoader loader = new URLClassLoader(projectClasspathList.toArray(new URL[0]));
// ... and now you can pass the above classloader to Reflections
} catch (ClassNotFoundException e) {
throw new MojoExecutionException(e.getMessage());
} catch (DependencyResolutionRequiredException e) {
new MojoExecutionException("Dependency resolution failed", e);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1743 次 |
| 最近记录: |