Maven: how to exec a pom twice with different profiles? - maven

I want to make a build with Maven within a pom which can be built with two different profiles.
In other words, my task is to force the build in order that the build produces two different builds in the target folder as like I will execute "maven install -P" two times.
So, to be more clear:
one single invocation
two different results based on the profile associated in that time
I've tried the exec-maven-plugin but I'm not sure if is the correct way, because it's not doing what I desire.
How can I perform that?
Here's the pom I'm trying.
The tasks are:
copy resources from another project
replace content based on the profile
Build both profiles.
pom.xml
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>MavenDoubleInstallation</artifactId>
<profiles>
<profile>
<id>profileA</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<env>profileA</env>
<token>/path/for/profileA</token>
</properties>
</profile>
<profile>
<id>profileB</id>
<properties>
<env>profileB</env>
<token>/path/for/profileB</token>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<includeEmptyDirs>true</includeEmptyDirs>
</configuration>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes/${env}/result</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/../SourceProject/resources/result</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<basedir>${basedir}/target/classes/${env}/result</basedir>
<includes>
<include>**/*.sh</include>
</includes>
<replacements>
<replacement>
<token>%%TOKEN%%</token>
<value>${token}</value>
</replacement>
</replacements>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>build-profileA</id>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>maven</executable>
<arguments>
<argument>-P profileA -X</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>build-profileB</id>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>maven</executable>
<arguments>
<argument>-P profileB -X</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Related

Running snyk-maven-plugin with current git branch

I have a multi-module project with a snyk-maven-plugin in my parent's pom.xml:
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>.....</groupId>
<artifactId>.....</artifactId>
<version>......</version>
<packaging>pom</packaging>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>io.snyk</groupId>
<artifactId>snyk-maven-plugin</artifactId>
<version>2.0.0</version>
<inherited>false</inherited>
<executions>
<execution>
<id>snyk-test</id>
<goals>
<goal>test</goal>
</goals>
</execution>
<execution>
<id>snyk-monitor</id>
<goals>
<goal>monitor</goal>
</goals>
</execution>
</executions>
<configuration>
<apiToken>${env.SNYK_TOKEN}</apiToken>
<args>
<arg>--all-projects</arg>
<arg>--target-reference=${git.branch}</arg>
</args>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
I run "mvn snyk:monitor" that should check all projects in my solution.
I'd like to report with a --target-reference to take a value of ${git.branch}
But then I need another plugin to generate this property:
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>4.9.10</version>
<executions>
<execution>
<id>get-the-git-infos</id>
<phase>package</phase>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
<configuration>
<dotGitDirectory>${project.basedir}/../.git</dotGitDirectory>
<skipPoms>false</skipPoms>
<runOnlyOnce>true</runOnlyOnce>
<useNativeGit>true</useNativeGit>
<verbose>true</verbose>
<generateGitPropertiesFile>false</generateGitPropertiesFile>
<includeOnlyProperties>
<includeOnlyProperty>^git.branch$</includeOnlyProperty>
</includeOnlyProperties>
</configuration>
</plugin>
How do I wrap these 2 plugins together?
git-commit-id-plugin should retrieve the git.branch and pass it to snyk-maven-plugin.
The "mvn snyk:monitor" has to be changed somehow to execute git-commit-id-plugin first.

Integrate create-react-app into maven build process

I am trying to integrate a recently created ReactJs application into the build process of the rest of my larger maven based application. I have done this in the past for an AngularJs application using npm, Gulp, and Bower. The difference this time around is that I am using Yarn but also the create-react-app base application from Facebook which relies on the 'react-scripts' development dependencies.
Previously my pom.xml for the maven project that built the AngularJs app looked as so:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>myapp</artifactId>
<packaging>war</packaging>
<name>MyApp Service</name>
<description>MyApp Service</description>
<parent>
...
</parent>
<profiles>
<profile>
<id>exec-unix</id>
<activation>
<os>
<family>unix</family>
</os>
</activation>
<properties>
<npmexec>npm</npmexec>
<gulpexec>gulp</gulpexec>
<bowerexec>bower</bowerexec>
</properties>
</profile>
</profiles>
<properties>
<frontendDir>src/main/myappui</frontendDir>
</properties>
<dependencies>
...
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<configuration>
<filesets>
<fileset>
<directory>src/main/webapp</directory>
</fileset>
</filesets>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>npm install</id>
<goals>
<goal>exec</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<executable>${npmexec}</executable>
<arguments>
<argument>install</argument>
</arguments>
<workingDirectory>${frontendDir}</workingDirectory>
</configuration>
</execution>
<execution>
<id>bower install</id>
<goals>
<goal>exec</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<executable>${bowerexec}</executable>
<arguments>
<argument>install</argument>
</arguments>
<workingDirectory>${frontendDir}</workingDirectory>
</configuration>
</execution>
<execution>
<id>gulp build</id>
<goals>
<goal>exec</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<executable>${gulpexec}</executable>
<arguments>
<argument>prepare-for-maven-war</argument>
</arguments>
<workingDirectory>${frontendDir}</workingDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I am not particularly sure what to do about the 'react-scripts' part. I tried adding <yarnexec>yarn</yarnexec> with an execution for yarn install and yarn build but it didn't provide desired results.

spring-boot-maven-plugin reverts the property files after run

This is the build configuration of my project in pom.xml, When I pack the project, the property files already changed with the selected profile properties, but when I run the project using maven-boot-plugin:run in Intellij IDEA, the property files changed to old style and the database connection raises exception. for example the normal application.properties has the below key :
spring.datasource.url=${datasourceUrl}
and after packing the project it is :
spring.datasource.url=url1
but after running spring boot plugin the property files changed to the first style.
<profiles>
<profile>
<id>Live</id>
<properties>
<datasourceUrl>url1</datasourceUrl>
</properties>
</profile>
<profile>
<id>Test</id>
<properties>
<datasourceUrl>url2</datasourceUrl>
</properties>
</profile>
</profiles>
<build>
<defaultGoal>install</defaultGoal>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.3</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<id>run</id>
<phase>package</phase>
<goals>
<goal>resources</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>repack</id>
<phase>package</phase>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
mywar
<type>war</type>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/classes/static/</outputDirectory>
<includes>**/*</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

