maven-assembly插件-如何创建嵌套的程序集 [英] maven-assembly plugin - how to create nested assemblies

查看:78
本文介绍了maven-assembly插件-如何创建嵌套的程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,试图在其中创建一个zip分发文件,其中包含(除其他文件外)一个与我的Java项目相关的可执行jar.

I have a project whereby I'm trying to create a distribution zip file, which contains (amongst other files) an executable jar with dependencies of my java project.

所以我有点希望它看起来像这样:

So I sort of want it to look like this:

-wiki-search-1.0.0-dist.zip
    -wiki-search.bat
    -wiki-search-help.html
    -wiki-search-1.0.0-jar-with-dependencies.jar
        -jar content...

我正在使用Assembly插件和预定义的描述符"jar-with-dependencies"来创建我的可执行jar文件.

I'm using the assembly plugin, and the predefined descriptor "jar-with-dependencies" to create my executable jar file.

我要在pom中指定一个单独的程序集插件条目,并引用一个自定义描述符来尝试构建可分发的zip文件.

I'm specifying a separate assembly plugin entry in my pom, referencing a custom descriptor to try and build the distributable zip file.

所以我pom的部分看起来像这样:

So the part of my pom looks like this:

<plugin>
 <artifactId>maven-assembly-plugin</artifactId>
 <configuration>
  <descriptorRefs>
   <descriptorRef>jar-with-dependencies</descriptorRef>
  </descriptorRefs>
  <archive>
   <manifest>
    <mainClass>quicksearch.QuickSearchApp</mainClass>
   </manifest>
  </archive>
 </configuration>
 <executions>
  <execution>
   <id>make-assembly</id>
   <phase>package</phase>
   <goals>
    <goal>attached</goal>
   </goals>
  </execution>
 </executions>
</plugin>
<plugin>
 <artifactId>maven-assembly-plugin</artifactId>
 <configuration>
  <descriptors>
   <descriptor>src/main/assembly/dist.xml</descriptor>
  </descriptors>
 </configuration>
 <executions>
  <execution>
   <id>make-assembly</id>
   <phase>package</phase>
   <goals>
    <goal>attached</goal>
   </goals>
  </execution>
 </executions>
</plugin>

我的自定义描述符如下:

And my custom descriptor looks like this:

<assembly>
  <id>dist</id>
  <formats>
    <format>tar.gz</format>
    <format>tar.bz2</format>
    <format>zip</format>
  </formats>
  <fileSets>
    <fileSet>
      <includes>
        <include>${project.basedir}/target/wiki-search-0.0.1-SNAPSHOT-jar-with-dependencies.jar</include>
      </includes>
      <outputDirectory>.</outputDirectory> 
    </fileSet>
    <fileSet>
      <directory>${project.basedir}/src/main/etc</directory>
      <includes>
        <include>*</include>
      </includes>
     <outputDirectory></outputDirectory> 
    </fileSet>
  </fileSets>
</assembly>

一切正常.具有依赖关系的jar正在构建.我的dist zip文件正在构建中.但是dist zip文件不包含jar-with-dependencies文件.

Everything works fine. The jar-with-dependencies is being built. My dist zip file is being built. But the dist zip file does not contain the jar-with-dependencies file.

推荐答案

使用您现有的配置,您的程序集插件的两个单独配置将被合并,并且这些配置也将被合并.

With your existing configuration, your two separate configurations for the assembly plugin will be merged, and the configurations will also be merged.

要实现您的目标,您应该定义具有多个嵌套执行的单个程序集插件配置,然后为其中的每个执行定义配置.然后,程序集插件将按顺序执行每个程序集,因此jar-with-dependencies jar将可用于包含在dist jar中.另请注意,不推荐使用attached目标,而推荐使用single目标.

To achieve your goal you should define a single assembly-plugin configuration with multiple nested executions, then define the configuration for each execution inside it. The assembly plugin will then execute each assembly sequentially, so the jar-with-dependencies jar will be available for inclusion in the dist jar. Also note the attached goal is deprecated in favour of the single goal.

还要注意,程序集中的路径是相对于根的,要包含特定文件,应使用<files>元素而不是<filesets>元素.您还可以在程序集中指定属性,以使其更改时不那么脆弱.

Also note that paths in the assembly are relative to the root, and to include a particular file you should use the <files> element rather than the <filesets> element. You can also specify properties in the assembly to make it less fragile to change.

下面重新排列的配置和组装应该可以满足您的要求:

The rearranged configuration and assembly below should do what you're after:

组装描述符:

<assembly>
  <id>dist</id>
  <formats>
    <format>tar.gz</format>
    <format>tar.bz2</format>
    <format>zip</format>
  </formats>
  <files>
    <file>
      <source>
        target/${project.artifactId}-${project.version}-jar-with-dependencies.jar
      </source>
      <outputDirectory>/</outputDirectory>
    </file>
  </files>
  <fileSets>
    <fileSet>
      <directory>${project.basedir}/src/main/resources</directory>
      <includes>
        <include>*</include>
      </includes>
     <outputDirectory>/</outputDirectory> 
    </fileSet>
  </fileSets>
</assembly>

组装插件:

<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>quicksearch.QuickSearchApp</mainClass>
     </manifest>
    </archive>
   </configuration>
  </execution>
  <execution>
   <id>dist</id>
   <phase>package</phase>
   <goals>
    <goal>single</goal>
   </goals>
   <configuration>
    <descriptors>
     <descriptor>src/main/assembly/dist.xml</descriptor>
    </descriptors>
   </configuration>
  </execution>
 </executions>
</plugin>

这篇关于maven-assembly插件-如何创建嵌套的程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