首页 > 解决方案 > Maven 多模块在将一个模块用于另一个模块之前对其进行遮蔽

问题描述

我有一个多模块 Maven 构建,其中一个模块遮蔽了一个库,另一个模块使用共享库。

如果我运行mvn test,这将导致package com.example.shaded.javax.el7 does not exist.

如果我运行mvn package test,则编译和测试通过。

所以我想要的是阴影模块不仅可以编译,而且在编译maven-shade-plugin:3.2.1:shade依赖模块之前被阴影(打包)运行。

是否可以在 pom.xml 中的 maven 中引入这样的目标依赖项?

将阴影插件绑定到包以外的其他阶段会产生以下错误消息:

[ERROR] The project main artifact does not exist. This could have the following
[ERROR] reasons:
[ERROR] - You have invoked the goal directly from the command line. This is not
[ERROR]   supported. Please add the goal to the default lifecycle via an
[ERROR]   <execution> element in your POM and use "mvn package" to have it run.
[ERROR] - You have bound the goal to a lifecycle phase before "package". Please
[ERROR]   remove this binding from your POM such that the goal will be run in
[ERROR]   the proper phase.

标签: maven-3maven-shade-pluginmulti-project

解决方案


更新似乎没有一种干净的方法可以做到这一点。

在两者之间插入一个新的 Maven 模块来进行着色。附加到此模块shade中的阶段test。不要忘记依赖新模块。

代码失败,mvn test因为这不会创建任何 JAR,因此不会调用 Shade 插件。

您可能想更改运行 Shade 的阶段generate-test-resources或现有模块中的阶段,但如果在Maven 生命周期test的后期阶段发生某些事情,这可能会导致其他问题。这就是为什么我建议创建一个没有其他副作用的新模块。


推荐阅读