从Maven的父Pom继承配置属性 [英] Inheriting configuration property from parent pom in maven

查看:125
本文介绍了从Maven的父Pom继承配置属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试摆弄maven antrun插件. 我有一个父母pom和一个孩子pom.我想maven安装子pom,并具有一些要在jar清单中包含的父pom中指定的自定义属性.我想使用maven-antrun插件来做到这一点.

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.

这就是我所做的:

  1. 我在父pom中包含了蚂蚁罐任务.该ant jar任务指定了要添加到清单的属性.
  2. 我还在父子Pom中添加了虚拟回声任务.
  3. 我希望子pom应该继承父任务(echo和jar).为此,我在子pom和父pom中都具有相同的执行ID <id>execution-1</id>.子pom任务也有combine.children="append"
  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>

输出

[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-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: 593932
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_71

请注意,清单内容没有

Class-Path: mahesh

在父pom中指定.

疑问

  1. 由于未将Class-path:mahesh添加到清单中,因此很显然,父pom中指定的antrun任务不会生成jar.也可以在输出"的第一行中看到,jar似乎是由maven-jar-plugin生成的,而不是由antrun-plugin生成的.在antrun-plugin内部,我仅收到两个回声.我觉得我应该只在antrun-task start end内部获得构建jar输出.为什么它不起作用?
  2. 是这样做的正确方法(将父pom中指定的属性值添加到jar清单中).我觉得我不应该在父pom中包含jar任务,而应该在子pom中,并且jar任务应该访问父pom中指定的属性.他们是否有任何标准/更正确/更可取的方式来做到这一点?
  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?

推荐答案

第一件事是您正在尝试使用maven-antrun-plugin,这只是走错了路...更好地开始使用maven-jar -plugin来自定义MANIFEST.MF,如下所示:

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>

这可以通过pluginManagement在父级中配置.除此之外,我不知道您要解决哪种问题...?

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

这篇关于从Maven的父Pom继承配置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