Search code examples
javamavenmaven-assembly-plugin

How to package a Maven project in zip file with jar files inside


Small introduction

I'm pretty new in maven and I face with the problem. The structure of my project looks like this:

|--root |--bin |-- some bash scripts |--config |-- some .properties files |--j2ee-apps |-- some web related files |--META-INF |-- MANIFEST.MF |--src |-- main |-- java |-- test |-- java

Goal

After executing mvn package I have to get the .zip file with following structure:

|--zip |--bin |-- some bash scripts |--config |-- config.jar <-- jar with all .properties files inside |--j2ee-apps |-- some web related files |--META-INF |-- MANIFEST.MF |--lib |-- classes.jar <-- jar with all .class files inside (from src folder)

I've tried to use maven-assembly-plugin, but I didn't find examples that suit me.


Solution

  • Your assembly.xml should looks like

    <assembly>
         <id>zip</id>
         <formats>
           <format>zip</format>
         </formats>
        <includeBaseDirectory>false</includeBaseDirectory>
        <dependencySets>
         <dependencySet>
          <unpack>false</unpack>
          <scope>runtime</scope>
          <outputDirectory>lib</outputDirectory>
          <useProjectArtifact>false</useProjectArtifact>
         </dependencySet>
       </dependencySets>
       <fileSets>
        <fileSet>
         <directory>${project.build.directory}</directory>
         <outputDirectory>/lib</outputDirectory>
         <includes>
           <include>*.jar</include>
         </includes>
        </fileSet> 
        <fileSet>
          <directory>${project.basedir}/bin</directory>
          <outputDirectory>/bin</outputDirectory>
          <filtered>true</filtered>
          <includes>
            <include>**/*.*</include>
          </includes>
        </fileSet>
        <!-- add fileSet entries for other folders under root -->
      </fileSets>
    </assembly>
    

    I'm not sure if there any way to put META-INF just under zip not inside your project's jar file.

    To change name of you jar you need maven-jar-plugin

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>2.3.2</version>
      <configuration>
        <finalName>new_jar_name</finalName>                   
      </configuration>
    </plugin>