How do I build 2 docker images from maven, in separate phases? - maven

I use io.fabric8:docker-maven-plugin:0.28.0 in a Maven project and I need to build 2 different Docker images, in different phases:
One for the integration-test phase, to run some database for the integration tests. I would build it with some resources from the project (SQL files defining the schema).
One at the end, after the tests have passed (maybe during package), with the complete application.
My problem is that only the first image is built.
I tried to reference the image aliases in the <configuration> of the <execution> elements, but it doesn't seem to work. Only the first image is considered:
$ mvn install
...
[INFO] --- docker-maven-plugin:0.28.0:build (start) # dmp-example ---
[INFO] Building tar: /home/cosmin/workspaces/photon/test/dmp-example/target/docker/example/db-test/latest/tmp/docker-build.tar
[INFO] DOCKER> [example/db-test:latest] "example-db-test": Created docker-build.tar in 28 milliseconds
[INFO] DOCKER> [example/db-test:latest] "example-db-test": Built image sha256:d8233
[INFO]
[INFO] --- docker-maven-plugin:0.28.0:start (start) # dmp-example ---
[INFO] DOCKER> [example/db-test:latest] "example-db-test": Start container 47883d799641
[INFO]
[INFO] --- docker-maven-plugin:0.28.0:stop (stop) # dmp-example ---
[INFO] DOCKER> [example/db-test:latest] "example-db-test": Stop and removed container 47883d799641 after 0 ms
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) # dmp-example ---
[INFO] Installing /home/cosmin/workspaces/photon/test/dmp-example/target/dmp-example-0.0.1-SNAPSHOT.jar to /home/cosmin/.m2/repository/com/example/dmp-example/0.0.1-SNAPSHOT/dmp-example-0.0.1-SNAPSHOT.jar
[INFO] Installing /home/cosmin/workspaces/photon/test/dmp-example/pom.xml to /home/cosmin/.m2/repository/com/example/dmp-example/0.0.1-SNAPSHOT/dmp-example-0.0.1-SNAPSHOT.pom
[INFO]
[INFO] --- docker-maven-plugin:0.28.0:build (build) # dmp-example ---
[INFO] Building tar: /home/cosmin/workspaces/photon/test/dmp-example/target/docker/example/db-test/latest/tmp/docker-build.tar
[INFO] DOCKER> [example/db-test:latest] "example-packaged": Created docker-build.tar in 6 milliseconds
[INFO] DOCKER> [example/db-test:latest] "example-packaged": Built image sha256:d8233
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
...
The second image is not created, and the ID of the first one is also used when the second one should be built:
$ docker images "example/*"
REPOSITORY TAG IMAGE ID CREATED SIZE
example/db-test latest d8233ab899d4 7 days ago 1.2MB
Would there be another way of doing it?
This is my 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>
<groupId>com.example</groupId>
<artifactId>dmp-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.28.0</version>
<configuration>
<images>
<image>
<alias>example-db-test</alias>
<name>example/db-test:latest</name>
<build>
<from>busybox</from>
</build>
</image>
<image>
<alias>example-packaged</alias>
<name>example/packaged:latest</name>
<build>
<from>alpine</from>
</build>
</image>
</images>
</configuration>
<executions>
<execution>
<id>start</id>
<phase>pre-integration-test</phase>
<configuration>
<images>
<image>
<alias>example-db-test</alias>
</image>
</images>
</configuration>
<goals>
<goal>build</goal>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop</id>
<phase>post-integration-test</phase>
<configuration>
<images>
<image>
<alias>example-db-test</alias>
</image>
</images>
</configuration>
<goals>
<goal>stop</goal>
</goals>
</execution>
<execution>
<id>build</id>
<phase>package</phase>
<configuration>
<images>
<image>
<alias>example-packaged</alias>
</image>
</images>
</configuration>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
For keeping the example simple, I used busybox and alpine as the base images, but that has no relevance.

Try moving the full configuration for the image you want into the execution, instead of using global plugin config.
<execution>
<id>build</id>
<phase>package</phase>
<configuration>
<images>
<image>
<alias>example-packaged</alias>
<name>example/packaged:latest</name>
<build>
<from>alpine</from>
</build>
</image>
</images>
</configuration>
<goals>
<goal>build</goal>
</goals>
</execution>

