Maven 插件 MOJO 在解决依赖关系时阻止构建
Maven plugin MOJO brakes the build when resolving dependency
重现示例:https://github.com/g4s8/example-plugin
我正在尝试创建与编译测试一起使用的 Maven 插件 类。在访问 test-classes
文件之前,它必须解析类路径。我在 surefire-maven-plugin
来源中观察了类路径解析机制,但我的实现更简单(参见 https://github.com/g4s8/example-plugin/blob/master/example-maven-plugin/src/main/java/wtf/g4s8/RunWithClasspathMojo.java#L43-L49):
// resolve all dependencies for current project
final Set<Artifact> artifacts = this.repos.resolve(
new ArtifactResolutionRequest()
.setArtifact(this.project.getArtifact())
.setLocalRepository(this.lrep)
.setRemoteRepositories(this.project.getRemoteArtifactRepositories())
.setResolveTransitively(true)
).getArtifacts();
这个插件工作正常,但在第一个 运行 之后它破坏了项目的所有下一个构建(参见 example-project):maven-jar-plugin
失败并出现错误:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-jar-plugin:3.0.2:jar (default-jar) on project example-project: You have to use a classifier to attach supplemental artifacts to the project instead of replacing them. -> [Help 1]
如果我使用 -DskipTests
禁用我的插件或者只是删除
解决依赖的代码,目标项目将构建成功。
我想 ArtifactRepository.resolve
会以某种方式影响当前版本。
此代码有什么问题以及如何从插件正确解析 Maven 项目的依赖类路径?
在 Mojo 的顶行(您还定义相位等),您需要将 @requiresDependencyResolution
设置为 test
。
重现示例:https://github.com/g4s8/example-plugin
我正在尝试创建与编译测试一起使用的 Maven 插件 类。在访问 test-classes
文件之前,它必须解析类路径。我在 surefire-maven-plugin
来源中观察了类路径解析机制,但我的实现更简单(参见 https://github.com/g4s8/example-plugin/blob/master/example-maven-plugin/src/main/java/wtf/g4s8/RunWithClasspathMojo.java#L43-L49):
// resolve all dependencies for current project
final Set<Artifact> artifacts = this.repos.resolve(
new ArtifactResolutionRequest()
.setArtifact(this.project.getArtifact())
.setLocalRepository(this.lrep)
.setRemoteRepositories(this.project.getRemoteArtifactRepositories())
.setResolveTransitively(true)
).getArtifacts();
这个插件工作正常,但在第一个 运行 之后它破坏了项目的所有下一个构建(参见 example-project):maven-jar-plugin
失败并出现错误:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-jar-plugin:3.0.2:jar (default-jar) on project example-project: You have to use a classifier to attach supplemental artifacts to the project instead of replacing them. -> [Help 1]
如果我使用 -DskipTests
禁用我的插件或者只是删除
解决依赖的代码,目标项目将构建成功。
我想 ArtifactRepository.resolve
会以某种方式影响当前版本。
此代码有什么问题以及如何从插件正确解析 Maven 项目的依赖类路径?
在 Mojo 的顶行(您还定义相位等),您需要将 @requiresDependencyResolution
设置为 test
。