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

ind*_*ent 54 pom.xml maven

这是我的pom.xml的一个片段.我尝试了以下操作,但配置文件未激活.

mvn clean install -Pdev1
mvn clean install -P dev1
Run Code Online (Sandbox Code Playgroud)

当我尝试mvn help:active-profiles没有配置文件被列为活动时.如果我设置<activeByDefault>dev1true,并运行 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>
Run Code Online (Sandbox Code Playgroud)

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

kga*_*ron 63

你不会看到它

mvn clean install -Pdev1
mvn clean install -P dev1
Run Code Online (Sandbox Code Playgroud)

因为-Pdev1命令中没有.

你会看到它activeByDefault(但显然没有意义).

您显示的命令:

mvn help:active-profiles
Run Code Online (Sandbox Code Playgroud)

是正确的.问题可能不是配置文件未激活,而是它没有按照您的预期执行.

为确保这种情况,您可以在配置文件中设置truetrue,然后启动mvn help:active-profiles以查看它是否已被有效激活,然后启动-Pdev1并检查配置文件是否符合您的预期(这可能不是这种情况).

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

小智 13

系统属性的激活可以如下完成

<activation>
    <property>
        <name>foo</name>
        <value>bar</value>
    </property>
</activation>
Run Code Online (Sandbox Code Playgroud)

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

mvn clean install -Dfoo=bar
Run Code Online (Sandbox Code Playgroud)

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


ser*_*bay 7

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

请运行MVN CLI命令:mvn clean install -Pdev1 -DprofileIdEnabled=true

除了此解决方案之外,您无需删除前面提到的POM中的activeByDefault设置。

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


小智 5

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

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

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