Related

Jenkins Artifactory Plugin - Maven wrapper not publishing custom artifacts which have been installed

When building my Maven project, I expect the Jenkins Artifactory Plugin to deploy/publish artifacts which I successfully maven-install (ie, copied from the /target folder to my local maven repo).
The Jenkins-Artifactory plugin binds to the install phase of the Maven lifecycle. It is publishing the pom files for the modules of my project, but not the build files I need (which I have installed).
UPDATE: JFrog have confirmed this is a bug with the plugin: https://github.com/jfrog/jenkins-artifactory-plugin/issues/665 - I will post an update to this question once it is resolved.
PLEASE NOTE: A normal pom which simply creates a jar works as expected. This is specifically to do with publishing a custom artifact type having used the maven-install-plugin.
Some details:
The build process creates .bar files
These .bar files are added to /target using the maven-install-plugin execution shown below
The .pom files are being uploaded to Artifactory
The .bar files are not being uploaded to Artifactory
<execution>
<id>custom-install</id>
<phase>install</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<generatePom>true</generatePom>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<file>${project.build.directory}/${project.build.finalName}.bar</file>
<packaging>bar</packaging>
</configuration>
</execution>
Logs:
16:50:39 [main] INFO org.apache.maven.cli.event.ExecutionEventLogger - --- maven-install-plugin:2.5.2:install-file (custom-install) # MyMavenModule ---
16:50:39 [main] INFO org.codehaus.plexus.PlexusContainer - Installing /opt/jenkins/workspace/my-project/build/develop/2/MyMavenModule/target/MyMavenModule-1.0.0.bar to /home/jenkins-user/.m2/repository/my/project/group/MyMavenModule/1.0.0/MyMavenModule-1.0.0.bar
16:50:40 [pool-1-thread-1] INFO org.jfrog.build.extractor.maven.ArtifactoryManagerBuilder - [pool-1-thread-1] Deploying artifact: https://artifactory.mycompany.com/artifactory/libs-local/my/project/group/MyMavenModule/1.0.0/MyMavenModule-1.0.0.pom
Versions:
Jenkins: 2.332.1
Artifactory: 6.16.0 Pro
Jenkins Artifactory Plugin: 3.16.1
Maven: 3.8.5
Maven install plugin: 3.0.0-M1
Pipeline / Plugin configuration
rtMavenResolver (
id: "resolver-${config.serverKey}",
serverId: config.serverKey,
releaseRepo: config.repos.resolve.release,
snapshotRepo: config.repos.resolve.snapshot
)
rtMavenDeployer (
id: "deployer-${config.serverKey}",
serverId: config.serverKey,
releaseRepo: config.repos.deploy.release,
snapshotRepo: config.repos.deploy.snapshot,
deployArtifacts : config.deployArtifacts,
includePatterns: config.repos.deploy.includePatterns,
excludePatterns: config.repos.deploy.excludePatterns,
properties: config.repos.deploy.properties,
)
rtBuildInfo (
captureEnv: true,
buildName: config.buildName,
buildNumber: config.buildNumber
)
def mvn(goals) {
rtMavenRun (
tool: config.tools.maven,
pom: 'pom.xml',
goals: "-U -e -B -V -Dstyle.color=always $goals",
opts: '-Dmaven.test.failure.ignore=false -Djansi.force=true',
deployerId: "deployer-${config.artifacts.serverKey}",
resolverId: "resolver-${config.artifacts.serverKey}",
buildName: config.artifacts.buildName,
buildNumber: config.artifacts.buildNumber
)
}
<<<snip>>>
mvn 'install -Dmaven.test.skip=true -DskipTests'
Reproducible Example:
pom.xml
<?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>
<url>http://maven.apache.org</url>
<groupId>com.example</groupId>
<artifactId>MyModule</artifactId>
<packaging>pom</packaging>
<version>1.0.0-SNAPSHOT</version>
<properties>
<exec-maven-plugin-version>1.6.0</exec-maven-plugin-version>
<maven-install-plugin-version>3.0.0-M1</maven-install-plugin-version>
</properties>
<build>
<plugins>
<!-- Note: See 'http://maven.apache.org/plugins/maven-install-plugin/install-file-mojo.html' for details -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>${maven-install-plugin-version}</version>
<executions>
<execution>
<id>default-install</id>
<phase>install</phase>
<goals>
<goal>install</goal>
</goals>
<configuration>
<skip>true</skip>
</configuration>
</execution>
<execution>
<id>custom-install</id>
<phase>install</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<generatePom>true</generatePom>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<file>${project.build.directory}/${project.build.finalName}.bar</file>
<packaging>bar</packaging>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>Windows</id>
<activation>
<os>
<family>Windows</family>
</os>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>${exec-maven-plugin-version}</version>
<executions>
<execution>
<id>execution.compile.windows.buildBAR</id>
<phase>compile</phase>
<configuration>
<workingDirectory>${project.build.directory}</workingDirectory>
<executable>buildBAR.cmd</executable>
<arguments>
<argument>${iib.toolkit.path.windows}</argument>
<argument>${project.artifactId}</argument>
<argument>${project.build.finalName}</argument>
</arguments>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>unix</id>
<activation>
<os>
<family>unix</family>
</os>
</activation>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>${exec-maven-plugin-version}</version>
<executions>
<execution>
<id>execution.compile.unix.buildBAR</id>
<phase>compile</phase>
<configuration>
<workingDirectory>${project.build.directory}</workingDirectory>
<executable>sh</executable>
<arguments>
<argument>buildBAR.sh</argument>
<argument>${project.artifactId}</argument>
<argument>${project.build.finalName}</argument>
</arguments>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
</profiles>
</project>
buildBAR.cmd
SET moduleName=%1
SET barfileName=%2
echo %moduleName% > %barfileName%.bar
buildBAR.sh
#!/bin/sh
moduleName=$1
barfileName=$2
echo $moduleName > $barfileName.bar
mvn clean install yeilds:
PS C:\dev\barbuilder> mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------< com.example:MyModule >------------------------
[INFO] Building MyModule 1.0.0-SNAPSHOT
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) # MyModule ---
[INFO] Deleting C:\dev\barbuilder\target
[INFO]
[INFO] --- exec-maven-plugin:1.6.0:exec (execution.compile.windows.buildBAR) # MyModule ---
C:\dev\barbuilder\target>SET moduleName=MyModule
C:\dev\barbuilder\target>SET barfileName=MyModule-1.0.0-SNAPSHOT
C:\dev\barbuilder\target>echo MyModule 1>MyModule-1.0.0-SNAPSHOT.bar
[INFO]
[INFO] --- maven-install-plugin:3.0.0-M1:install (default-install) # MyModule ---
[INFO] Skipping artifact installation
[INFO]
[INFO] --- maven-install-plugin:3.0.0-M1:install-file (custom-install) # MyModule ---
[INFO] Installing C:\dev\barbuilder\target\MyModule-1.0.0-SNAPSHOT.bar to C:\Users\denham.coote\AppData\Local\.m2\repository\com\example\MyModule\1.0.0-SNAPSHOT\MyModule-1.0.0-SNAPSHOT.bar
[INFO] Installing C:\Users\DENHAM~1.COO\AppData\Local\Temp\mvninstall1034342466988970935.pom to C:\Users\denham.coote\AppData\Local\.m2\repository\com\example\MyModule\1.0.0-SNAPSHOT\MyModule-1.0.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.217 s
[INFO] Finished at: 2022-04-04T12:29:59+01:00
[INFO] ------------------------------------------------------------------------
PS C:\dev\barbuilder>

