【问题标题】:Create zip in maven with additional files next to the jar在 Maven 中使用 jar 旁边的其他文件创建 zip
【发布时间】:2016-02-12 03:12:43
【问题描述】:

我唯一的 Maven 经验是包含其他库,所以我需要一个非常基本的解释,了解如何使用 Eclipse 在 Maven 中实现。

我想定期创建我的 jar。然后我想再拿 3 个文件并将所有文件放在 1 个 zip 文件中。我的 zip 的内容应该是这样的:

A.jar
B.txt
C.txt
D.bat

我知道我必须在我的pom.xml 文件中插入某些目标。我已经在 Stack Overflow(即here)上看到过关于类似主题的帖子。我还尝试了像 here 这样的 Maven 文档。我是否已经需要任何 Maven 插件或者仍然是 Maven 核心的东西?

但我的技能还不足以为我的案件传递这些信息。

【问题讨论】:

  • 解决方案是 maven-assembly-plugin,如引用 (stackoverflow.com/questions/5717183/…) 的答案中所述。您需要做的就是将 maven-assembly-plugin 包含到您的 pom.xml 中,并在 src/main/assemble 中创建一个自定义的 assembly.xml。一般来说,assembly.xml 中的文件集定义了将包含在创建的存档中的内容。调用 mvn clean install 后,新的 zip 存档应该在您的项目目标文件夹中可用。希望对您有所帮助...
  • 程序集描述符的默认位置是src/assembly...参见maven.apache.org/guides/introduction/…

标签: java eclipse maven zip pom.xml


【解决方案1】:

当您需要创建自定义工件时,您需要使用maven-assembly-plugin。我强烈建议您阅读 Maven 书的Chapter 8. Maven Assemblies 以帮助您开始使用程序集。本章深入讲解了Maven程序集的创建,通俗易懂。

基本上,程序集是在assembly descriptor 的帮助下创建的。以下程序集描述符将在 zip 存档的根目录中包含项目主工件。您可以在此处添加更多<file><fileSets> 声明,以将您的自定义文件添加到您的存档中(逻辑相同)

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>assemby-id</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <files>
        <file>
            <source>${project.build.directory}/${project.build.finalName}.${project.packaging}</source>
            <outputDirectory>/</outputDirectory>
        </file>
    </files>
</assembly>

然后你只需要在你的 POM 中声明这个插件。在这种情况下,插件绑定到package 阶段并使用上面的描述符进行配置。默认情况下,程序集 id 会附加到存档的名称中:我在这里删除了它。

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.5.5</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <descriptors>
                    <descriptor>assembly.xml</descriptor> <!-- path to the descriptor -->
                </descriptors>
                <appendAssemblyId>false</appendAssemblyId>
            </configuration>
        </execution>
    </executions>
</plugin>

运行mvn clean package 时,您会看到在target 目录中创建了一个zip 存档。

【讨论】:

  • 不明白以下变量的用法:${project.build.directory}/${project.build.finalName}.${project.packaging}您将我的文件 A、B、C 和 D 包含在哪一部分?
  • @BerndErnst 变量在这里,因此主要工件的部分不是硬编码的,仅此而已。关于文件 A 到 D,正如我在回答中所说,您可以为每个文件添加 &lt;file&gt; 元素。这取决于你的目录结构,所以我无法给出更具体的答案。
【解决方案2】:
+ project 
    + src/main/java
    + src/main/resources
    + src/main/config
            +B.txt
            +C.txt
            +D.bat
    + src/main/assembly
            +bin.xml
    +pom.xml

bin.xml

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
  <id>bin</id>
  <baseDirectory>/</baseDirectory>
  <formats>
    <format>zip</format>
  </formats>
   <fileSets>
    <fileSet>
      <directory>${project.basedir}/src/main/config</directory>
      <outputDirectory>/</outputDirectory> 
    </fileSet>
    <fileSet>
      <directory>${project.build.directory}</directory>
      <outputDirectory>/</outputDirectory>
      <includes>
        <include>*.jar</include>
      </includes>
    </fileSet> 
  </fileSets>
</assembly>

poml.xml

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <descriptors>
            <descriptor>src/main/assembly/bin.xml</descriptor>
        </descriptors>
        <appendAssemblyId>false</appendAssemblyId>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id> <!-- this is used for inheritance merges -->
            <phase>package</phase> <!-- append to the packaging phase. -->
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

输出: mvn 清洁包

+ artifactId-version.zip
    + B.txt
    +C.txt
    +D.txt
    +artifactId-version.jar

在 src/main/resources 中没有添加 B.txt、C.txt、D.bat,因为将它们放在 CLASSSPATH 中并不好

【讨论】:

    猜你喜欢
    • 2012-01-07
    • 1970-01-01
    • 2012-01-11
    • 1970-01-01
    • 2016-08-28
    • 1970-01-01
    • 2017-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多