views:

145

answers:

1

I'm trying to create a distributable zip of my project that contains several configuration files and directories, as well as the executable jar of my project. In the Maven assembly plug-in, I've found how to make the executable jar with full dependencies. However, I haven't been able to figure out how to create the zip file around the jar after it has been made. Ideally, I'd like to move the jar to a directory which already has the correct files and sub-directories, then zip the whole thing at once. Is there any way to do this?

edit:

I now have the jar building, and a rudimentary zip as well. My assembly file looks like this:

1 <assembly>
2    <id>financials-import-server</id>
3    <formats>
4       <format>zip</format>
5    </formats>
6    <dependencySets>
7       <dependencySet>
8       </dependencySet>
9    </dependencySets>
10    <files>
11       <file>
12          <source>target/import-server-1.0.0-SNAPSHOT.jar</source>
13          <destName>service.jar</destName>
14          <outputDirectory>/</outputDirectory>
15       </file>
16    </files>
17 </assembly>

I feel comfortable including the other files I would need, such as config files or shell scripts. I have a few questions remaining. How would I create empty directories within the zip? Also, how do I change the name of the file that is produced?

Thanks for the help!

+2  A: 

First, use the Maven JAR plugin to create an executable JAR.

Then, configure your assembly to include all the files needed (eventually you can add a run.bat / run.sh file that will launches the application), and also all dependencies.

Finally, bind the assembly creation to your package goal in the pom.xml:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <version>2.2-beta-5</version>
      <configuration>
        <descriptors>
          <descriptor>/path/to/my-assembly.xml</descriptor>
        </descriptors>
      </configuration>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
        </execution>
      </executions>
      ...

This way, you will simply need to run one Maven command mvn clean install for example, to compile your project, and then creates the ZIP package.

Do not hesitate to update your question if my answer doesn't fit your needs, and you can also give your current assembly.xml and pom.xml...


Edit

Regarding your update:

I don't think the assembly will let you create an empty directory. An idea is to put an empty file (or a readme.txt for example) and include the directory in the final ZIP. For example, in my project, I want to have a logs/ directory, so I have a logs/ directory in my project which only contains a readme.txt file:

<fileSets>
    <fileSet>
        <directory>../my-package/logs</directory>
        <outputDirectory>logs/</outputDirectory>
    </fileSet>
</fileSets>

For the name of the ZIP created, you can specify it in the pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2-beta-4</version>
            <configuration>
                <descriptors>
                    <descriptor>my-assembly.xml</descriptor>
                </descriptors>
                <finalName>TheName</finalName>

If I am correct, the assembly will use this name, add the ID defined in the assembly.xml file, and uses the prefix adapted to the format specified.

romaintaz
Thanks, I worked some based on this, and made some progress. Then I got stuck again. Any suggestions?
Zoe Gagnon
@ZoeGagnon I've updated my answer.
romaintaz
Thank you. Your answer is fantastic, and very helpful.
Zoe Gagnon
you're welcome :)
romaintaz