Java Maven插件:注入项目类路径

Java Maven插件:注入项目类路径,java,maven,plugins,classpath,Java,Maven,Plugins,Classpath,我正在编写一个Maven插件,它的工作是委托给一个需要从项目类路径读取文件的核心模块(即,将插件声明为插件依赖项的项目) 然而,据我所知,Maven插件有自己的类路径,因此导致核心模块调用中的所有Class#getResourceAsStream返回null 有没有办法在pluginone中包含project类路径元素?好的,这个问题是由一个前导斜杠引起的。删除后,以下代码检索MavenProject实例(您需要将maven工件和maven项目添加到POM中): publicstaticclas

我正在编写一个Maven插件,它的工作是委托给一个需要从项目类路径读取文件的核心模块(即,将插件声明为插件依赖项的项目)

然而,据我所知,Maven插件有自己的类路径,因此导致核心模块调用中的所有
Class#getResourceAsStream
返回null


有没有办法在pluginone中包含project类路径元素?

好的,这个问题是由一个前导斜杠引起的。删除后,以下代码检索MavenProject实例(您需要将maven工件和maven项目添加到POM中):

publicstaticclassloader getClassLoader(MavenProject项目)抛出dependencResolutionRequiredException、MalformedUrlexException{
List classPathElements=compileClassPathElements(项目);
List classpathElementUrls=newArrayList(classPathElements.size());
for(字符串classPathElement:classPathElements){
添加(新文件(classPathElement.toURI().toURL());
}
返回新的URLClassLoader(
classpathElementUrls.toArray(新URL[classpathElementUrls.size()]),
Thread.currentThread().getContextClassLoader()
);
}
私有静态列表compileClassPathElements(MavenProject项目)引发DependencyResolutionRequiredException{
返回newArrayList(project.getCompileClasspathElements());
}
public static ClassLoader getClassLoader(MavenProject project) throws DependencyResolutionRequiredException, MalformedURLException {
    List<String> classPathElements = compileClassPathElements(project);
    List<URL> classpathElementUrls = new ArrayList<>(classPathElements.size());
    for (String classPathElement : classPathElements) {
        classpathElementUrls.add(new File(classPathElement).toURI().toURL());
    }
    return new URLClassLoader(
        classpathElementUrls.toArray(new URL[classpathElementUrls.size()]),
        Thread.currentThread().getContextClassLoader()
    );
}

private static List<String> compileClassPathElements(MavenProject project) throws DependencyResolutionRequiredException {
    return newArrayList(project.getCompileClasspathElements());
}