【问题标题】:Maven: How do I activate a profile from command line?Maven:如何从命令行激活配置文件?
【发布时间】:2015-09-04 18:47:14
【问题描述】:

这是来自我的 pom.xml 的 sn-p。 我尝试了以下操作,但配置文件未激活。

mvn clean install -Pdev1
mvn clean install -P dev1

当我尝试mvn help:active-profiles 时,没有配置文件被列为活动。 如果我将dev1<activeByDefault> 设置为true,并运行mvn help:active-profiles,它显示配置文件已激活。

<profile>
        <id>dev1</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <systemPropertyVariables>
                            <env>local</env>
                            <properties.file>src/test/resources/dev1.properties</properties.file>
                        </systemPropertyVariables>
                        <suiteXmlFiles>
                            <suiteXmlFile>src/test/resources/dev1.testng.xml</suiteXmlFile>
                        </suiteXmlFiles>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>dev2</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <systemPropertyVariables>
                            <env>local</env>
                            <properties.file>src/test/resources/dev2.properties</properties.file>
                        </systemPropertyVariables>
                        <suiteXmlFiles>
                            <suiteXmlFile>src/test/resources/dev2.testng.xml</suiteXmlFile>
                        </suiteXmlFiles>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>

我想知道为什么我的个人资料没有被激活。有没有人遇到过类似的问题?

【问题讨论】:

  • 至少使用 maven 3.5.3 -P 确实可以按预期工作...FWIW...

标签: maven pom.xml


【解决方案1】:

两个命令都是正确的:

mvn clean install -Pdev1
mvn clean install -P dev1

问题很可能不是配置文件激活,而是配置文件没有达到您的预期

命令正常:

mvn help:active-profiles

不显示配置文件,因为它不包含-Pdev1。您可以添加它以显示配置文件,但这将毫无意义,因为您将测试 maven 本身。

您应该执行以下操作检查配置文件行为

  1. 在配置文件配置中将activeByDefault 设置为true
  2. 运行mvn help:active-profiles(以确保它有效 即使没有-Pdev1也激活),
  3. 运行mvn install

它应该给出与以前相同的结果,因此确认问题是配置文件没有按照您的预期执行。

【讨论】:

  • 是的,当我将 activeByDefault 设置为 true 时,它​​会显示在 mvn help:active-profiles 下。但是执行 mvn clean install -Pdev1 不会激活配置文件。
【解决方案2】:

通过系统属性激活可以如下进行

<activation>
    <property>
        <name>foo</name>
        <value>bar</value>
    </property>
</activation>

并使用 -D 运行 mvn 构建以设置系统属性

mvn clean install -Dfoo=bar

此方法还有助于在项目工件的传递依赖中选择配置文件。

【讨论】:

【解决方案3】:

我遇到了这个问题,我通过在运行 mvn cli 命令时添加 -DprofileIdEnabled=true 参数解决了上述问题。

请将您的 mvn cli 命令运行为:mvn clean install -Pdev1 -DprofileIdEnabled=true

除此解决方案外,您无需删除 POM 中的 activeByDefault 设置,如之前的答案所述。

我希望这个答案能解决你的问题。

【讨论】:

  • 有人可能也想跳过测试以获得更快的构建 mvn clean install -Prelease -DprofileIdEnabled=true -DskipTests
【解决方案4】:

只是删除激活部分,我不知道为什么 -Pdev1 不会覆盖默认的错误激活。但如果你省略这个:

<activation> <activeByDefault>false</activeByDefault> </activation>

只有在明确声明为 -Pdev1 后才会激活您的配置文件

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-03
    • 2011-11-12
    • 2014-10-29
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 2014-10-01
    • 1970-01-01
    相关资源
    最近更新 更多