java - Comment puis-je créer un JAR exécutable avec des dépendances utilisant Maven

Mots clés : javamaven-2build-processbuild-automationexecutable-jarjava

meilleur 5 Réponses java - Comment puis-je créer un JAR exécutable avec des dépendances utilisant Maven

vote vote

95

<build>   <plugins>     <plugin>       <artifactId>maven-assembly-plugin</artifactId>       <configuration>         <archive>           <manifest>             <mainClass>fully.qualified.MainClass</mainClass>           </manifest>         </archive>         <descriptorRefs>           <descriptorRef>jar-with-dependencies</descriptorRef>         </descriptorRefs>       </configuration>     </plugin>   </plugins> </build> 
mvn clean compile assembly:single 
<plugin>   <artifactId>maven-assembly-plugin</artifactId>   <configuration>     <archive>       <manifest>         <mainClass>fully.qualified.MainClass</mainClass>       </manifest>     </archive>     <descriptorRefs>       <descriptorRef>jar-with-dependencies</descriptorRef>     </descriptorRefs>   </configuration>   <executions>     <execution>       <id>make-assembly</id> <!-- this is used for inheritance merges -->       <phase>package</phase> <!-- bind to the packaging phase -->       <goals>         <goal>single</goal>       </goals>     </execution>   </executions> </plugin> 
vote vote

82

<plugin>     <groupId>org.apache.maven.plugins</groupId>     <artifactId>maven-dependency-plugin</artifactId>     <executions>         <execution>             <id>copy-dependencies</id>             <phase>prepare-package</phase>             <goals>                 <goal>copy-dependencies</goal>             </goals>             <configuration>                 <outputDirectory>${project.build.directory}/lib</outputDirectory>                 <overWriteReleases>false</overWriteReleases>                 <overWriteSnapshots>false</overWriteSnapshots>                 <overWriteIfNewer>true</overWriteIfNewer>             </configuration>         </execution>     </executions> </plugin> <plugin>     <groupId>org.apache.maven.plugins</groupId>     <artifactId>maven-jar-plugin</artifactId>     <configuration>         <archive>             <manifest>                 <addClasspath>true</addClasspath>                 <classpathPrefix>lib/</classpathPrefix>                 <mainClass>theMainClass</mainClass>             </manifest>         </archive>     </configuration> </plugin> 
vote vote

73

<plugin>   <groupId>org.apache.maven.plugins</groupId>   <artifactId>maven-dependency-plugin</artifactId>   <executions>     <execution>       <id>copy-dependencies</id>       <phase>prepare-package</phase>       <goals>         <goal>copy-dependencies</goal>       </goals>       <configuration>         <outputDirectory>${project.build.directory}/${project.build.finalName}.lib</outputDirectory>       </configuration>     </execution>   </executions> </plugin> 
<plugin>   <groupId>org.apache.maven.plugins</groupId>   <artifactId>maven-jar-plugin</artifactId>   <configuration>     <archive>       <manifest>         <addClasspath>true</addClasspath>         <classpathPrefix>${project.build.finalName}.lib/</classpathPrefix>         <mainClass>${fully.qualified.main.class}</mainClass>       </manifest>     </archive>   </configuration> </plugin> 
$ java -jar target/${project.build.finalName}.jar 
<plugin>   <groupId>org.apache.maven.plugins</groupId>   <artifactId>maven-antrun-plugin</artifactId>   <executions>     <execution>       <id>antrun-archive</id>       <phase>package</phase>       <goals>         <goal>run</goal>       </goals>       <configuration>         <target>           <property name="final.name" value="${project.build.directory}/${project.build.finalName}"/>           <property name="archive.includes" value="${project.build.finalName}.${project.packaging} ${project.build.finalName}.lib/*"/>           <property name="tar.destfile" value="${final.name}.tar"/>           <zip basedir="${project.build.directory}" destfile="${final.name}.zip" includes="${archive.includes}" />           <tar basedir="${project.build.directory}" destfile="${tar.destfile}" includes="${archive.includes}" />           <gzip src="${tar.destfile}" destfile="${tar.destfile}.gz" />           <bzip2 src="${tar.destfile}" destfile="${tar.destfile}.bz2" />         </target>       </configuration>     </execution>   </executions> </plugin> 
<plugin>   <groupId>org.apache.maven.plugins</groupId>   <artifactId>maven-assembly-plugin</artifactId>   <executions>     <execution>       <phase>package</phase>       <goals>         <goal>single</goal>       </goals>       <configuration>         <archive>           <manifest>             <mainClass>${fully.qualified.main.class}</mainClass>           </manifest>         </archive>         <descriptorRefs>           <descriptorRef>jar-with-dependencies</descriptorRef>         </descriptorRefs>       </configuration>     </execution>   </executions> </plugin> 
<plugin>   <groupId>org.apache.maven.plugins</groupId>   <artifactId>maven-shade-plugin</artifactId>   <executions>     <execution>       <goals>         <goal>shade</goal>       </goals>       <configuration>         <shadedArtifactAttached>true</shadedArtifactAttached>         <transformers>           <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">             <mainClass>${fully.qualified.main.class}</mainClass>           </transformer>         </transformers>       </configuration>     </execution>   </executions> </plugin> 
<plugin>   <!--groupId>org.dstovall</groupId--> <!-- not available on the central -->   <groupId>com.jolira</groupId>   <artifactId>onejar-maven-plugin</artifactId>   <executions>     <execution>       <configuration>         <mainClass>${fully.qualified.main.class}</mainClass>         <attachToBuild>true</attachToBuild>         <!-- https://code.google.com/p/onejar-maven-plugin/issues/detail?id=8 -->         <!--classifier>onejar</classifier-->         <filename>${project.build.finalName}-onejar.${project.packaging}</filename>       </configuration>       <goals>         <goal>one-jar</goal>       </goals>     </execution>   </executions> </plugin> 
<plugin>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-maven-plugin</artifactId>   <executions>     <execution>       <goals>         <goal>repackage</goal>       </goals>       <configuration>         <classifier>spring-boot</classifier>         <mainClass>${fully.qualified.main.class}</mainClass>       </configuration>     </execution>   </executions> </plugin> 
vote vote

