在Maven中创建包含附加文件的zip压缩文件与jar文件并列。

13

我对Maven唯一的经验就是包含其他库,因此我需要一个非常基础的解释来了解如何在Eclipse中使用Maven。

我想定期创建我的jar文件。然后我想取出3个进一步的文件并将所有文件放在一个zip文件中。我的zip文件内容应该像这样:

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

我知道我需要在pom.xml文件中添加特定的目标。我已经看过 Stack Overflow 上的帖子(例如 这里)了解相关主题。我还尝试了Maven的文档,例如这里。我是否已经需要使用任何Maven插件或者这已经是Maven核心的东西了?

但是我的技能还不足以将这些信息应用到我的情况中。


1
解决方案是使用maven-assembly-plugin,如在参考链接中所述(https://dev59.com/wG025IYBdhLWcg3w7qfL)。您需要将maven-assembly-plugin包含到pom.xml中,并在src/main/assemble中创建自定义的assembly.xml。一般来说,assembly.xml中的文件集定义了将包含在创建的归档文件中的内容。调用mvn clean install后,新的zip归档文件应该可以在项目的目标文件夹中找到。希望这有所帮助... - Jochen.Kohler
汇编描述符的默认位置是 src/assembly...请参阅 https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html - khmarbaise
2个回答

16
+ 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>

pom.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 clean package

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

由于将它们放在CLASSPATH中不太合适,因此未在src/main/resources中添加B.txt、C.txt、D.bat。


8
您需要使用maven-assembly-plugin来创建自定义构件。我强烈建议您阅读Maven书籍的第8章 Maven Assemblies,以便了解如何使用组件。本章包含有关创建Maven组件的深入说明,并且易于理解。
基本上,使用组件描述文件可以创建组件。以下组件描述将在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命令后,您将看到一个zip归档文件已经被创建在target目录中。

我不理解以下变量的用法:<source>${project.build.directory}/${project.build.finalName}.${project.packaging}</source> 你在哪个部分包含我的文件A、B、C和D? - Ernst Robert
1
@BerndErnst 这些变量的作用是为了避免将主要部分硬编码,仅此而已。至于文件 A 到 D,正如我在回答中所说,您可以为每个文件添加 <file> 元素。这取决于您的目录结构,因此我无法给出更具体的答案。 - Tunaki

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接