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

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?

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 :
set activeByDefault to true in the profile configuration,
run mvn help:active-profiles (to make sure it is now always activated, even without -Pdev1),
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.

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.

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

Related

Show Maven Profile

I want to build a spring-boot Java project without running the integration tests. I'm taking the approach of adding annotation
#Category(IntegrationTest.class)
and defining interface
public interface IntegrationTest {}
I have this in my pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<skipTests>${maven-surefire-plugin.skipUnitTests}</skipTests>
<excludedGroups>
com.test.annotationtype.IntegrationTest
</excludedGroups>
</configuration>
</plugin>
When I run mvn install, it runs the integration test.
It's a multi-profile pom.xml, and I'd like to at least verify that the profile I think I'm running, really is the one actually run.
<profile>
<id>woodsman-default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
...
</profile>
When I run
mvn -debug install
no where in the log do I see the profile id woodsman-default. How can I get Maven to show me which profile it decided to use?
Bonus points for suggesting why it's not ignoring integration tests, but I'd settle for the easy answer.
Thx
Woodsman

Running multiple Maven profiles + Cucumber JVM

Since the Cucumber JVM does not support Profiles, I'm trying to use Maven instead. What I want to do is activate a certain subset of tests based on its tag, and point it to the correct environment.
Here is my feature file:
Feature: Validate data
#dev
Scenario: Dev environment
Given dev data
#qa
Scenario: Test environment
Given test data
Here is my pom.xml:
...
<profiles>
<profile>
<id>dev</id>
<properties>
<cucumber.options>
--tags #dev
</cucumber.options>
<base.url>http://dev.base.url.to.application</base.url>
</properties>
</profile>
<profile>
<id>qa</id>
<properties>
<cucumber.options>
--tags #qa
</cucumber.options>
<base.url>http://qa.base.url.to.application</base.url>
</properties>
</profile>
<profile>
<id>firefox</id>
<properties>
<driver.class>org.openqa.selenium.firefox.FirefoxDriver</driver.class>
</properties>
</profile>
<profile>
<id>chrome</id>
<properties>
<driver.class>org.openqa.selenium.chrome.ChromeDriver</driver.class>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<cucumber.options>${cucumber.options}</cucumber.options>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>
When I try to run multiple profiles at once, i.e., using the following command:
mvn test -P dev,qa
Only one of my tests gets executed. If I run it without profiles, both tests get executed (the qa profile in specific). Have I configured my profiles improperly? Or is it an issue with my tags?
In the dev profile you set the cucumber.options property to --tags #dev. In the qa profile you set the cucumber.options property to --tags #qa. So when combining these profiles with -P dev,qa the qa profile will override the value of the cucucumber.options set by the dev profile.
And because this is a single element combine.children=merge will not work and there is no good way to merge these profiles.

How to configure maven jar plugin to exclude properties on profile

