0

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:

  1. I have included ant jar task in parent pom. That ant jar task specifies properties to be added to the manifest.
  2. I have also added dummy echo tasks in both parent and child poms.
  3. 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 have combine.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

  1. 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?
  2. 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?
Mahesha999
  • 22,693
  • 29
  • 116
  • 189

1 Answers1

0

The first thing is that you are trying to use maven-antrun-plugin which is simply the wrong path to go...Better start using the maven-jar-plugin to customize the MANIFEST.MF like this:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.0.2</version>
        <configuration>
          <archive>
            <index>true</index>
            <manifest>
              <addClasspath>true</addClasspath>
            </manifest>
            <manifestEntries>
              <mode>development</mode>
              <url>${project.url}</url>
              <key>value</key>
            </manifestEntries>
          </archive>
        </configuration>
        ...
      </plugin>
    </plugins>
  </build>
  ...
</project>

This can be configured in parent via pluginManagement..Apart from that I don't understand what kind of problem you are trying to solve...?

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • Yep, that sounds correct. But can you please tell me why `jar` task of `maven-antrun-plugin` specified in parent pom is not executed? And why instead of that `jar` task of `maven-jar-plugin` is executed? Just curious. – Mahesha999 Feb 13 '18 at 18:57
  • maven-jar-plugin is by default bound to the lifecyle which means in other words it will be executed during things like `mvn clean package` whereas maven-antrun-plugin is not bound to the life cycle and so will not being executed. – khmarbaise Feb 14 '18 at 07:38
  • Shouldnt specifying `package` inside `maven-antrun-plugin` bind `maven-antrun-plugin` to lifecycle? Tried disabling `maven-jar-plugin` as told [here](https://stackoverflow.com/a/4853816), but didnt work. Old code (in question) started working when I did it in new workspace. Now guessing why? ([Download workspace](https://docs.zoho.com/file/5isgl707322372ce04e2ba12a1e19c494416a)). I was using antrun instead of maven-jar, because I want to do something which I dont know if I can do with maven-jar, as discussed in this [new question](https://stackoverflow.com/questions/48786850). – Mahesha999 Feb 14 '18 at 12:19