Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven: How do I activate a profile from command line?

Tags:

maven

pom.xml

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?

like image 342
indolent Avatar asked Jun 18 '15 22:06

indolent


People also ask

How use mvn command line?

To build a Maven project via the command line, you use the mvn command from the command line. The command must be executed in the directory which contains the relevant pom file. You pass the build life cycle, phase or goal as parameter to this command.


2 Answers

Both commands are correct :

mvn clean install -Pdev1 mvn clean install -P dev1 

The problem is most likely not profile activation, but 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 effectively activated even without -Pdev1),
  3. run mvn install.

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

like image 156
kgautron Avatar answered Sep 17 '22 03:09

kgautron


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.

like image 39
koios Avatar answered Sep 18 '22 03:09

koios