从Maven中的父型POM继承配置属性



我正在尝试使用Maven Antrun插件进行摆弄。我有一个父母的pom和孩子pom。我想设置pom pom,并具有一些自定义属性,这些属性已在jar清单中包含在父pom中。我想使用Maven-Antrun插件来执行此操作。

这就是我所做的:

  1. 我已将蚂蚁罐任务包含在父pom中。该蚂蚁罐任务指定要添加到清单中的属性。
  2. 我还添加了父母和儿童poms中的虚拟回声任务。
  3. 我希望孩子POM应该继承父任务(Echo和Jar(。为此,我在孩子和父型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:Maheshworkspacesworkspace2sample-projecttargetsample-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:Maheshworkspacesworkspace2sample-projecttargetsample-project.jar to D:MavenReporepositorycomabcxyzsample-project2.0.0-SNAPSHOTsample-project-2.0.0-SNAPSHOT.jar
[INFO] Installing D:Maheshworkspacesworkspace2sample-projectpom.xml to D:MavenReporepositorycomabcxyzsample-project2.0.0-SNAPSHOTsample-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没有被添加到清单上,因此很明显,JAR不会由Parent POM中指定的ANTRUN任务生成。另外,它可以在"输出"的第一行中看到,JAR似乎是由Maven-Jar-Plugin生成的,而不是由Antrun-Plugin生成的。在Antrun-Plugin中,我只有两个回声。我觉得我应该仅在Antrun任务开始端启动端。为什么不起作用?
  2. 是这样做的正确方法(添加父pom中指定的属性值以清楚地表明(。我觉得我不应该在父型POM中执行JAR任务,而应该在Child Pom中,并且该JAR任务应访问Parent Pom中指定的属性。他们的任何标准/更正确/更偏爱这样做的方法吗?

首先是您尝试使用Maven-Antrun-Plugin,这只是错误的路径...更好地开始使用Maven-Jar-Plugin来自定义清单.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>

可以通过插件在父级中配置。

最新更新