Maven:如何从命令行激活一个配置文件?

119

这是我的pom.xml文件的一部分。 我尝试了以下内容,但是配置文件未被激活。

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>

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


1
至少在maven 3.5.3中,-P能按预期工作...顺便说一句... - rogerdpack
4个回答

132

这两个命令都是正确的:

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

它应该产生与之前相同的结果,从而确认问题是配置文件未执行您期望的操作。


2
是的,当我将activeByDefault设置为true时,它会在mvn help:active-profiles下显示。但执行mvn clean install -Pdev1并不会激活该配置文件。 - indolent

16

可以通过系统属性进行激活,操作步骤如下:

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

使用-D选项来设置系统属性运行mvn build

mvn clean install -Dfoo=bar

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


在我的情况下,*.properties 文件中的属性未替换占位符,请您查看 https://stackoverflow.com/questions/51186963/value-from-maven-profile-properties-is-not-getting-used-in-properties-file - vikramvi

13

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

请按如下方式运行mvn cli命令:mvn clean install -Pdev1 -DprofileIdEnabled=true

除了这个解决方法外,您无需删除作为以前答案中提到的activeByDefault设置的POM中的设置。

希望这个答案能够解决您的问题。


有些人可能也想跳过测试以加快构建速度 mvn clean install -Prelease -DprofileIdEnabled=true -DskipTests - Yeasin Ar Rahman

8

只需删除激活部分,我不知道为什么-Pdev1不能覆盖默认的false激活。但如果你省略了这一部分:

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

那么你的配置文件只有在明确声明为-Pdev1后才会被激活。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接