Error in pom.xml: Element docker is not allowed here

Following this question and these instructions, I get the error in pom: Element docker is not allowed here.
EDIT: The complete pom
Any thoughts about what I am missing?
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>build-image</goal>
</goals>
</execution>
</executions>
<configuration>
<image>
<name>docker.example.com/library/${project.artifactId}</name>
<publish>true</publish>
</image>
<docker>
<publishRegistry>
<username>user</username>
<password>secret</password>
<url>https://docker.example.com/v1/</url>
<email>user#example.com</email>
</publishRegistry>
</docker>
</configuration>
</plugin>
I check the documentation that you mention and this option is available in version > 2.4.x of Spring that is the reason for your project's failure. If you update the parent-pom to > 2.4.x this will works.
I run this command mvn spring-boot:build-image
[INFO] [creator] *** Images (4a75f3011ab0):
[INFO] [creator] docker.example.com/library/coding-challenge:latest
[INFO]
[INFO] Successfully built image 'docker.example.com/library/coding-challenge:latest'
[INFO]
[INFO] > Pushing image 'docker.example.com/library/coding-challenge:latest' 100%

Getting skip non existing resourceDirectory issue while using properties maven plugin iterator plugin and resources plugin

I am trying to use three maven plugin at a time but getting error while executing. My goal is to read each environment properties that will place under src folder like application-sit.properties .. etc.
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>
<groupId>com.test.config</groupId>
<artifactId>config-poc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>config-poc</name>
<build>
<directory>${project.basedir}/target</directory>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/config/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>com.soebes.maven.plugins</groupId>
<artifactId>iterator-maven-plugin</artifactId>
<version>0.5.1</version>
<executions>
<execution>
<id>moving property file</id>
<phase>initialize</phase>
<goals>
<goal>iterator</goal>
</goals>
<configuration>
<items>
<item>sit</item>
<item>uat</item>
</items>
<pluginExecutors>
<pluginExecutor>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
</plugin>
<goal>read-project-properties</goal>
<configuration>
<files>
<file>${project.basedir}/config/resources/environments/common.properties</file>
<file>${project.basedir}/config/resources/environments/#item#.properties</file>
</files>
</configuration>
</pluginExecutor>
<pluginExecutor>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
</plugin>
<goal>copy-resources</goal>
<configuration>
<outputDirectory>${project.build.outputDirectory}/config/${item}/</outputDirectory>
<resources>
<resource>
<directory>${project.build.outputDirectory}</directory>
<filtering>false</filtering>
<include>*.properties</include>
</resource>
</resources>
</configuration>
</pluginExecutor>
</pluginExecutors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Getting below error while executing looks like read property plugin is not generating application.properties inside target folder. Below is the snapshot of my project.
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------< com.test.config:config-poc >---------------------
[INFO] Building config-poc 0.0.1-SNAPSHOT
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) # config-poc ---
[INFO]
[INFO] --- iterator-maven-plugin:0.5.1:iterator (moving property file) # config-poc ---
[INFO] ------ (sit) org.codehaus.mojo:properties-maven-plugin:1.0.0:read-project-properties
[INFO] ------ (sit) org.apache.maven.plugins:maven-resources-plugin:2.7:copy-resources
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\eclipse\F\eclipse_workspace3\config-poc\target\classes
[INFO] ------ (uat) org.codehaus.mojo:properties-maven-plugin:1.0.0:read-project-properties
[INFO] ------ (uat) org.apache.maven.plugins:maven-resources-plugin:2.7:copy-resources
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\eclipse\F\eclipse_workspace3\config-poc\target\classes
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) # config-poc ---
[INFO] Installing C:\eclipse\F\eclipse_workspace3\config-poc\pom.xml to C:\Users\Abhishek\.m2\repository\com\test\config\config-poc\0.0.1-SNAPSHOT\config-poc-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.798 s
[INFO] Finished at: 2020-08-29T16:29:36+05:30
[INFO] ------------------------------------------------------------------------
Below pluginExecutor structure will solve your problem.
<pluginExecutor>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<goal>copy-resources</goal>
<configuration>
<outputDirectory>${project.build.outputDirectory}/${item}/</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/config/resources/templates</directory>
<filtering>false</filtering>
<include>application.properties</include>
</resource>
</resources>
</configuration>

