从maven

时间:2018-02-13 14:44:26

标签: java maven ant maven-antrun-plugin

我正试图摆弄maven antrun插件。 我有一个父母pom和孩子pom。我想maven-install子pom并且有一些在父pom中指定的自定义属性包含在jar清单中。我想用maven-antrun插件做到这一点。

这就是我所做的:

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

    儿童-的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没有被添加到清单中,很明显jar不是由父pom中指定的antrun任务生成的。也可以在“输出”的第一行看到,jar似乎是由maven-jar-plugin生成的,但不是由antrun-plugin生成的。在antrun-plugin里面,我只得到了两个回声。我觉得我应该只在antrun-task start end中构建jar输出。为什么不工作?
    2. 这是否正确(将父pom中指定的属性值添加到jar清单)。我觉得我不应该在父pom中有jar任务,但应该在子pom中,并且jar任务应该访问父pom中指定的属性。是他们的标准/更正确/更优先的方式吗?

1 个答案:

答案 0 :(得分:0)

第一件事是你正在尝试使用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>

这可以通过插件管理在父母中进行配置。除此之外,我不了解您要解决的问题类型......?