简体   繁体   English

如何使用属性激活Maven子模块中的配置文件?

[英]How to activate a profile in a maven submodule using a property?

I am using Maven 3.6.0 and OpenJDK8 on Ubuntu 18.04 (also tested with Alpine Linux). 我在Ubuntu 18.04上使用了Maven 3.6.0和OpenJDK8(也已在Alpine Linux上进行了测试)。

I have a pom.xml in the root of my project that includes my submodules : 我在项目的根目录中有一个pom.xml ,其中包含我的子模块:

...
  <modules>
    <module>mysubmodule</module>
  </modules>
...

In the mysubmodule folder, the pom.xml has a profile that I want to activate based on a property passed to the mvn executable: mysubmodule文件夹中, pom.xml具有要基于传递给mvn可执行文件的属性来激活的配置文件:

...
    <profile>
      <id>my-profile</id>
      <activation>
        <property>
          <name>activateMyProfile</name>
        </property>
      </activation>
      ...
    </profile>
...

I then execute mvn to start the build, but the profile is never activated: 然后,我执行mvn来开始构建,但是该配置文件从未被激活:

  • If I run mvn -DactivateMyProfile release:prepare from the root of my project, the profile is never activated and never runs 如果我从项目的根目录运行mvn -DactivateMyProfile release:prepare ,则永远不会激活配置文件,也不会运行
  • If I run mvn release:prepare from the root of my project, the profile is never run. 如果我从项目的根目录运行mvn release:prepare ,则永远不会运行配置文件。

I also tried the inverse: 我也尝试过相反的方法:

...
    <profile>
      <id>my-profile</id>
      <activation>
        <property>
          <name>!doNotActivateMyProfile</name>
        </property>
      </activation>
      ...
    </profile>
...
  • If I run mvn -DdoNotActivateMyProfile release:prepare from the root of my project, the profile is still executed 如果我从项目的根目录运行mvn -DdoNotActivateMyProfile release:prepare ,则仍然执行配置文件
  • If I run mvn release:prepare from the root of my project, the profile is also executed 如果我从项目的根目录运行mvn release:prepare ,也会执行配置文件

It looks like mvn is not able to see the properties being passed through the command line. 看起来mvn无法看到通过命令行传递的属性。 What is the correct way to activate a profile in a submodule using a property? 使用属性激活子模块中的配置文件的正确方法是什么?

As I am using the maven release plugin, parameters must be passed using the -Darguments argument. 当我使用Maven版本插件时,必须使用-Darguments参数传递参数。

For example, instead of using mvn -DactivateMyProfile release:prepare , the correct invocation is: mvn -Darguments=-DactivateMyProfile release:prepare 例如,代替使用mvn -DactivateMyProfile release:prepare ,正确的调用是: mvn -Darguments=-DactivateMyProfile release:prepare

If there are multiple arguments, use mvn -Darguments="-DactivateMyProfile -DsomeOtherArg -DanotherArg=something" release:prepare 如果有多个参数,请使用mvn -Darguments="-DactivateMyProfile -DsomeOtherArg -DanotherArg=something" release:prepare

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM