使用jar旁边的其他文件在maven中创建zip

Ern*_*nst 9 java eclipse zip pom.xml maven

我拥有的唯一Maven体验包括其他库,因此我需要一个非常基本的解释,说明如何使用Eclipse在Maven中实现目标.

我想定期制作我的罐子.然后我想再拿3个文件并将所有文件放在一个zip文件中.我的zip内容应如下所示:

A.jar
B.txt
C.txt
D.bat
Run Code Online (Sandbox Code Playgroud)

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

但是我的技能还不足以为我的案子转移这些信息.

que*_*com 11

+ project 
    + src/main/java
    + src/main/resources
    + src/main/config
            +B.txt
            +C.txt
            +D.bat
    + src/main/assembly
            +bin.xml
    +pom.xml
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

输出:mvn clean包

+ artifactId-version.zip
    + B.txt
    +C.txt
    +D.txt
    +artifactId-version.jar
Run Code Online (Sandbox Code Playgroud)

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


Tun*_*aki 5

需要maven-assembly-plugin创建自定义工件时,需要使用。我强烈建议您阅读第8章。Maven程序集。Maven书籍中的Maven程序集,让您开始使用程序集。本章包含有关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>
Run Code Online (Sandbox Code Playgroud)

然后,您只需要在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>
Run Code Online (Sandbox Code Playgroud)

运行时mvn clean package,您会看到将在target目录中创建一个zip归档文件。