Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
132 views
in Technique[技术] by (71.8m points)

maven-assembly plugin - how to create nested assemblies

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...

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

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

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>

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.

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 descriptor:

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

Assembly plugin:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...