Inheriting configuration property from parent pom in maven

自古美人都是妖i 提交于 2019-12-24 21:47:38

问题


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?

回答1:


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...?



来源:https://stackoverflow.com/questions/48769394/inheriting-configuration-property-from-parent-pom-in-maven

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!