如何在从属模块中激活Maven配置文件? [英] How to activate a Maven profile in a dependent module?
问题描述
假设我有一个模块A:jar
,其运行时和依赖关系的编译集取决于JDK版本.在我的示例中,对于 JAXB API,我有一个pre-jdk6-profile
:在JDK 1.6.0之前需要包括jaxb-api-nnn.jar
作为编译依赖项.此配置文件已放置到A.pom
.
我还有模块B:war
,该模块取决于A:jar
.我希望能够在构建服务器上激活此配置文件以构建JDK 1.5.x可交付成果.在激活给定配置文件的情况下执行Maven时,出现以下消息:
mvn -Ppre-jdk6-profile -o install
[WARNING]
Profile with id: 'pre-jdk6-profile' has not been activated.
在结果B.war
中缺少
和jaxb-api-nnn.jar
.但是,如果从父级pom.xml
进行构建时激活了此配置文件,则一切正常.这意味着配置文件不是从依赖项继承的,并且父多模块pom.xml能够正确构建所有内容,因为似乎所有配置文件都已合并在反应堆中.
将配置文件转移到父pom会使情况变得更糟,因为依赖项已应用于所有其他项目(例如,到C:ear
).对于此任务是否有很好的解决方案,即如果任何模块A
依赖于模块B
,那么由配置文件激活的所有编译和运行时依赖项都将得到正确处理?
项目A:jar
中的配置文件如下:
<project ...>
<artifactId>A</artifactId>
<packaging>jar</packaging>
...
<parent>
<artifactId>P</artifactId>
...
</parent>
<profiles>
<profile>
<id>pre-jdk6-profile</id>
<activation>
<jdk>(,1.6.0)</jdk>
</activation>
<dependencies>
<dependency>
<groupId>javax.xml.ws</groupId>
<artifactId>jaxws-api</artifactId>
</dependency>
</dependencies>
</profile>
</profiles>
...
</project>
a)在多模块构建中,您应始终从顶部pom进行构建,而不应从单个模块进行构建.如果您只想构建一个模块,请使用高级反应堆选项(请参阅mvn --help),如下所示:
mvn -pl mymodule
b)在父pom中定义并激活配置文件,但在子pom中添加配置.
父pom.xml
<profiles>
<profile>
<id>pre-jdk-6</id>
<activation>
<jdk>(,1.6.0)</jdk>
</activation>
</profile>
</profiles>
子pom.xml
<profiles>
<profile>
<id>pre-jdk-6</id>
<dependencies>
<dependency>
<groupId>javax.xml.ws</groupId>
<artifactId>jaxws-api</artifactId>
</dependency>
</dependencies>
</profile>
</profiles>
Suppose I have a module A:jar
, whose runtime and compilation set of dependencies depends on the JDK version. In my example, I have a pre-jdk6-profile
for JAXB API: prior to JDK 1.6.0 I need to include jaxb-api-nnn.jar
as a compile dependency. This profile is placed to A.pom
.
I also have module B:war
, which depends on A:jar
. I want to be able to activate this profile on a build server to build the JDK 1.5.x deliverable. When I execute Maven with a given profile activated, I get the message:
mvn -Ppre-jdk6-profile -o install
[WARNING]
Profile with id: 'pre-jdk6-profile' has not been activated.
and jaxb-api-nnn.jar
is missing in resulting B.war
. However if I activate this profile when building from the parent pom.xml
, everything is OK. That means the profiles are not inherited from dependencies, and the parent multi-module pom.xml was able to build everything correctly because it seems like all profiles are merged in reactor.
Shifting the profile to parent pom makes things worse, as the dependencies are applied to all other projects (e.g. to C:ear
). Are there nice solutions for this task, namely, if any module A
depends on module B
, then all compile and runtime dependencies which are activated by a profile, are correctly handled?
The profile in project A:jar
follows:
<project ...>
<artifactId>A</artifactId>
<packaging>jar</packaging>
...
<parent>
<artifactId>P</artifactId>
...
</parent>
<profiles>
<profile>
<id>pre-jdk6-profile</id>
<activation>
<jdk>(,1.6.0)</jdk>
</activation>
<dependencies>
<dependency>
<groupId>javax.xml.ws</groupId>
<artifactId>jaxws-api</artifactId>
</dependency>
</dependencies>
</profile>
</profiles>
...
</project>
a) In a multi-module build, you should always build from the top pom, never from an individual module. If you want to build only one module, use advanced reactor options (see mvn --help) like this:
mvn -pl mymodule
b) Define and activate the profile in the parent pom, but add the configuration in the child pom.
parent pom.xml
<profiles>
<profile>
<id>pre-jdk-6</id>
<activation>
<jdk>(,1.6.0)</jdk>
</activation>
</profile>
</profiles>
child pom.xml
<profiles>
<profile>
<id>pre-jdk-6</id>
<dependencies>
<dependency>
<groupId>javax.xml.ws</groupId>
<artifactId>jaxws-api</artifactId>
</dependency>
</dependencies>
</profile>
</profiles>
这篇关于如何在从属模块中激活Maven配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!