使用Maven程序集插件组合Jar-with-dependencies和ZIP文件

时间:2015-03-08 22:33:51

标签: java maven jar maven-assembly-plugin

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

我最终希望zip文件中包含以下目录结构:

装载器/斌/ shellscript.sh

装载机/ lib中/罐与 - dependencies.jar

装载器/应用程序的名字/配置/ 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 个答案:

答案 0 :(得分:5)

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