如何通过pom.xml激活Maven配置文件? [英] How to activate a Maven profile per pom.xml?
问题描述
我有一个大型的Maven项目,其中包含一个主模块和许多子代,它们以漂亮的树形结构组织.但是,某些模块需要特殊设置,这将导致其他模块的异常行为.例如,有些在Hadoop上执行工作的模块需要Java源代码级别为1.7,所有其他Java模块使用源代码级别为1.8,现在Scala模块已引入此树中,并使用scala-maven-plugin
进行编译,需要特殊设置编译混合Java/Scala模块时使用maven-compiler-plugin
.
I have a large Maven project with one master module and lots of children, organized in a nice tree structure. However, some of the modules need special settings which would cause misbehaviour in other modules. For example, there are modules which do stuff on Hadoop which need a Java source level of 1.7, all other Java modules use a source level of 1.8, and now Scala modules are introduced into this tree which compile with scala-maven-plugin
which needs special settings for maven-compiler-plugin
when compiling mixed Java/Scala modules.
过去,我只是拥有一个用于Hadoop的主模块,该模块定义了1.7的源级别,并添加了所有必需的Hadoop依赖项.现在,我要避免为Scala编写的Hadoop东西定义另一个主模块,而在Scala中为非Hadoop东西定义另一个模块.我以为可以使用配置文件,所以我在主POM中定义了两个配置文件:
In the past I simply had a master module for the Hadoop stuff which defined the source level of 1.7 and added all necessary Hadoop dependencies. Now I want to avoid defining another master module for Hadoop stuff written in Scala, and yet another module for Non-Hadoop stuff in Scala. I thought I could use profiles, so I defined two profiles in my master POM:
java
activates themaven-javadoc-plugin
scala
does not activatemaven-javadoc-plugin
but themaven-scala-plugin
and configures it like in the example from its documentation
在主POM中,默认情况下将java
配置文件设置为活动状态,因为Java是默认设置.当项目使用Scala时,我想激活scala
配置文件,然后停用java
,但是将java
配置文件的activeByDefault
设置为false
无效,导致编译错误.
In the master POM, the java
profile is set as active by default, as Java is the default. When a project uses Scala, I want to activate the scala
profile, and deactivate java
, but setting activeByDefault
to false
for the java
profile has no effect, causing compile errors.
那么,有没有一种方法可以启用特定于模块的配置文件,而不必在每个pom中手动设置它?
So, is there a way to enable a profile specific to a module without having to set it manually in each pom?
推荐答案
据我了解,您想根据子pom的配置激活其配置文件.恐怕是不可能的.但是,您可以重组项目树结构以利用pom继承:
pom.xml // general properties for all projects
|-- java-modules
| -- pom.xml // inherits from master pom, configures maven-compile-plugin
| -- java-8-modules
| -- pom.xml // inherits from java-modules pom, uses java 8 compiler
| -- java-8-project-a
| -- java-7-modules
| -- pom.xml // inherits from java-modules pom, uses java 7 compiler
| -- java-7-project-a
|-- scala-modules
-- pom.xml // inherits from master pom, configures maven-scala-plugin
-- scala-project-a
您为顶层目录中的所有模块编写一个主pom,子pom仅指定java/scala详细信息.
As far as I understood you want to activate profile from child pom based on its configuration. I'm afraid it is not possible. However you can reorganize project tree structure to take advantage of pom inheritance:
pom.xml // general properties for all projects
|-- java-modules
| -- pom.xml // inherits from master pom, configures maven-compile-plugin
| -- java-8-modules
| -- pom.xml // inherits from java-modules pom, uses java 8 compiler
| -- java-8-project-a
| -- java-7-modules
| -- pom.xml // inherits from java-modules pom, uses java 7 compiler
| -- java-7-project-a
|-- scala-modules
-- pom.xml // inherits from master pom, configures maven-scala-plugin
-- scala-project-a
You write one master pom for all modules in top directory, child poms just specify java/scala details.
有关pom继承的更多信息 https://maven. apache.org/guides/introduction/introduction-to-the-pom.html#Project_Inheritance
More about pom inheritance https://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Project_Inheritance
这篇关于如何通过pom.xml激活Maven配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!