【问题标题】:How to package a Maven project in zip file with jar files inside如何将 Maven 项目打包为 zip 文件,其中包含 jar 文件
【发布时间】:2017-12-22 16:24:17
【问题描述】:

小介绍

我是 maven 的新手,我面临着这个问题。 我的项目结构如下:

|--root |--bin |-- some bash scripts |--config |-- some .properties files |--j2ee-apps |-- some web related files |--META-INF |-- MANIFEST.MF |--src |-- main |-- java |-- test |-- java

目标

执行mvn package 后,我必须得到具有以下结构的 .zip 文件:

|--zip |--bin |-- some bash scripts |--config |-- config.jar <-- jar with all .properties files inside |--j2ee-apps |-- some web related files |--META-INF |-- MANIFEST.MF |--lib |-- classes.jar <-- jar with all .class files inside (from src folder)

我尝试使用 maven-assembly-plugin,但没有找到适合我的示例。

【问题讨论】:

  • 你有明确定义的 MANIFEST.MF 所以你正在创建一个 OSGi 包?或者定义自己的 MANIFEST.MF 的目的是什么?
  • 我需要一个明确定义的 MANIFEST.MF 用于 ATG 应用程序。因为我正在使用 atg 平台。
  • MANIFEST.MF 文件的默认位置是src/main/resources/META-INF/MANIFEST.MF..此外属性到src/main/resources ...bash 脚本到src/main/scripts...

标签: java maven maven-assembly-plugin


【解决方案1】:

您的 assembly.xml 应如下所示

<assembly>
     <id>zip</id>
     <formats>
       <format>zip</format>
     </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
     <dependencySet>
      <unpack>false</unpack>
      <scope>runtime</scope>
      <outputDirectory>lib</outputDirectory>
      <useProjectArtifact>false</useProjectArtifact>
     </dependencySet>
   </dependencySets>
   <fileSets>
    <fileSet>
     <directory>${project.build.directory}</directory>
     <outputDirectory>/lib</outputDirectory>
     <includes>
       <include>*.jar</include>
     </includes>
    </fileSet> 
    <fileSet>
      <directory>${project.basedir}/bin</directory>
      <outputDirectory>/bin</outputDirectory>
      <filtered>true</filtered>
      <includes>
        <include>**/*.*</include>
      </includes>
    </fileSet>
    <!-- add fileSet entries for other folders under root -->
  </fileSets>
</assembly>

我不确定是否有任何方法可以将 META-INF 放在 zip 下,而不是放在项目的 jar 文件中。

要更改 jar 的名称,您需要 maven-jar-plugin

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <version>2.3.2</version>
  <configuration>
    <finalName>new_jar_name</finalName>                   
  </configuration>
</plugin> 

【讨论】:

  • 感谢您的回答。我已经用你提供的 assembly.xml 打包了我的项目。查看最终的 .zip 文件,我发现了 lib/groupId-version.jar。我想将此 jar 文件完全命名为 classes.jar。你知道这是怎么做的吗?
  • 更新了我的答案
  • 添加 maven-jar-plugin 后,内部 jar 文件没有更改名称。
  • 在答案中更新了 assembly.xml
猜你喜欢
  • 2013-01-13
  • 1970-01-01
  • 2018-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-15
  • 2013-05-14
相关资源
最近更新 更多