I have this configuration in my pom
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<configuration>
<excludes>
<exclude>**/logging/*</exclude>
<exclude>**/config/*</exclude>
</excludes>
</configuration>
</plugin>
I use profiles to handle different behaviour from local environment to production environment.
Is it possible not activate the exclusions when executing mvn install with local profile?
I tried to set a blank properties on local environment like this
but the plugin complains.
This is a workaround solution, maybe a better one exists. I think the easiest you could do is to let your DEV environment free from any config of the jar plugin. And then place your PROD config in a dedicated profile :
<profiles>
<profile>
<id>PROD</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<configuration>
<excludes>
<exclude>**/logging/*</exclude>
<exclude>**/config/*</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
When you need to build the production jar, launch :
mvn clean install -PPROD
Are the logging and config files resources for testing only? If yes, put them in ${basedir}/src/test/resources. They'll be on the classpath for your tests but will not end up in the final jar, and you won't need specific jar plugin config.
The best workaround I found is to use an invalid value to filter on when executing on DEV environment.
<profiles>
<profile>
<id>env-local</id>
<activation>
<property>
<name>env</name>
<value>local</value>
</property>
</activation>
<properties>
<jndi.iban0>cont0Data</jndi.iban0>
<config.file.path>classpath:config</config.file.path>
<logging.file.path>classpath:logging</logging.file.path>
<exclude.logging>none</exclude.logging>
<exclude.config>none</exclude.config>
</properties>
</profile>
</profiles>

GlassFish 3 + Maven + remote deploy

I couldn't find any clear answer about how to deploy simple Maven based project to remote GlassFish server via maven like
mvn package xxx:deploy
I think only cargo plugin supports GlassFish 3. Right?
I've problems at configuration side.
Any sample remote GlassFish deployment will be great. Cargo is not a must, if others are support remote GlassFish then we can also use it too.
In case you want to only use maven-glassfish-plugin (let say version 2.1), you can do a remote deploy by specifying the "host" parameter. Below is an example where configurations are setup in maven settings.xml and an plugin loads them using a profile:
In settings.xml define a profile:
<profile>
<id>production-config</id>
<properties>
<glassfish.glassfishDirectory>/var/local/glassfish/</glassfish.glassfishDirectory>
<glassfish.user>admin</glassfish.user>
<glassfish.adminPassword>adminadmin</glassfish.adminPassword>
<glassfish.domain.name>prd-domain</glassfish.domain.name>
<glassfish.domain.host>NAMEOFYOURREMOTEHOST</glassfish.domain.host>
<glassfish.domain.adminPort>10161</glassfish.domain.adminPort>
.
.
</properties>
</profile>
Next put this profile in your active profiles:
<activeProfiles>
<activeProfile>production-config</activeProfile>
</activeProfiles>
In your maven project pom.xml, create a profile and add the maven-glassfish-plugin in your list of profiles:
<profile>
<id>production</id>
<activation>
<activeByDefault>false</activeByDefault>
<os>
<arch>x86</arch>
<family>linux</family>
</os>
<property>
<name>profile</name>
<value>production</value>
</property>
<file>
<exists>
${glassfish.glassfishDirectory}/domains/${glassfish.domain.name}/config/domain.passwords
</exists>
</file>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.glassfish.maven.plugin</groupId>
<artifactId>maven-glassfish-plugin</artifactId>
<configuration>
<terse>true</terse>
<echo>true</echo>
<debug>true</debug>
<glassfishDirectory>${glassfish.glassfishDirectory}</glassfishDirectory>
<user>${glassfish.user}</user>
<adminPassword>${glassfish.adminPassword}</adminPassword>
<domain>
<name>${glassfish.domain.name}</name>
<host>${glassfish.domain.host}</host>
<adminPort>${glassfish.domain.adminPort}</adminPort>
</domain>
<components>
<component>
<name>${project.artifactId}</name>
<artifact>${project.build.directory}/${project.build.finalName}.war</artifact>
</component>
</components>
</configuration>
<executions>
<execution>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
This should do the trick. You can run this profile using maven : mvn glassfish:deploy -P production or just mvn deploy -P production (since we have added the goal deploy inside the executions part of plugin)
Using the model above you can create different profile per environment (dev, acc, tst, prd), and use different settings. For instance you can create a developer profile where a local glassfish is being used to deploy and run unit/integration tests on it.
Common mistake people make is to mix up the settings for the machine from where you are doing the remote deployment with the host where deployment is to be installed. glassfishDirectory is place from where you are running the deployment plugin from. As a result of mistake plugin just hangs, doing nothing and just waiting giving the impression that something is happening. Another mistake is to specify a password file instead of a password for a remote deploy which will also result in nothing.
As far as I know and could find around, only Cargo delivers (or deploys, in this case).
This is an example tested as working on a Maven OSGi WAR project:
<build>
<plugins>
...
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.1.2</version>
<configuration>
<container>
<containerId>glassfish3x</containerId>
<type>remote</type>
</container>
<configuration>
<type>runtime</type>
<properties>
<cargo.hostname>myhostname</cargo.hostname>
<cargo.remote.username>myusername</cargo.remote.username>
<cargo.remote.password>mypassword</cargo.remote.password>
</properties>
</configuration>
</configuration>
<dependencies>
<dependency>
<groupId>org.glassfish.deployment</groupId>
<artifactId>deployment-client</artifactId>
<version>3.2-b06</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
As you can see, the trick lies in the deployment-client dependency.
For the sake of completeness, you then just mvn package cargo:deploy and Bob's your uncle.

How to skip unittests when using mvn scm:bootstrap

I'm trying to use the mvn scm plugin to check out the daily tag, and create an assembly from that version of the code. I configured the scm plugin and everythhing is working well, except that I can not seem to tell it to not run the unittests.
I tried:
Passing the -Dmaven.test.skip=true command line parameter
Creating a profile where the surefire plugin skips test, and list that profile in the scm configuration "profiles" section
setting the "maven.test.skip=true" as an environment variable
In all cases, when the scm plugin starts running the goals I told it to run in the configuration (see below), it also runs the unittests.
Below is the example I used to skip tests by using a profile:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
<version>1.0</version>
<configuration>
<goals>install,assembly:assembly</goals>
<profiles>skiptest</profiles>
</configuration>
</plugin>
And this is the profile (I defined this in the pom.xml of the project):
<profiles>
<profile>
<id>skiptest</id>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
The command I use to do the checkout and bootstrap is:
mvn scm:bootstrap -DscmVersion=daily-20110427-421 -DscmVersionType=tag
I'm running mvn 2.2.1 on a Linux machine, and doing a checkout from a CVS repository. It's an existing project, I have continuous integration and tagging all up and running, I just want to check out a daily tag and create an assembly from that.
Any tips are much appreciated.
Edit: Got it to work with the answer below, but only after I upgraded to maven-scm-plugin version 1.1. Apparently, 1.0 did not propagate profiles.
Try this in the profile:
<profiles>
<profile>
<id>skiptest</id>
<properties>
<maven.test.skip>true</maven.test.skip>
</properties>
</profile>
</profiles>

Resources