70

<build>     <plugins>         <plugin>             <groupId>org.apache.maven.plugins</groupId>             <artifactId>maven-jar-plugin</artifactId>             <configuration>                 <archive>                     <manifest>                         <addClasspath>true</addClasspath>                         <mainClass>fully.qualified.MainClass</mainClass>                     </manifest>                 </archive>             </configuration>         </plugin>         <plugin>             <artifactId>maven-assembly-plugin</artifactId>             <configuration>                 <descriptorRefs>                     <descriptorRef>jar-with-dependencies</descriptorRef>                 </descriptorRefs>             </configuration>         </plugin>     </plugins> </build> 
<project>   [...]   <build>       <plugins>           <plugin>               <artifactId>maven-assembly-plugin</artifactId>               <configuration>                   <archive>                       <manifest>                           <addClasspath>true</addClasspath>                           <mainClass>fully.qualified.MainClass</mainClass>                       </manifest>                   </archive>                   <descriptorRefs>                       <descriptorRef>jar-with-dependencies</descriptorRef>                   </descriptorRefs>               </configuration>               <executions>                   <execution>                       <id>make-my-jar-with-dependencies</id>                       <phase>package</phase>                       <goals>                           <goal>single</goal>                       </goals>                   </execution>               </executions>           </plugin>       [...]       </plugins>     [...]   </build> </project> 
vote vote

51

      <plugins>     <!-- This plugin provides the capability to package the artifact in an uber-jar, including its dependencies and to shade - i.e. rename - the packages of some of the dependencies. -->         <plugin>             <groupId>org.apache.maven.plugins</groupId>             <artifactId>maven-shade-plugin</artifactId>             <version>1.4</version>             <executions>                 <execution>                     <phase>package</phase>                     <goals>                         <goal>shade</goal>                     </goals>                     <configuration>                         <artifactSet>                         <!-- signed jars-->                             <excludes>                                 <exclude>bouncycastle:bcprov-jdk15</exclude>                             </excludes>                         </artifactSet>                           <transformers>                             <transformer                                 implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">                                 <!-- Main class -->                                 <mainClass>com.main.MyMainClass</mainClass>                             </transformer>                             <!-- Use resource transformers to prevent file overwrites -->                             <transformer                                   implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">                                 <resource>properties.properties</resource>                             </transformer>                             <transformer                                 implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">                                 <resource>applicationContext.xml</resource>                             </transformer>                             <transformer                                 implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">                                 <resource>META-INF/cxf/cxf.extension</resource>                             </transformer>                             <transformer                                 implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">                                 <resource>META-INF/cxf/bus-extensions.xml</resource>                             </transformer>                      </transformers>                     </configuration>                 </execution>             </executions>         </plugin>      </plugins> 

Questions similaires