是否有可能创建一个“超级"?jar 包含项目类和项目依赖项作为带有自定义清单文件的 jars? [英] Is it possible to create an "uber" jar containing the project classes and the project dependencies as jars with a custom manifest file?

查看:26
本文介绍了是否有可能创建一个“超级"?jar 包含项目类和项目依赖项作为带有自定义清单文件的 jars?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个可执行 jar(使用 maven),其中包含项目类及其依赖项,其中包含一个清单文件,其中包含主类的条目和指向打包在根目录中的依赖项的类路径条目罐子的;类似这样的:

清单文件:

<前>.....主类:com.acme.MainClass类路径:dependecy1.jardependecy2.jar.....

罐子:

<前>罐根|-- ....|-- com/acme/../*.class|--dependecy1.jar`--dependecy2.jar

我使用 maven-jar-plugin 创建清单文件,使用 maven-shade-plugin 创建uber"jar,但依赖项已解包并作为类添加到我的 jar 中.

解决方案

实际上,我并没有像 maven 2 那样检查 maven-shade-plugin 正在做什么(或任何其他插件)内置了所有东西来创建一个 megajar 或 uberjar.您只需要使用带有预定义 jar-with-dependencies 描述符的 maven-assembly-plugin.

只需将此代码段添加到您的 pom.xml 即可自定义清单:

<groupId>org.apache.maven.plugins</groupId><artifactId>maven-assembly-plugin</artifactId><配置><存档><清单><mainClass>my.package.to.my.MainClass</mainClass></清单></归档></配置></插件>

以下命令将生成您的 uberjar:

mvn assembly:assembly -DdescriptorId=jar-with-dependencies

但是,同样,这个描述符的默认行为是解包依赖项(如 maven-shade-plugin).老实说,我不明白为什么这是一个问题,但是,如果这真的不是您想要的,您可以使用您自己的自定义程序集描述符.

为此,首先,创建您的程序集描述符,例如src/assembly/uberjar.xml,内容如下:

<程序集><id>uberjar</id><格式><format>jar</format></格式><includeBaseDirectory>false</includeBaseDirectory><依赖集><依赖集><unpack>false</unpack><scope>运行时</scope><useProjectArtifact>false</useProjectArtifact></dependencySet></dependencySets><文件集><文件集><目录>${project.build.outputDirectory}</directory><outputDirectory>/</outputDirectory></fileSet></fileSets></组装>

然后,配置 maven-assembly-plugin 以使用此描述符并将依赖项添加到清单的 Class-Path 条目:

<groupId>org.apache.maven.plugins</groupId><artifactId>maven-assembly-plugin</artifactId><配置><描述符><descriptor>src/assembly/uberjar.xml</descriptor></描述符><存档><清单><mainClass>my.package.to.my.MainClass</mainClass><addClasspath>true</addClasspath></清单></归档></配置><!--<执行><执行><phase>包</phase><目标><目标>单</目标></目标></执行></执行>--></插件>

最后运行 mvn assembly:assembly 来生成你的 uberjar.

或者,取消注释 executions 元素以在 package 阶段绑定程序集插件(并将程序集作为正常构建的一部分生成).

I'm trying to create a executable jar(using maven) that contains the project classes and it's dependencies with a manifest file that has the entry for the main class and the class path entry that points to the dependencies packed in the root of the jar;something like this :

Manifest File:

.....
Main-Class : com.acme.MainClass
Class-Path : dependecy1.jar dependecy2.jar
.....

Jar:

jar-root
|-- ....
|-- com/acme/../*.class
|-- dependecy1.jar
`-- dependecy2.jar

I'm using the maven-jar-plugin to create the manifest file and the maven-shade-plugin to create the "uber" jar but the dependencies are unpacked and added as classes to my jar.

解决方案

Actually, I didn't check what the maven-shade-plugin is doing exactly (or any other plugin) as maven 2 has everything built-in to create a megajar or uberjar. You just have to use the maven-assembly-plugin with the predefined jar-with-dependencies descriptor.

Just add this snippet to your pom.xml to customize the manifest:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <mainClass>my.package.to.my.MainClass</mainClass>
      </manifest>
    </archive>
  </configuration>
</plugin>

And the following command will generate your uberjar:

mvn assembly:assembly -DdescriptorId=jar-with-dependencies

But, again, the default behavior of this descriptor is to unpack dependencies (like the maven-shade-plugin). To be honest, I don't get why this is a problem but, if this is really not what you want, you can use your own custom assembly descriptor.

To do so, first, create your assembly descriptor, let's say src/assembly/uberjar.xml, with the following content:

<assembly>
  <id>uberjar</id>
  <formats>
    <format>jar</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <unpack>false</unpack>
      <scope>runtime</scope>
      <useProjectArtifact>false</useProjectArtifact>
    </dependencySet>
  </dependencySets>
  <fileSets>
    <fileSet>
      <directory>${project.build.outputDirectory}</directory>
      <outputDirectory>/</outputDirectory>
    </fileSet>
  </fileSets>
</assembly>

Then, configure the maven-assembly-plugin to use this descriptor and to add the dependencies to the Class-Path entry of the manifest:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <descriptors> 
      <descriptor>src/assembly/uberjar.xml</descriptor>
    </descriptors>
    <archive>
      <manifest>
        <mainClass>my.package.to.my.MainClass</mainClass>
        <addClasspath>true</addClasspath>
      </manifest>
    </archive>
  </configuration>
  <!--
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
  -->
</plugin>

Finally run mvn assembly:assembly to produce your uberjar.

Optionally, uncomment the executions element to bind the assembly plugin on the package phase (and have the assembly produced as part of the normal build).

这篇关于是否有可能创建一个“超级"?jar 包含项目类和项目依赖项作为带有自定义清单文件的 jars?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