在maven项目

时间:2015-08-08 19:04:54

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

是否有可能告诉Maven或其常见插件之一将最终程序集中的一个依赖JAR打包为JAR文件

ie 如果我依赖org.some-group:some-artifact:1.2.3,Maven插件会将整个some-artifact-1.2.3.jar填入我的最终JAR文件中?

1 个答案:

答案 0 :(得分:0)

如果我清楚地理解了您的问题,您需要在生成的jar中添加一个特定的jar,在这种情况下,您可以使用classifier使用maven-assembly-plugin

<强>的pom.xml

  <dependencies>
    <dependency>
      <groupId>com.example</groupId>
      <artifactId>jar-with-dependencies-module</artifactId>
      <version>1.0.0-SNAPSHOT</version>
      <classifier>jar-with-dependencies</classifier>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.3</version>
        <configuration>
          <appendAssemblyId>false</appendAssemblyId>
          <descriptors>
            <descriptor>src/main/assembly/assembly.xml</descriptor>
          </descriptors>
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

<强> assembly.xml

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">

  <id>final-assembly</id>
  <formats>
    <format>jar</format>
  </formats>

  <dependencySets>
    <!-- Include the jar-with-dependencies -->
    <dependencySet>
      <includes>
        <include>org.some-group:some-artifact:1.2.3</include>
      </includes>
      <useProjectArtifact>false</useProjectArtifact>
      <!-- Don't use transitive dependencies since they are already included in the jar -->
      <useTransitiveDependencies>false</useTransitiveDependencies>
    </dependencySet>t>
  </dependencySets>
</assembly>

以上配置可能会让您知道从哪里开始以及如何将特定罐子包含在最终的罐子中