带有多个jar版本的Maven pom.xml

时间:2015-10-28 12:08:30

标签: maven maven-plugin maven-assembly-plugin maven-jar-plugin

这是我的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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <url>http://maven.apache.org</url>

    <name>SomeProject</name>

    <groupId>com.test.te</groupId>
    <artifactId>testjar</artifactId>
    <version>1.1.6</version>
    <packaging>jar</packaging>

    <dependencies>
    ....
    ....
    </dependencies>
    <build>

        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.conf</include>
                    <include>**/*.java</include>
                </includes>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
                <executions>
                    <execution>
                        <id>drive</id>
                        <goals><goal>jar</goal></goals>
                            <phase>install</phase>
                            <configuration>
                                <finalName>driver</finalName>
                                <includes>
                                    <include>com/test/te/p1/*.java<include>
                                    <include>com/test/te/p1/*.class</include>
                                </includes>
                            </configuration>
                        </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

以下是我执行此操作的方法

mvn clean install

这是构建输出的最后几行。

[INFO] [jar:jar {execution: default-jar}]
[INFO] Building jar: /home/mycp/workspace/SomeProject/target/testjar-1.1.6.jar
[INFO] [install:install {execution: default-install}]
[INFO] Installing /home/mycp/workspace/SomeProject/target/testjar-1.1.6.jar to /home/mycp/.m2/repository/com/test/te/testjar/1.1.6/testjar-1.1.6.jar
[INFO] [jar:jar {execution: driver}]
[INFO] Building jar: /home/mycp/workspace/TestProject/target/driver.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11 seconds
[INFO] Finished at: Wed Oct 28 17:27:10 IST 2015
[INFO] Final Memory: 129M/888M
[INFO] ------------------------------------------------------------------------
  • 为什么第二个jar(driver.jar)没有版本化(driver-1.1.6.jar)?
  • 为什么第二个jar没有安装在本地仓库?

怎么做?

我的要求是创建两个罐子 testjar - $ {project.version} .jar driver - $ {project.version} .jar 并将这两个装在两个不同的工件中

/home/mycp/.m2/repossitory/com/test/te/ testjar / $ {项目版本} / testjar - $ {} project.version的.jar

/home/mycp/.m2/repossitory/com/test/te/ 驱动 / $ {项目版本} / 司机 - $ {} project.version的.jar

1 个答案:

答案 0 :(得分:3)

  

为什么第二个jar(driver.jar)没有版本化(driver-1.1.6.jar)?

这是因为您明确告诉maven-jar-plugin其名称应为driver <finalName>driver</finalName>。如果要添加版本,则需要进行以下配置:<finalName>driver-${project.version}</finalName>

  

为什么第二个jar没有安装在本地仓库?

这是因为您指定了<phase>install</phase>的阶段,因此它将在执行default-install之后执行,这是将artefact安装到本地存储库的执行。相反,您应该将该执行绑定到package阶段。

这是maven-jar-plugin应该具备的配置。需要一个分类器来创建另一个jar。

<execution>
  <id>drive</id>
  <goals>
    <goal>jar</goal>
  </goals>
  <phase>package</phase>
  <configuration>
    <finalName>driver-${project.version}</finalName>
    <classifier>classifier</classifier>
    <includes>
      <include>com/test/te/p1/*.java</include>
      <include>com/test/te/p1/*.class</include>
    </includes>
  </configuration>
</execution>

作为旁注,我注意到您还在POM中配置了<resources>元素,包括Java和conf文件。 你不应该那样。更具体地说:所有资源都应放在src/main/resources下,所有Java文件放在src/main/java下。然后就不需要那种配置了。

根据评论,如果您无法添加分类符,则必须删除maven-jar-plugin并使用maven-assembly-plugin代替。这是一个简单的汇编描述符,可以帮助您入门。将此文件放在src/main/assembly/assembly.xml

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>driver</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <useProjectArtifact>true</useProjectArtifact>
            <unpackOptions>
                <includes>
                    <include>com/test/te/p1/*.class</include>
                </includes>
            </unpackOptions>
            <unpack>true</unpack>
        </dependencySet>
    </dependencySets>
</assembly>

在POM中使用以下配置:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.5.5</version>
    <configuration>
        <descriptors>
            <descriptor>src/main/assembly/assembly.xml</descriptor> <!-- point to the location of the assembly.xml file in your project structure -->
        </descriptors>
        <appendAssemblyId>false</appendAssemblyId>
        <finalName>driver-${project.version}</finalName>
    </configuration>
    <executions>
        <execution>
            <phase>prepare-package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>