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

这是我的 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>

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

225776 次浏览

Both commands are correct :

mvn clean install -Pdev1
mvn clean install -P dev1

The problem is most likely not profile activation, but rather the profile not accomplishing what you expect it to.

It is normal that the command :

mvn help:active-profiles

does not display the profile, because is does not contain -Pdev1. You could add it to make the profile appear, but it would be pointless because you would be testing maven itself.

What you should do is check the profile behavior by doing the following :

  1. set activeByDefault to true in the profile configuration,
  2. run mvn help:active-profiles (to make sure it is now always activated, even without -Pdev1),
  3. run mvn install.

It should produce the same result as before, and therefore confirm that the problem is the profile not doing what you expect.

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.

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

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.