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

问题描述

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

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 :

清单文件:


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

Jar:


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

我正在使用maven-jar-plugin来创建清单文件和maven-shade-plugin来创建uberjar,但依赖项被解压缩并作为类添加到我的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.

推荐答案

实际上,我没有检查 maven-shade-plugin 正在完成(或任何其他插件),因为maven 2已经构建了一切 - 创造一个megajar或uberjar。你只需要使用maven-assembly-plugin和预定义的 jar-with-dependencies 描述符。

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将此代码段添加到 pom.xml 以自定义清单:

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>

以下命令将生成你的uberjar:

And the following command will generate your uberjar:

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

但是,同样,此描述符的默认行为是解包依赖项(如maven-shade-plugin)。说实话,我不明白为什么这是一个问题但是,如果这不是你想要的,你可以使用自己的自定义汇编描述符。

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这样做,首先,创建你的汇编描述符,假设是 src / assembly / uberjar.xml ,其中包含以下内容:

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>

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

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>

最后运行 mvn assembly:assembly 来生产你的uberjar。

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包含项目类和项目依赖项作为jar与自定义清单文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 01:31