Maven: How do I activate a profile from command li

2019-03-14 06:06发布

This is a snippet from my pom.xml. I tried the following, but the profile was not activated.

mvn clean install -Pdev1
mvn clean install -P dev1

When I tried mvn help:active-profiles no profiles were listed as active. If I set <activeByDefault> for dev1 to true, and run mvn help:active-profiles, it shows me the profile is activated.

<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>

I am wondering why my profile is not getting activated. Has anyone encountered similar issue?

标签: maven pom.xml
4条回答
三岁会撩人
2楼-- · 2019-03-14 06:14

I have encountered this problem and i solved mentioned problem by adding -DprofileIdEnabled=true parameter while running mvn cli command.

Please run your mvn cli command as : mvn clean install -Pdev1 -DprofileIdEnabled=true.

In addition to this solution, you don't need to remove activeByDefault settings in your POM mentioned as previouses answer.

I hope this answer solve your problem.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-03-14 06:21

Activation by system properties can be done as follows

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

And run the mvn build with -D to set system property

mvn clean install -Dfoo=bar

This method also helps select profiles in transitive dependency of project artifacts.

查看更多
祖国的老花朵
4楼-- · 2019-03-14 06:23

You won't see it with

mvn help:active-profiles

as there is no -Pdev1 in the command.

You would see it with mvn help:active-profiles -Pdev1 (but there obviously is no point in that).

The commands you showed :

mvn clean install -Pdev1
mvn clean install -P dev1

are correct. The problem is probably not that the profile is not activated, but rather that it does not do what you expect it to.

To make sure that's the case, you can set activeByDefault to true on the profile, then launch mvn help:active-profiles to see it's effectively activated, then launch mvn install and check if the profile does what you expect (which again is probably not the case).

查看更多
欢心
5楼-- · 2019-03-14 06:31

Just remove activation section, I don't know why -Pdev1 doesn't override default false activation. But if you omit this:

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

then your profile will be activated only after explicit declaration as -Pdev1

查看更多
登录 后发表回答