Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven plugin to create executable jar with dependencies not unpacked (jar with jars)

I read a lot of solutions to build executable jar with dependencies (maven shade plugin, maven dependency plugin, maven assembly plugin) and all of this plugins unpack dependency jars and repack them in executable jar. The only plugin that pack dependency jars unpacked in executable jar is one jar plugin but this plugin add its runner code in executable jar.

Is there any solution to create jar like this:

├─executable.jar
├──lib/
├───dependency1.jar
├───dependency2.jar
.
.
.

and that solution to work.

like image 969
dalibor983 Avatar asked Jul 16 '13 11:07

dalibor983


1 Answers

The most common way is to use assembly plugin which will allow you to configure packaging in a way you need

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
         <archive>
              <manifest>
                   <mainClass>com.somewhere.Main</mainClass>
              </manifest>
         </archive>
         <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
         </descriptorRefs>
    </configuration>
    <executions>
          <execution>
               <id>make-assembly</id>
               <phase>package</phase>
               <goals>
                    <goal>single</goal>
               </goals>
         </execution>
    </executions>
</plugin>

Also you can specify assembly descriptor for configuration

<configuration>
    <appendAssemblyId>false</appendAssemblyId>
    <descriptors>
      <descriptor>src/main/assembly/assembly.xml</descriptor>
    </descriptors>
</configuration>

And assembly.xml itself

<assembly>
    <id>assembly</id>
    <formats>
         <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
             <directory>${project.build.outputDirectory}</directory>
             <outputDirectory>/</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

Assembly descriptor can also contain dependency section:

<!-- lib -->
<dependencySets>
    <dependencySet>
        <outputDirectory>lib</outputDirectory>
    </dependencySet>
</dependencySets>

As far as I understand you're looking for the last one. As it just includes jar files into the assembly without any modifications. So the final solution will look like:

<assembly>
    <id>assembly</id>
    <formats>
         <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <!-- lib -->
    <dependencySets>
        <dependencySet>
             <outputDirectory>lib</outputDirectory>
        </dependencySet>
    </dependencySets>
</assembly>

and pom part:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <appendAssemblyId>false</appendAssemblyId>
        <descriptors>
             <descriptor>src/main/assembly/assembly.xml</descriptor>
        </descriptors>
    </configuration>
    <executions>
          <execution>
               <id>make-assembly</id>
               <phase>package</phase>
               <goals>
                    <goal>single</goal>
               </goals>
         </execution>
    </executions>
</plugin>
like image 109
nesteant Avatar answered Oct 24 '22 16:10

nesteant