Maven runs profile executions only after the build executions

Given a following maven pom.xml development profile
<profiles>
<profile>
<id>development</id>
<build>
<plugins>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.0.4</version>
<executions>
<execution>
<id>01-liquibase.dropAll</id>
<phase>process-resources</phase>
<configuration>
[...skip...]
</configuration>
<goals>
<goal>dropAll</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
and main section of pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.openjpa</groupId>
<artifactId>openjpa-maven-plugin</artifactId>
<version>2.2.2</version>
<configuration>
<includes>package/pm/entity/**/*.class</includes>
<addDefaultConstructor>true</addDefaultConstructor>
<enforcePropertyRestrictions>true</enforcePropertyRestrictions>
<persistenceXmlFile>${project.parent.basedir}/forms-webapp/src/main/resources/META-INF/persistence.xml</persistenceXmlFile>
</configuration>
<executions>
<execution>
<id>02-enhancer</id>
<phase>process-classes</phase>
<goals>
<goal>enhance</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.openjpa</groupId>
<artifactId>openjpa</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.0.4</version>
<executions>
<execution>
<id>03-liquibase.update</id>
<phase>process-resources</phase>
<configuration>
[...skipped...]
</configuration>
<goals>
<goal>update</goal>
</goals>
</execution>
<!-- for tests -->
<execution>
<id>04-liquibase-test.dropAll</id>
<phase>process-test-resources</phase>
<configuration>
[...skipped...]
</configuration>
<goals>
<goal>dropAll</goal>
</goals>
</execution>
<execution>
<id>05-liquibase-test.update</id>
<phase>process-test-resources</phase>
<configuration>
[...skipped...]
</configuration>
<goals>
<goal>update</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
I expect that executions will run in the following order:
....
01-liquibase.dropAll
03-liquibase.update
....
But in build log I see:
[INFO] --- liquibase-maven-plugin:3.0.4:update (03-liquibase.update) # forms-entity ---
[INFO] ------------------------------------------------------------------------
[INFO] Parsing Liquibase Properties File
[INFO] File: src/main/resources/liquibase.properties
[INFO] 'classpath' in properties file is not being used by this task.
[INFO] ------------------------------------------------------------------------
[INFO] Executing on Database: jdbc:postgresql://localhost/pman_trunk
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO]
[INFO] --- liquibase-maven-plugin:3.0.4:dropAll (01-liquibase.dropAll) # forms-entity ---
[INFO] ------------------------------------------------------------------------
[INFO] Parsing Liquibase Properties File
[INFO] File: src/main/resources/liquibase.properties
[INFO] 'classpath' in properties file is not being used by this task.
[INFO] ------------------------------------------------------------------------
[INFO] Executing on Database: jdbc:postgresql://localhost/pman_trunk
[INFO] ------------------------------------------------------------------------
Is there any way to fix this wrong execution order?
The executions for id 01-liquibase.dropAll and 03-liquibase.update are executing on the same phase.
You could always move the execution with id 01-liquibase.dropAll to an earlier phase such as generate-resources
<id>01-liquibase.dropAll</id>
<phase>process-resources</phase>
See the Default Lifecycle section in Maven Lifecycle Reference.

