gpt4 book ai didi

java - Maven插件: inject project classpath

转载 作者:行者123 更新时间:2023-12-01 12:17:28 24 4
gpt4 key购买 nike

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

但是,据我了解,Maven 插件带有自己的类路径,因此导致我的核心模块调用中的所有 Class#getResourceAsStream 返回 null。

有没有办法将项目类路径元素包含在插件中?

最佳答案

好的,问题是由于前导斜杠造成的。删除后,以下代码将检索 MavenProject 实例(您需要将 maven-artifact 和 maven-project 添加到 POM 中):

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());
}

关于java - Maven插件: inject project classpath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26902325/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com