maven release plugin with phase and jar-with-dependencies

I have a build set up to run a variety of tasks when I run mvn release:prepare and mvn:release:perform. Specifically, I have a phase set up so that my javadocs and source-plugins are run only when I release. This allows my build to avoid a lot of time for the common case of mvn clean install. I'd like to add to this my maven-assembly-plugin jar-with-dependencies so only when I release the assembly plugin is run.
Here's what my build looks like:
<?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>example</groupId>
<artifactId>example</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Example</name>
<build>
<plugins>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<releaseProfiles>release</releaseProfiles>
<goals>deploy</goals>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>release</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<doclet>org.asciidoctor.Asciidoclet</doclet>
<docletArtifact>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoclet</artifactId>
<version>0.1.3</version>
</docletArtifact>
<linksource>true</linksource>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
the maven-source-plugin and maven-javadoc-plugin both run during the release phase, but the maven-assembly-plugin does not. What do I have to do to make this plugin run during a maven release?
The single isn't bound to a default phase, so you have to specify it in the execution-block.
If you compare it with the javadoc:jar you'll see that this goal is by default bound to the package phase.

How to override default binding to phase of a Maven plugin

I want to define different executions for plugins in the pluginManagement of my parent pom and then bind specific executions to phases in the child poms.
I see inconsistent behavior depending on the plugin used and the location of the pluginManagement section.
In this first example the pluginManagement is located in the parent pom, defining 2 executions for compiler plugin and 2 executions for antrun plugin.
<?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/xsd/maven-4.0.0.xsd">
<name>master-pom</name>
<modelVersion>4.0.0</modelVersion>
<groupId>plugin.test</groupId>
<artifactId>master-pom</artifactId>
<packaging>pom</packaging>
<version>1.0.0-SNAPSHOT</version>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>execution-1</id>
<phase>none</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<source>1.6</source>
<target>1.6</target>
<includes>
<include>**/*</include>
</includes>
</configuration>
</execution>
<execution>
<id>execution-2</id>
<phase>none</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<source>1.5</source>
<target>1.5</target>
<includes>
<include>**/*</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>execution-1</id>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<echo message="execution 1"/>
</target>
</configuration>
</execution>
<execution>
<id>execution-2</id>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<echo message="execution 1"/>
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
And the child pom:
<?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/xsd/maven-4.0.0.xsd">
<name>build</name>
<modelVersion>4.0.0</modelVersion>
<groupId>plugin.test</groupId>
<artifactId>build</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>plugin.test</groupId>
<artifactId>master-pom</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>./master-pom.xml</relativePath>
</parent>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>execution-1</id>
<phase>generate-sources</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>execution-1</id>
<phase>generate-sources</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
Running 'mvn clean install' on the child pom will run both executions of the compiler plugin and only the 1st execution of the antrun plugin, although only the 1st execution of each was bound to a phase.
Now moving the pluginManagement to the child pom:
<?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/xsd/maven-4.0.0.xsd">
<name>build</name>
<modelVersion>4.0.0</modelVersion>
<groupId>plugin.test</groupId>
<artifactId>build</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>execution-1</id>
<phase>none</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<source>1.6</source>
<target>1.6</target>
<includes>
<include>**/*</include>
</includes>
</configuration>
</execution>
<execution>
<id>execution-2</id>
<phase>none</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<source>1.5</source>
<target>1.5</target>
<includes>
<include>**/*</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>execution-1</id>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<echo message="execution 1"/>
</target>
</configuration>
</execution>
<execution>
<id>execution-2</id>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<echo message="execution 2"/>
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>execution-1</id>
<phase>generate-sources</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>execution-1</id>
<phase>generate-sources</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
This pom gives the desired behavior which is running only the 1st execution for each plugin.
The compiler plugin (and most others) works correctly only in the case where the pluginManagement is located in the same pom and every execution is bound to phase=none (probably because it binds executions to a default phase).
The antrun plugin works correctly in any case.
How can I achieve this while having the pluginManagement section in the parent pom and without having to specifically bind unwanted executions to phase=none in the child poms?
Is this a bug in Maven or is this behavior somehow justified?
I have tried this on Maven 3.0.4 and Maven 2.2.1 with the same results.
The example provided works correctly. I had not redeployed the parent after including the fix.
Most plugins will bind executions to a default phase. So when one execution of a plugin is triggered, all unbound executions will be bound to the default phase and will also run.
To avoid this behavior, all executions of the plugin in the pluginManagement section of the parent pom should be bound to phase=none (as shown in the provided example). This way no execution will run unless the phase is explicitly overridden in the child poms.

Resources