Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Maven to create a zip file containing an executable jar with dependencies

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:

<assembly>
   <id>financials-import-server</id>
   <formats>
      <format>zip</format>
   </formats>
   <dependencySets>
      <dependencySet>
      </dependencySet>
   </dependencySets>
    <files>
       <file>
          <source>target/import-server-1.0.0-SNAPSHOT.jar</source>
          <destName>service.jar</destName>
          <outputDirectory>/</outputDirectory>
       </file>
    </files>
</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!

like image 558
Zoe Avatar asked Sep 15 '10 20:09

Zoe


1 Answers

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.

like image 111
Romain Linsolas Avatar answered Nov 04 '22 04:11

Romain Linsolas