tags:

views:

1202

answers:

4

Suppose I have the following directory layout in a Maven project:

src/
|-- main
|   |-- bin
|   |   |-- run.cmd
|   |   `-- run.sh
|   |-- etc
|   |   |-- common-spring.xml
|   |   |-- log4j.xml
|   |   `-- xml-spring.xml
|   `-- java
|       `-- com
...

I would like to build a zip file that, when unzipped, produces something like this:

assembly
|-- bin
|   |-- run.cmd
|   `-- run.sh
|-- etc
|   |-- common-spring.xml
|   |-- log4j.xml
|   `-- xml-spring.xml
`-- lib
    |-- dependency1.jar
    |-- dependency2.jar
...

where `run.xx' are executable shell scripts that will call my main application and put all dependencies on the classpath.

Is this possible with some of the `official' Maven plugins, e.g. maven-assembly-plugin?

+6  A: 

I use the AppAssembler plugin to get something similar. Example:

...
<build>
<plugins>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>appassembler-maven-plugin</artifactId>
    <configuration>
      <programs>
        <program>
          <mainClass>com.acme.MainClass</mainClass>
          <name>app</name>
        </program>
      </programs>
    </configuration>
  </plugin>
</plugins>

jassuncao
A: 

The appassembler generates the 'run.xx' files for you.

If you have already created the shell scripts yourself you can use the maven-assembly-plugin to create the zip file. To gather the dependencies you can use maven-dependency-plugin.

+1  A: 

The maven-assembly-plugin can also copy the dependencies into your assembly, by including something like the below in your assembly descriptor file:

<dependencySets>
 <!-- Copy dependency jar files to 'lib' -->
 <dependencySet>
  <outputDirectory>lib</outputDirectory>
  <includes>
   <include>*:jar:*</include>
  </includes>
 </dependencySet>
</dependencySets>
+2  A: 

I've used the maven-assembly-plugin to acheive something similar in a project. I wanted a zip file to be built during the package phase, instead of manually calling assembly:assembly. Here's what I came up with:

/src/assemble/distribution.xml:

<assembly>
  <id>distribution</id>

  <!-- specify the output formats -->
  <formats>
    <format>zip</format>
  </formats>

  <!-- include all runtime libraries in the /lib folder of the output file -->
  <dependencySets>
    <dependencySet>
      <outputDirectory>/lib</outputDirectory>
      <scope>runtime</scope>
    </dependencySet>
  </dependencySets>

  <fileSets>
    <!-- include all *.jar files in the target directory -->
    <fileSet>
      <directory>target</directory>
      <outputDirectory></outputDirectory>
      <includes>
        <include>*.jar</include>
      </includes>
    </fileSet>

    <!-- include all files in the /conf directory -->
    <fileSet>
       <outputDirectory></outputDirectory>
       <includes>
        <include>conf/**</include>
       </includes>
    </fileSet>
  </fileSets>

</assembly>

/pom.xml

...

  <plugin>
   <artifactId>maven-assembly-plugin</artifactId>

   <configuration>
    <descriptors>
     <descriptor>src/assemble/distribution.xml
     </descriptor>
    </descriptors>
   </configuration>

   <!-- append assembly:assembly to the package phase -->
   <executions>
    <execution>
     <phase>package</phase>
     <goals>
      <goal>assembly</goal>
     </goals>
    </execution>
   </executions>

  </plugin>

...

Can this assembly be deployed automatically with mvn deploy?
Nathan Feger