How do I invoke two different profiles in one maven command? - java

I have two profiles for different environments in pom.xml, I have to run mvn -PTest1 install and mvn -PTest2 install command to get these profiles in use. Can we integrate two separate maven commands in a single one (like mvn clean install)?
Here is my Pom entry
<profiles>
<profile>
<id>Test1</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.5</jdk>
<os>
<name>Windows XP</name>
<family>Windows</family>
<arch>x86</arch>
<version>5.1.2600</version>
</os>
<property>
<name>sparrow-type</name>
<value>African</value>
</property>
</activation>
<dependencies>
<dependency>
<groupId>
com.endeca
</groupId>
<artifactId>
endeca_navigation_Test1
</artifactId>
<version>
6.1
</version>
<!--<version>stable</version> -->
<scope>
compile
</scope>
</dependency>
</profile>
<profile>
<id>Test2</id>
<activation>
<activeByDefault>false</activeByDefault>
<jdk>1.5</jdk>
<os>
<name>Windows XP</name>
<family>Windows</family>
<arch>x86</arch>
<version>5.1.2600</version>
</os>
<property>
<name>sparrow-type</name>
<value>African</value>
</property>
</activation>
<dependencies>
<dependency>
<groupId>
com.endeca
</groupId>
<artifactId>
endeca_navigation_Test2
</artifactId>
<version>
6.1
</version>
<!--<version>stable</version> -->
<scope>
compile
</scope>
</dependency>
</dependencies>
</profile>
</profiles>
It will helpfull to manage hudson job using single command

Based on the documentation and discussion here, try separating profile names with a comma:
mvn install -P Test1,Test2

Mifeet's answer is correct, but in Windows PowerShell you should quote parameters, otherwise you'll get "unknown lifecycle phase" error.
mvn install -P 'Test1,Test2'

For me Mifeet's answer isn't working. I get "unknown lifecycle phase Test2". For me this is working:
mvn install -PTest1 -PTest2

Based on the maven help command
-P,--activate-profiles <arg> Comma-delimited list of profiles to activate
So you can run mvn package -Pp1,p2 to run profile id with p1 and p2

Related

Maven pom.xml argument that will install different dependencies depending on the value [duplicate]

This question already has answers here:
Different dependencies for different build profiles
(2 answers)
Closed 3 years ago.
I would like to have a property/argument on Maven that will install a different dependency depending on the argument.
That is, when a user specifies -DgpuCuda=True, the dependency on the pom.xml will change accordingly.
So mvn -gpuCuda=True install will install DL4J-GPU instead of DL4J-CPU.
If -gpuCuda=True is specified, then this will be installed:
<dependency>
<groupId>org.nd4j</groupId>
<artifactId>nd4j-cuda-10.1</artifactId>
<version>1.0.0-beta4</version>
</dependency>
If -gpuCuda=False, this will be installed:
<dependency>
<groupId>org.nd4j</groupId>
<artifactId>nd4j-native</artifactId>
<version>1.0.0-beta4</version>
</dependency>
Is this possible? What would a workaround be?
Thanks!!
You add something like
<profiles>
<profile>
<id>gpu</id>
<activation>
<property>
<name>gpuCuda</name>
<value>True</value>
</property>
</activation>
<dependencies>
<dependency>
<groupId>org.nd4j</groupId>
<artifactId>nd4j-cuda-10.1</artifactId>
<version>1.0.0-beta4</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>cpu</id>
<activation>
<property>
<name>gpuCuda</name>
<value>False</value>
</property>
</activation>
<dependencies>
<dependency>
<groupId>org.nd4j</groupId>
<artifactId>nd4j-native</artifactId>
<version>1.0.0-beta4</version>
</dependency>
</dependencies>
</profile>
</profiles
Then you activate/deactivate the profiles by the command line properties you stated, like mvn -DgpuCuda=True install.

Profile activation in appengine:deploy

