gpt4 book ai didi

java - 用于从托管项目加载类的 Maven mojo 插件

转载 作者:太空宇宙 更新时间:2023-11-04 11:35:53 26 4
gpt4 key购买 nike

我有一个加载类的自定义插件,例如

Class<?> clazz = Class.forName(NAME_OF_CLASS_FROM_HOST_DEPENDENCIES);

NAME_OF_CLASS_FROM_HOST_DEPENDENCIES - 是使用此插件的项目依赖项中存在的类。

在托管项目pom中,我这样调用插件:

<plugin>
<groupId>com.plugins</groupId>
<artifactId>the_plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<executions>
<execution>
<id>do</id>
<phase>process-classes</phase>
<goals>
<goal>do</goal>
</goals>
</execution>
</executions>
</plugin>

获取 ClassNotFoundException

重要的是,pom 中定义的依赖项为

<scope>provided</scope>

最佳答案

最终得到以下结果。

List<URL> listUrl = new ArrayList<URL>();

Set<Artifact> deps = project.getDependencyArtifacts();
for (Artifact artifact : deps) {
final URL url = artifact.getFile().toURI().toURL();
listUrl.add(url);
}

newClassLoader = new URLClassLoader(listUrl.toArray(new URL[listUrl.size()]), Thread.currentThread().getContextClassLoader());

关于java - 用于从托管项目加载类的 Maven mojo 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43291857/

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