使用 Maven 程序集插件将 Jar 与依赖项和 ZIP 文件相结合

2022-09-02 10:59:10

我想将JAR中的源代码与所有依赖项相结合,并将其与其他文件一起打包成zip文件。我能够创建一个包含所有依赖项的Jar以及ZIP文件,但我无法将两者结合起来。

我最终想要在zip文件中使用以下目录结构:

加载器/箱/外壳脚本.sh

loader/lib/jar-with-dependencies.jar

loader/appname/config/config.xml

这是我的pom文件的摘录:

<plugins>
    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                       <mainClass>fully.qualified.MainClass</mainClass>
                    </manifest>
                 </archive>
                 <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                 </descriptorRefs>
                 <descriptors>
                    <descriptor>src/main/assembly/assembly.xml</descriptor>
                 </descriptors>
              </configuration>
        </plugin>

这是来自我的程序集的摘录

<assembly>
    <id>bin</id>
    <!-- Specifies that our binary distribution is a zip package -->
    <formats>
        <format>zip</format>
    </formats>

    <baseDirectory>SpreadsheetLoaderApp</baseDirectory>

    <fileSets>
        <fileSet>
            <directory>corporatebondpurchases</directory>
            <outputDirectory>${basedir}/corporatebondpurchases/config</outputDirectory>
            <includes>
                <include>*.xml</include>
                <include>*.properties</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>corporatebondpurchases</directory>
            <outputDirectory>${basedir}/bin</outputDirectory>
            <includes>
                <include>*.sh</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

下面是部分构建的输出:

[INFO] --- maven-assembly-plugin:2.4:single (default-cli) @ SpreadsheetLoader ---
[INFO] Reading assembly descriptor: src/main/assembly/assembly.xml
[INFO] Building zip: C:\Software\SpringSTS\workspace\SpreadsheetLoader\target\SpreadsheetLoader-0.0.1-SNAPSHOT-bin.zip
[INFO] Building jar: C:\Software\SpringSTS\workspace\SpreadsheetLoader\target\SpreadsheetLoader-0.0.1-SNAPSHOT-jar-with-dependencies

这成功地在目标下以及ZIP文件下创建了一个Jar(尽管具有完整路径而不是相对路径)。我想要的是将 Jar 文件作为 zip 文件的一部分包含在内。

编辑:

在研究了各种博客文章之后,我设法通过使用以下POM和汇编文件来使其工作

<build>
   <plugins>
       <plugin>
       <artifactId>maven-assembly-plugin</artifactId>
       <executions>
           <execution>
               <id>jar-with-dependencies</id>
               <phase>package</phase>
               <goals>
                   <goal>single</goal>
               </goals>
               <configuration>
                   <descriptorRefs>
                       <descriptorRef>jar-with-dependencies</descriptorRef>
                   </descriptorRefs>
                   <archive>
                       <manifest>
                           <mainClass>fully.qualified.MainClass</mainClass>
                       </manifest>
                   </archive>
               </configuration>
           </execution>
           <execution>
               <id>dist</id>
               <phase>package</phase>
               <goals>
                   <goal>single</goal>
               </goals>
               <configuration>
                   <descriptors>
                       <descriptor>src/main/assembly/assembly.xml</descriptor>
                   </descriptors>
               </configuration>
           </execution>
       </executions>
   </plugin>
  </plugins>
</build>

以及相应的汇编文件...

<assembly>
    <id>dist</id>
    <!-- Specifies that our binary distribution is a zip package -->
    <formats>
        <format>zip</format>
    </formats>

    <includeBaseDirectory>true</includeBaseDirectory>
    <baseDirectory>SpreadsheetLoader</baseDirectory>

    <files>
        <file>
            <source>target/${project.artifactId}-${project.version}-jar-with-dependencies.jar</source>
            <outputDirectory>lib</outputDirectory>
        </file>
    </files>

    <fileSets>
        <fileSet>
            <directory>applicationbuild/${buildname}</directory>
            <outputDirectory>${buildname}/config</outputDirectory>
            <includes>
                <include>*.xml</include>
                <include>*.properties</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>applicationbuild/${buildname}</directory>
            <outputDirectory>bin</outputDirectory>
            <includes>
                <include>*.sh</include>
            </includes>
            <lineEnding>unix</lineEnding>
            <fileMode>0755</fileMode>
        </fileSet>
        <fileSet>
            <directory>./</directory>
            <outputDirectory>logs</outputDirectory>
            <excludes>
                <exclude>*/**</exclude>
            </excludes>
        </fileSet>
        <fileSet>
            <directory>./</directory>
            <outputDirectory>${buildname}/sourcedata</outputDirectory>
            <excludes>
                <exclude>*/**</exclude>
            </excludes>
        </fileSet>
    </fileSets>
</assembly>

这为我提供了一个很好的简单目录结构,用于部署目的。我的主要信息来源来自以下网站:

http://www.drawbackz.com/stack/175442/maven-assembly-plugin-how-to-create-nested-assemblies.html

谢谢


答案 1

我对上述结果感到满意。然而,这是我第一次尝试使用Maven。如果有人有更好的方法来做到这一点,我很乐意接受这个建议。


答案 2

推荐