I am trying to fiddle with maven antrun plugin. I have a parent pom and child pom. I want to maven-install child pom and have some custom properties that are specified in parent pom to be included in the jar manifest. I want to do this with maven-antrun plugin.
This is what I have done:
- I have included ant jar task in parent pom. That ant jar task specifies properties to be added to the manifest.
- I have also added dummy echo tasks in both parent and child poms.
- I am hoping that child pom should inherit parent tasks (both echo and jar). For this I have same execution id
<id>execution-1</id>
in both child and parent pom. Also child pom task havecombine.children="append"
parent-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.abc.xyz</groupId>
<artifactId>parent-pom</artifactId>
<version>2.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>execution-1</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>from parent pom!!!</echo>
<jar destfile="${project.build.directory}/${project.build.finalName}.jar"
basedir="src/main">
<manifest>
<attribute name="Class-Path" value="mahesh" />
<attribute name="Main-Class" value="com.abc.xyz.Application" />
<attribute name="Build-Jdk" value="${java.version}" />
<attribute name="Built-By" value="${user.name}" />
</manifest>
</jar>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
child-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>sample-project</artifactId>
<parent>
<groupId>com.abc.xyz</groupId>
<artifactId>parent-pom</artifactId>
<version>2.0.0-SNAPSHOT</version>
</parent>
<build>
<finalName>sample-project</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>execution-1</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks combine.children="append">
<echo>from child pom!!!</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Output
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ sample-project ---
[INFO] Building jar: D:\Mahesh\workspaces\workspace2\sample-project\target\sample-project.jar
[INFO]
[INFO] --- maven-antrun-plugin:1.3:run (execution-1) @ sample-project ---
[INFO] Executing tasks
[echo] from parent pom!!!
[echo] from child pom!!!
[INFO] Executed tasks
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ sample-project ---
[INFO] Installing D:\Mahesh\workspaces\workspace2\sample-project\target\sample-project.jar to D:\MavenRepo\repository\com\abc\xyz\sample-project\2.0.0-SNAPSHOT\sample-project-2.0.0-SNAPSHOT.jar
[INFO] Installing D:\Mahesh\workspaces\workspace2\sample-project\pom.xml to D:\MavenRepo\repository\com\abc\xyz\sample-project\2.0.0-SNAPSHOT\sample-project-2.0.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
Manifest contents
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: 593932
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_71
Note that the manifest content does not have
Class-Path: mahesh
which is specified in parent pom.
Doubts
- Since
Class-path:mahesh
is not getting added to the manifest, it is clear that jar is not getting generated by antrun task specified in parent pom. Also it can be seen in the first line of the "Output", jar seems to have generated by maven-jar-plugin but not by the antrun-plugin. Inside antrun-plugin I am getting only two echoes. I feel I should get building jar output inside antrun-task start end only. Why its not working? - Is this right way to do this (adding property values specified in parent pom to jar manifest). I feel I shouldn't have jar task in parent pom, but should be in child pom and that jar task should access property specified in parent pom. Is their any standard / more correct / preferrable way to do this?