【问题标题】:Inheriting configuration property from parent pom in maven从maven中的父pom继承配置属性
【发布时间】:2018-02-13 14:44:26
【问题描述】:

我正在尝试使用 maven antrun 插件。 我有一个父 pom 和一个子 pom。我想 maven-install child pom 并在父 pom 中指定一些自定义属性以包含在 jar 清单中。我想用 maven-antrun 插件来做到这一点。

这就是我所做的:

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

注意manifest内容没有

Class-Path: mahesh

在父 pom 中指定。

怀疑

  1. 由于Class-path:mahesh 没有被添加到清单中,很明显jar 不是由父pom 中指定的antrun 任务生成的。在“输出”的第一行也可以看到,jar 似乎是由 maven-jar-plugin 生成的,而不是由 antrun-plugin 生成的。在 antrun-plugin 内部,我只得到两个回声。我觉得我应该只在 antrun-task start end 内构建 jar 输出。为什么它不起作用?
  2. 这是正确的方法吗(将父 pom 中指定的属性值添加到 jar 清单)。我觉得我不应该在父 pom 中有 jar 任务,而应该在子 pom 中,并且该 jar 任务应该访问父 pom 中指定的属性。他们有什么标准/更正确/更可取的方法吗?

【问题讨论】:

    标签: java maven ant maven-antrun-plugin


    【解决方案1】:

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

    <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在父级中配置..除此之外我不明白您要解决什么样的问题...?

    【讨论】:

    • 是的,这听起来是正确的。但是你能告诉我为什么没有执行父pom中指定的maven-antrun-pluginjar任务吗?为什么不执行maven-jar-pluginjar 任务?只是好奇。
    • maven-jar-plugin 默认绑定到生命周期,这意味着它将在mvn clean package 等期间执行,而 maven-antrun-plugin 未绑定到生命周期等不会被执行。
    • 不应该在maven-antrun-plugin 中指定&lt;phase&gt;package&lt;/phase&gt; 绑定maven-antrun-plugin 到生命周期吗?尝试按照here 的说明禁用maven-jar-plugin,但没有奏效。当我在新工作区中执行旧代码(有问题)时,它开始工作。现在猜猜为什么? (Download workspace)。我使用的是 antrun 而不是 maven-jar,因为我想做一些我不知道是否可以使用 maven-jar 的事情,正如 new question 中所讨论的那样。
    猜你喜欢
    • 2012-12-22
    • 1970-01-01
    • 2011-11-26
    • 2012-03-17
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    • 2012-03-31
    • 2017-02-22
    相关资源
    最近更新 更多