I'm trying to activate profiles of an AppEngine application using the maven command like the following :
mvn appengine:deploy -Dspring.profiles.active=prod
But it is ignored.
Is it possible to activate profiles using maven ?
I succeeded by linking Maven Profiles to Spring Profiles. In the following I explain how I did :
1 - Create Maven Profiles:
In pom.xml I identified my maven profiles, and will link them later to spring profiles by storing them in "spring.profiles.to.activate" property :
<!-- PROFILES -->
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<spring.profiles.to.active>dev</spring.profiles.to.active>
</properties>
</profile>
<profile>
<id>uat</id>
<properties>
<spring.profiles.to.active>uat</spring.profiles.to.active>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<spring.profiles.to.active>prod</spring.profiles.to.active>
</properties>
</profile>
</profiles>
2 - Activate Maven filtering :
I activated filtering in folder ${basedir}/src/main/webapp, by adding maven-war-plugin to build.
This will allow us to resolve placeholders ${...} (In this particular case ${spring.profiles.to.activate}) in the mentioned folder.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resources>
<directory>${basedir}/src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
<targetPath>WEB-INF</targetPath>
</resources>
</webResources>
</configuration>
</plugin>
3- Activate profile Spring
In appengine-web.xml declare the system property : "spring.profiles.active" as being the maven property ${spring.profiles.to.activate}
<appengine-web-app
xmlns="http://appengine.google.com/ns/1.0">
<version>1</version>
<threadsafe>true</threadsafe>
<runtime>java8</runtime>
<system-properties>
<property name="spring.profiles.active" value="${spring.profiles.to.active}" />
</system-properties>
</appengine-web-app>
4 - Deploy to Appengine
# Dev
mvn appengine:deploy -Pdev
# UAT
mvn appengine:deploy -Puat
#PROD
mvn appengine:deploy -Pprod
#dev profile, try adding space between -P and dev
mvn appengine:deploy -P dev
#uat profile, try adding space between -P and uat
mvn appengine:deploy -P qa
#prod profile, try adding space between -P and prod
mvn appengine:deploy -P prd

Conditional inclusion of JARs in a WAR built by Maven

I have to build a war file using maven to include jars conditionally,
each jar is created by a seperate maven project which deploys jar to nexus(our organisations remote) repository
Eg : I have jars like these core.jar,reward.jar,payment.jar,domains.jar so on
I need to build a final war based on conditions(environmnet) to include above jars
Combination of final war(w1)
w1.war : core.jar,domains.jar
w1.war : core.jar,domains.jar,rewards.jar(Any way to specify to include this jar if rewards is applicable)
The Maven WAR Plugin allows you to include/exclude JARs. For example:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<packagingExcludes>
WEB-INF/lib/excluded.jar
</packagingExcludes>
<packagingIncludes>
WEB-INF/lib/included.jar
</packagingIncludes>
</configuration>
</plugin>
You can associate the inclusions/exclusions with a condition by using profiles. For example, let the WAR plugin use properties (${excludedResources}, ${includedResources}) ...
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<packagingExcludes>
${excludedResources}
</packagingExcludes>
<packagingIncludes>
${includedResources}
</packagingIncludes>
</configuration>
</plugin>
... and define values for those properties via profiles:
<profiles>
<profile>
<id>prod</id>
<properties>
<excludedResources>WEB-INF/lib/a.jar,WEB-INF/lib/b.jar</excludedResources>
<includedResources>WEB-INF/lib/c.jar</includedResources>
</properties>
</profile>
<profile>
<id>tst</id>
<properties>
<excludedResources>WEB-INF/lib/x.jar,WEB-INF/lib/y.jar</excludedResources>
<includedResources>WEB-INF/lib/z.jar</includedResources>
</properties>
</profile>
</profiles>
So, you can use the Maven WAR Plugin's built-in ability to tweak the WAR contents and you can make these tweaks conditional by using Maven profiles.
You can try to use profiles capabilities in maven. Each dependency can be included into its own profile block e.g. domains will be included only if you switch this profile on and services - by services profile.
At the same time you can identify common jars through the common dependency block (in our case core.jar will be common)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.stackoverflow</groupId>
<artifactId>conditional-war</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>War Which Includes Jar By Conditions</name>
<!-- Common dependency block which will be always included -->
<dependencies>
<dependency>
<groupId>com.stackoverflow</groupId>
<artifactId>core</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<profiles>
<!-- Profile for domains jars. Will be included by
profile\condition "domains" -->
<profile>
<id>domains</id>
<activation>
<property>
<name>domains</name>
</property>
</activation>
<dependencies>
<dependency>
<groupId>com.stackoverflow</groupId>
<artifactId>domains</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</profile>
<!-- Profile for domains jars. Will be included by
profile\condition "services" -->
<profile>
<id>services</id>
<activation>
<property>
<name>services</name>
</property>
</activation>
<dependencies>
<dependency>
<groupId>com.stackoverflow</groupId>
<artifactId>services</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</profile>
</profiles>
</project>
Command line for the activation can be following:
By this command line will be included core.jar and domains.jar
mvn clean install -Pdomains
In such case war will include core.jar and services.jar
mvn clean install -Pservices
And finally by this command line will be included all the jars
mvn clean install -Pdomains,services

What is wrong with my Maven Config?

