【问题标题】:How to tell Maven to re-package all dependencies into a single jar file如何告诉 Maven 将所有依赖项重新打包到单个 jar 文件中
【发布时间】:2019-07-06 15:30:21
【问题描述】:

给定一个包含依赖列表的 pom.xml 文件,是否有一个 Maven 命令可以将所有这些 jar 打包到一个大 jar 中? 我不想在构建中包含我的 .class 文件 - 我只想在构建中包含库 .class/jar 文件 - 有没有办法做到这一点?

我看到了这个插件:

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/Crunchify/lib</outputDirectory>
                <overWriteReleases>false</overWriteReleases>
                <overWriteSnapshots>false</overWriteSnapshots>
                <overWriteIfNewer>true</overWriteIfNewer>
            </configuration>
        </execution>
    </executions>
</plugin>
  1. 我无法弄清楚它将依赖项复制到哪里 - 即使我硬编码了一个路径而不是 ${project.build.directory}/Crunchify/lib,也没有任何内容写入该位置,
  2. 我假设它是在复制 jar,而不是将多个 jar 的内容放入一个大 jar 文件中。

我也看到了这个通用插件:

http://maven.apache.org/plugins/maven-assembly-plugin/usage.html

     <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.1.1</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id> <!-- this is used for inheritance merges -->
            <phase>package</phase> <!-- bind to the packaging phase -->
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

但是当我在 pom.xml 中使用 maven-assembly-plugin 运行 mvn clean package 时,我没有看到任何新的 jar 文件。

【问题讨论】:

  • maven-shade-plugin (maven.apache.org/plugins/maven-shade-plugin) 是要走的路。它可以处理组合 jar 时出现的所有奇怪情况(例如,合并元数据)。
  • @Rob 谢谢,你能展示一个只打包依赖项而不打包用户/项目类的例子吗?
  • 为了帮助理解一个好的解决方案可能是什么,您能否解释一下为什么您不希望您的类出现在生成的 JAR 中?
  • ${project.build.directory} 通常是target 文件夹...

标签: java maven jar pom.xml


【解决方案1】:

机智jar-with-dependenciessingle~package,您被困在程序集的“GOTCHAS”之一中(打包之前没有要组装的东西(未完成))。

但是结合两个插件,这将起作用:

/pom.xml

<project>
   ...
    <!-- many many dependencies -->
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/assembly/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.1.1</version>
                <executions>
                    <execution>
                        <id>copy-deps</id>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

/src/assembly/assembly.xml

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
  <id>big-big-jar</id>
  <formats>
    <format>jar</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <fileSets>
    <fileSet>
      <directory>${project.build.directory}/dependency</directory>
      <outputDirectory />
      <includes>
        <!-- attention: maybe you need more ... zip, tz.gz.bz. -->
        <include>*.jar</include>
      </includes>
    </fileSet>
  </fileSets>
</assembly>

命令

mvn clean package
...
BUILD SUCCESS

...会给你一个:

/target/${build.finalName}-big-big-jar.jar

..file,包含所有临时依赖项没有您的 .jar/类。

【讨论】:

    猜你喜欢
    • 2011-10-01
    • 2011-01-13
    • 1970-01-01
    • 2012-11-01
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 2017-07-03
    相关资源
    最近更新 更多