tags:

views:

1413

answers:

2

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

Any help would be appreciated.

A: 

You have two different configurations for the assembly plug-in, and you want them to be run in order (jar before zip), but I don't think Maven implies any order in how this will be resolved. My guess is that the zip file is being generated before the JAR file.

Even if that is not the case, I would suggest you create one module per artifact. Move the JAR assembly into its own module and have the now Zip-only module depend on it. That way Maven's dependency order resolution can kick in and build them in order.

SingleShot
+10  A: 

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>
Rich Seller
+1 very nice and detailed answer
Pascal Thivent
Legend. Worked first time. Cheers.
Thanks! Is there a way to achieve this without attaching the execution to the default life cycle? Just omitting the 'phase' and 'goals' elements didn't work for me.
Roland Schneider