I want to checkout sonar, so I added the following snippet to my pom.xml the dependency part was taken from http://maven.apache.org/general.html#tools-jar-dependency
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.jdbc.url>jdbc:derby://localhost:1527/sonar;create=true</sonar.jdbc.url>
<sonar.jdbc.driverClassName>org.apache.derby.jdbc.ClientDriver
</sonar.jdbc.driverClassName>
<sonar.jdbc.username>sonar</sonar.jdbc.username>
<sonar.jdbc.password>sonar</sonar.jdbc.password>
<sonar.host.url>http://localhost:8080/sonar</sonar.host.url>
</properties>
<dependencies>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.4.2</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
</profile>
Unfortunatly the error persists
Embedded error: Missing:
----------
1) com.sun:tools:jar:1.4.2
Try downloading the file manually from the project website.
Then, install it using the command:
mvn install:install-file -DgroupId=com.sun -DartifactId=tools -Dversion=1.4.2 -Dpackaging=jar -Dfile=/path/to/file
I also followed the suggestion to add the missing jar manually to the repository, which had no effect.
mvn install:install-file -DgroupId=com.sun -DartifactId=tools -Dversion=1.4.2 -Dpackaging=jar -Dfile=$JAVA_HOME/lib/tools.jar
What am I doing wrong?
EDIT:
I verified that tools.jar has been added to my local repository. In debug mode maven shows the error:
1 required artifact is missing.
for artifact:
group:artifact:war:1.0.0-BUILD-SNAPSHOT
from the specified remote repositories:
central (http://repo1.maven.org/maven2)
Are you running this in eclipse? If the answer is yes, this is an annoying and very misunderstood problem. Take a look at my answer here
You may not be pointing eclipse to the right jre/jdk when you're starting up (this is something you didn't necessarily configure rather was Windows)
A problem I had once was different location of tools.jar under Mac OS. Here's the profiles section to solve the problem:
<profiles>
<profile>
<id>java-home-parent-lib-tools-jar</id>
<activation>
<activeByDefault>false</activeByDefault>
<file>
<exists>${java.home}/../lib/tools.jar</exists>
</file>
</activation>
<dependencies>
<dependency>
<groupId>sun.jdk</groupId>
<artifactId>tools</artifactId>
<version>1.5.0</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
</profile>
<profile>
<id>java-home-parent-classes-classes-jar</id>
<activation>
<activeByDefault>false</activeByDefault>
<file>
<exists>${java.home}/../Classes/classes.jar</exists>
</file>
</activation>
<dependencies>
<dependency>
<groupId>sun.jdk</groupId>
<artifactId>tools</artifactId>
<version>1.5.0</version>
<scope>system</scope>
<systemPath>${java.home}/../Classes/classes.jar</systemPath>
</dependency>
</dependencies>
</profile>
</profiles>
However I am not sure this is something you're facing.
If your JAVA_HOME points to your jdk (e.g./usr/lib/jvm/jdk1.6.0_33), your tools.jar configuration (${java.home}/../lib/tools.jar) indicates that there should be a tools jar with the following path: /usr/lib/jvm/lib/tools.jar.
If you change the tools jar path to ${java.home}/lib/tools.jar and verify that in your JAVA_HOME/lib there is the tools.jar file, it should work.
There you can find the related jira.

How to activate a pom.xml iff the os is not of a given family (eg. mac)?

From Maven's website:
...
<profiles>
<profile>
<id>default-tools.jar</id>
<activation>
<property>
<name>java.vendor</name>
<value>Sun Microsystems Inc.</value>
</property>
</activation>
<dependencies>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.4.2</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
</profile>
</profiles>
...
tools.jar is already inclduded on Macs, embedded with classes.jar. Was there no way to specify !mac in the activation settings (except for listing every os except the mac) in the pom.xml, instead of always getting:
ERROR>warning: [path] bad path element ""/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/lib/tools.jar"": no such file or directory
You can use the '!' modifier in the os section of activation (tried and works with Maven 3.0.3). Uses the same algorithm as the enforcer plugin, described here.
<profiles>
<profile>
<id>not-mac</id>
<activation>
<os>
<family>!mac</family>
</os>
</activation>
<properties>
<!-- Define non-mac properties here -->
</properties>
</profile>
</profiles>
See this article for a more complete description of using tools.jar in Maven.
I found another method. Have the dependency in an "activeByDefault" profile. Have another profile (profile-2) which gets activated if the OS is mac. According to this the "activeByDefault" profiles get deactivated if any of the other profiles get activated. So if the OS is mac, profile-2 will get activated, which will deactivate the "activeByDefault" profile.

Categories

Resources