Maven exec plugin execution conundrum

Sample maven build file:
<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>test</groupId>
<artifactId>test</artifactId>
<version>1.0.0</version>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>my-validation-one</id>
<phase>validate</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>echo</executable>
<arguments>
<argument>"My validation one"</argument>
</arguments>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>my-validation-two</id>
<phase>validate</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>echo</executable>
<arguments>
<argument>"My validation two"</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
</dependencies>
</project>
Sample maven output:
$ mvn install
/cygdrive/c/Program Files/Java/jdk1.5.0_20/bin/java -classpath C:\Program Files\Apache Software Foundation\apache-maven-2.2.1/boot/classworlds-1.1.jar -Dclassworlds.conf=C:\Program Files\Apache Software Foundation\apache-maven-2.2.1/bin/m2.conf -Dmaven.home=C:\Program Files\Apache Software Foundation\apache-maven-2.2.1 org.codehaus.classworlds.Launcher "install"
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Unnamed - test:test:jar:1.0.0
[INFO] task-segment: [install]
[INFO] ------------------------------------------------------------------------
[INFO] [exec:exec {execution: my-validation-one}]
[INFO] "My validation two"
[INFO] [exec:exec {execution: my-validation-two}]
[INFO] "My validation two"
[INFO] [resources:resources {execution: default-resources}]
<snip>
Shouldn't my-validation-one echo "My validation one" to the console? Why does it seem like maven is getting its wires crossed?
Thanks,
Steven
You should put the configuration specific to the execution within the execution block. Then it should work correctly.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>my-validation-two</id>
<phase>validate</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>echo</executable>
<arguments>
<argument>"My validation two"</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
Also, if both the executions are bound to same phase and goal, you can combine then within executions block instead of declaring the plugin twice.

Resources