Building multiple Maven profiles for a single Jenkins job - maven

I am trying to build multiple Maven profiles in a single Jenkins job. Each profile changes some code and then creates a jar by executing mvn -Pdev install then mvn -Pprod install in the command line (According to Maven using mvn -Pdev,prod install is supposed to work but it isn't working for me). Here are the two profiles in my project's pom.xml:
<profiles>
<!-- prod profile -->
<profile>
<id>prod</id>
<build>
<plugins>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.2</version>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<file>src/main/java/com/IQzone/android/configuration/AbstractHoldingRefreshable.java</file>
<replacements>
<replacement>
<token>TrUe</token>
<value>TOAST_SWITCH</value>
</replacement>
</replacements>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>prod</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<!-- dev profile -->
<profile>
<id>dev</id>
<build>
<plugins>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.2</version>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<file>src/main/java/com/IQzone/android/configuration/AbstractHoldingRefreshable.java</file>
<replacements>
<replacement>
<token>TOAST_SWITCH</token>
<value>TrUe</value>
</replacement>
</replacements>
</configuration>
</plugin>
<!-- build project with JAVA 1.6 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>dev</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
How would I setup Jenkins to automatically build both of these profiles for a single Jenkins job whenever the job is hit for a build? And put both of these jars in the Artifactory? I have very little Jenkins knowledge and there isn't much information on this on the web.

You could create a Jenkins matrix job. A matrix job allows the same job to run with changing settings (in your case: a string).
Each changing setting is called an axis. In your case you would create a string axis containing the two values: dev and prod.
That way your job would run twice, with both values.
However: your usage of profiles is dangerous. Since the profile used to run the build is not codified into your artifact, your break the "one source revision should always lead to exactly the same target artifact" contract of Maven (see: http://www.blackbuild.com/how-to-really-use-maven-profiles-without-endangering-your-karma/ for a more detailed explanation)
Consider creating either two different artifacts using classifier (-dev and -prod) or even better: create two separate modules of your build, each one creating only one of your target artifacts.

In Maven, if you use mvn -Pdev,prod, then you are activating both profiles simultaneously in one command.
It seems you want 2 distinct run of the command, i.e. something you would achieve on the command line by doing 2 builds:
mvn -Pdev install; mvn -Pprod install
In jenkins you can achieve this with either
one free style project job (with 2 shell builders running the mvn -P$PROFILE install tasks)
2 maven type jobs (that you can chain one after the other using "build after other projects are built").

In addition to Matrix job and multiple maven invocations in a free-style job, there's another way: Run top-level Maven targets as a pre-build step and run the other command via maven jenkins plugin.
Make sure that the pre-build step uses the same maven repo as the other command by supplying -Dmaven.repo.local=/${whatever-it-is}/${EXECUTOR_NUMBER}.
Please refer to other answers for details on matrix job, etc.

You can do it by setting different execution ids to each execution and then trigger the command
mvn -Pdev,prod clean package install

Related

Maven failsafe is not complaining that an inexistent profile was specified

I have two profiles in my pom:
<profiles>
<profile>
<id>functional-tests</id>
<build>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<testSourceDirectory>test/test-functional/java</testSourceDirectory>
<includes>
<include>**/*FT.java</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>it-tests</id>
<build>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<testSourceDirectory>test/test-it/java</testSourceDirectory>
<includes>
<include>**/*IT.java</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
...
I can trigger each of these two profiles like this:
mvn failsafe:integration-test -Pfunctional-tests
mvn failsafe:integration-test -Pit-tests
But when I run this:
mvn failsafe:integration-test -PrandomWord
It triggers it-tests profile. I was wondering why and if there is a way to have failsafe plugin output something like unrecognised profile.
Thank you for your help
In case it matters, here is my failsafe-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
</executions>
</plugin>
Answering your question about why it triggers it-tests. In fact, it does not activate any of the profiles, hence default plugin configuration is used which has **/*IT.java in include list. So, it runs all IT tests by default.
This is weird approach to manage plugin executions by profiles. I doubt there is a reasonable way to validate profile names as you describe. I would recommend another approach here.
Approach 1. Use <id> and cli with #
You could just specify two executions of the plugin with id and then you could do this: How to execute maven plugin execution directly from command line?
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<id>it-tests</id>
<phase>none</phase> <!-- detach this execution from default lifecycle -->
<configuration>
<testSourceDirectory>test/test-it/java</testSourceDirectory>
<includes>
<include>**/*IT.java</include>
</includes>
</configuration>
</execution>
<execution>
<id>functional-tests</id>
<phase>none</phase> <!-- detach this execution from default lifecycle -->
<configuration>
<testSourceDirectory>test/test-ft/java</testSourceDirectory>
<includes>
<include>**/*FT.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
Now you could execute it from command line:
mvn failsafe:integration-test#it-tests
mvn failsafe:integration-test#functional-tests
Update: No need to specify goals as it is relevant to lifecycle only, we type it in command line anyway.
Approach 2. Use <skip> and properties
Keep both executions as a part of lifecycle but control execution by providing skip flags. I.e. define two properties e.g. skip.tests.it=true, skip.tests.ft=true and add <skip>${skip.tests.ft}</skip> to relevant configuration sections. Then you could just do
# run with no tests by default
mvn verify
# run with only FT
mvn verify -Dskip.tests.ft=false
# run with all tests
mvn verify -Dskip.tests.ft=false -Dskip.tests.it=false
to run full lifecycle together with desired tests.

Run Maven goal only in parent POM by activation

I am working on integrating a plugin into a multi-module project.
I am using a 3rd party plugin that essentially needs to only by run from the parent project (based on my understanding and usage of it). I tried to accomplish this by using a profile, like so:
<profiles>
<profile>
<id>run-my-guy</id>
<build>
<plugins>
<plugin>
<groupId>com.myproject</groupId>
<artifactId>myproject-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>runThing</goal>
</goals>
<inherited>false</inherited>
</execution>
</executions>
<inherited>false</inherited>
</plugin>
</plugins>
</build>
</profile>
</profiles>
I have several <inherited>false</inherited>, but if I run mvn help:all-profiles I can still see this profile in every single module. If I run my mvn package -P run-my-guy I see this get executed in every single subproject. I want the ability to activate this and I do not want it to be on by default.
If I try to add it the <build> section, like this:
<build>
<plugins>
<plugin>
<groupId>com.myproject</groupId>
<artifactId>myproject-maven-plugin</artifactId>
<inherited>false</inherited>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>runThing</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Here, I also have a few <inherited>false</inherited>, just to try and enforce that the plugin and the execution are not inherited. However, whenI run the package phase, or anything that includes that phase, the runThing goal is included.
How do I run a goal only by activation (like profile or some other feature, or just by explicitly running the goal) and only in the parent?
As shown in an answer for "Run a single Maven plugin execution?", it is now possible (since Maven 3.3.1) to specify an execution Id for a direct goal invocation.
pom.xml
<build>
<plugins>
<plugin>
<groupId>com.myproject</groupId>
<artifactId>myproject-maven-plugin</artifactId>
<inherited>false</inherited>
<executions>
<id>myproject-exec-id</id> <!-- note the execution Id -->
<execution>
<phase>none</phase>
<goals>
<goal>runThing</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
And then invoking the goal from the command line uses the optional #executionId parameter:
mvn myproject:runThing#myproject-exec-id

Is isolation of failsafe and surefire runs from each other using a skip approach possible?

The value of property skipITs when overridden on the command line appears to be ignored by maven if used in conjunction with -Dit.test=full.Classname. The specified failsafe test will not be run (case one).
As opposed to this specifiing skipITs without the it.test switch leads to running of existing failsafe tests (case two).
Background: Am trying to isolate surefire tests from failsafe tests runs by namely either running both types of them or one of each only. The maven calls are:
mvn -Pintegration -DskipTests=true -DskipITs=false -Dit.test=full.Classname verify
in the first case and:
mvn -Pintegration -DskipTests=true -DskipITs=false verify
in the second.
The relevant configuration (pom.xml, snippets only) being:
<properties>
<skipTests>false</skipTests>
<skipITs>false</skipITs>
</properties>
(those are defaults) and
<profiles>
<profile>
<id>integration</id>
<build>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<id>custom<id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<skipTests>${skipITs}</skipTests>
<skip>${skipITs}</skip>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Did you observe that too or have you eventually found a better working approach?
By default, the Surefire plugins runs during the test phase and usually you configure the Failsafe plugin to run during the integration-test and verify phase like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.18.1</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
To run the Surefire tests only, use
mvn clean test
To run both the Surefire and the Failsafe tests, use
mvn clean verify
You can completely skip any plugin by using the <skip> configuration option. If you configure the Surefire plugin like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<skip>${skip.surefire}</skip>
</configuration>
</plugin>
you can run only the Failsafe tests by calling
mvn clean verify -Dskip.surefire=true
If the integration test (it.test) has a non-default name I need to add an include pattern accordingly, like in (pom.xml snippet):
<profiles>
<profile>
<id>dbUnit</id>
<build>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<id>custom</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<skipTests>${skipITs}</skipTests>
<skip>${skipITs}</skip>
<includes>
<include>**/*DbTest.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
But that appears valid only for the original approach. Also observe that the default inclusion patterns might need to be added too in case for your integration tests you followed different naming schemes.

Is it possible to get the list of active profiles in Maven?

In Maven, is there a way to get a list of the active profiles, say, as a property or as a text file?
More specifically, when I run:
mvn resources:resources -P MyProfile
I want to get the string MyProfile somewhere I can read it into my Java program.
Maven 3.2.1
Thanks
Edit
I attempted to configure the Maven Help plugin to run the active-profiles goal whenever the goal resources:resources is run by configuring an execution to participate in the process-resources phase as shown below. That did not work either ...:
<packaging>jar</packaging>
<build>
<plugins>
<!-- ... -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-help-plugin</artifactId>
<version>2.2</version>
<configuration>
<output>${basedir}/target/active-profiles.txt</output>
</configuration>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>active-profiles</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
You could try this maven plugin. The configuration below will create a text file that will contain the profiles that were active during the build.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-help-plugin</artifactId>
<version>2.2</version>
<configuration>
<output>${basedir}/target/active-profiles.txt</output>
</configuration>
</plugin>

Can I modify the Maven deploy phase to replace the maven-deploy-plugin with my own plugin?

I'm pretty new to Maven...
What I'm trying to do is skip the maven-deploy-plugin during the deploy phase, while replacing it with my own plugin (i.e. I'm deploying to a non-repository location).
I realize I could do this in multiple other ways, but the boss wants to be able to run:
mvn deploy
To get the results of my current workaround, which is disabling the maven-deploy-plugin (which seems to be disabling the entire deploy phase), and manually specifying the custom upload goal from the command line.
I'm currently failing to succeed in my mission with:
<executions>
<execution>
<phase>deploy</phase>
</execution>
</executions>
in the build/plugins/plugin section containing my plugin specification, since the deploy phase is skipped by:
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
Thanks!
disabling the maven-deploy-plugin (which seems to be disabling the entire deploy phase)
This is not correct. Disabling maven-deploy-plugin doesn't disable the entire deploy phase. This is how it should be done (looks like you're doing it already):
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
Try this (untested) alternative for disabling the standard deploy plugin:
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>default-deploy</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
I want to build on #yegor256's answer a bit... 8 years, 4 months later!
I found myself here getting into the weeds on some legacy Maven configurations that were full of cruft. Coming from a Maven mindset, albeit some years between now and active hacking, I was re-familiarizing myself with the Maven lifecycle.
TLDR... mvn help:effective-pom is your friend. Use your IDE's tools for viewing the effective POM often (NetBeans makes it easy. I added a keyboard shortcut in IntelliJ.)
In the configuration I was reviewing, the previous developers had created two (2) deploy-file executions, one war, one jar.
<build>
...
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<executions>
<execution>
<id>deploy-war</id>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
... omitted ...
</configuration>
</execution>
<execution>
<id>deploy-jar</id>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
... omitted ...
</configuration>
</execution>
</executions>
</plugin>
...
</plugins>
...
</build>
I was aware that these executions would be appended to the default-deploy bound to the deploy phase and observed this behavior in the logs. The default-deploy would run, uploading an empty war file, then the deploy-war would run, uploading, and overwriting, the first war file.
Several options exist.
skip and combine.self="override" (my preference)
As presented, using <skip> as a <configuration> option is viable. It is safe and more portable than setting setting the <phase> to none.
However, it will be inherited by the other executions (certainly as presented). To prevent this, you must explicitly tell your additional <execution> configurations to not inherit.
...
...
<executions>
<execution>
<id>deploy-war</id>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration combine.self="override">
... omitted ...
</configuration>
</execution>
...
...
Override default-deploy
Another option, possibly more verbose and lest esoteric than combine.self="override" is to override the execution of the default-deploy <id> of the plugin.
...
<execution>
<id>default-deploy</id>
<configuration>
<skip>true</skip>
</configuration>
</execution>
...
This will not be inherited by the additional <executions>.
Another option
As #yegor256 notes, but in the additional configurations explicitly state <skip>false</skip> to "reset" the inherited <skip> from the plugin.
HTH.

Resources