您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

是否可以创建一个“uber”jar,其中包含项目类和项目依赖项(作为jar)以及自定义清单文件?

是否可以创建一个“uber”jar,其中包含项目类和项目依赖项(作为jar)以及自定义清单文件?

实际上,我没有检查它到底在maven-shade-plugin做什么(或任何其他插件),因为maven 2具有内置的所有功能来创建megajar或uberjar。你只需要使用带有预定义jar-with-dependencies描述符的maven-assembly-plugin即可。

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

<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

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

但是,同样,此描述符的认行为是解压缩依赖项(例如maven-shade-plugin)。老实说,我不明白为什么会出现这个问题,但是,如果这不是你想要的,那么你可以使用自己的自定义程序集描述符。

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

<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项添加到清单的条目中:

<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。

(可选)取消注释executions元素以在package阶段上绑定程序集插件(并使程序集作为常规构建的一部分生成)。

其他 2022/1/1 18:15:20 有561人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