将Maven项目打包成可执行jar文件和外种配置方式(引用第三方jar)

时间:2021-08-18 09:32:14

http://blog.csdn.net/zhangdaiscott/article/details/6911640

方法一. mvn assembly 或 mvn package (一个jar包)

把依赖包和自己项目的文件打包如同一个jar包(这种方式对spring的项目不支持)

[html] view plain copy
  1.   <build>  
  2.       <plugins>  
  3.           <plugin>  
  4.               <artifactId>maven-assembly-plugin</artifactId>  
  5.               <configuration>  
  6.                   <archive>  
  7.                       <manifest>  
  8.                           <mainClass>com.tang.CSVUtils</mainClass>  
  9.                       </manifest>  
  10.                   </archive>  
  11.                   <descriptorRefs>  
  12.                       <descriptorRef>  
  13.                           jar-with-dependencies  
  14.                       </descriptorRef>  
  15.                   </descriptorRefs>  
  16.               </configuration>  
  17.               <!-- 加上下面这一段,使用 mvn package命令,不加则使用mvn assembly-->  
  18.               <!-- <executions>     
  19.                   <execution>  
  20.                       <id>make-assemble</id>  
  21.                       <phase>package</phase>  
  22.                       <goals>  
  23.                           <goal>single</goal>  
  24.                       </goals>  
  25.                   </execution>  
  26.               </executions> -->  
  27.           </plugin>  
  28.       </plugins>  
  29.   </build>  
Eclipse 中 Run As--- Maven clean ---Maven assembly:assembly(或 Maven package)

Target目录生成如下文件:

将Maven项目打包成可执行jar文件和外种配置方式(引用第三方jar)

其中,

testLog4j-0.1.jar 是不可直接运行的,因为没有带第三方包。

testLog4j-0.1-jar-with-dependencies.jar 就是带有第三方包的可执行 jar 包,

在命令行执行 java -jar testLog4j-0.1-jar-with-dependencies.jar 即可运行


方法二:mvn package(一个jar包和一个lib文件夹)

[html] view plain copy
  1. <build>  
  2.     <plugins>  
  3.         <plugin>  
  4.             <groupId>org.apache.maven.plugins</groupId>  
  5.             <artifactId>maven-jar-plugin</artifactId>  
  6.             <version>2.4</version>  
  7.             <configuration>  
  8.                 <archive>  
  9.                     <manifest>  
  10.                         <addClasspath>true</addClasspath>  
  11.                         <classpathPrefix>lib/</classpathPrefix>  
  12.                         <mainClass>com.tang.CSVUtils</mainClass>  
  13.                     </manifest>  
  14.                 </archive>  
  15.             </configuration>  
  16.         </plugin>  
  17.         <plugin>  
  18.             <groupId>org.apache.maven.plugins</groupId>  
  19.             <artifactId>maven-dependency-plugin</artifactId>  
  20.             <executions>  
  21.                 <execution>  
  22.                     <id>copy</id>  
  23.                     <phase>package</phase>  
  24.                     <goals>  
  25.                         <goal>copy-dependencies</goal>  
  26.                     </goals>  
  27.                     <configuration>  
  28.                         <outputDirectory>${project.build.directory}/lib</outputDirectory>  
  29.                     </configuration>  
  30.                 </execution>  
  31.             </executions>  
  32.         </plugin>  
  33.     </plugins>  
  34. </build>    

执行maven package 进行打包进入target目录。

其中lib就是第三方jar包的目录,在其他项目引入testLog4j-0.1.jar会自动找到并引入lib里面的包.

只需要拷贝这两个(lib和testLog4j-0.1.jar)即可放到其他地方用了。

在命令行执行:java -jar testLog4j-0.1.jar

将Maven项目打包成可执行jar文件和外种配置方式(引用第三方jar)

方法三:mvn package(推荐)(一个zip包,里面有一个jar包,一个lib文件夹,和一个conf文件夹)

(1)conf/package.xml (conf 文件夹和pom.xml在同一级目录) [html] view plain copy
  1. <assembly>    
  2.     <id>bin</id>    
  3.     <!-- 最终打包成一个用于发布的zip文件 -->    
  4.     <formats>    
  5.         <format>zip</format>    
  6.     </formats>    
  7.     
  8.     <!-- Adds dependencies to zip package under lib directory -->    
  9.     <dependencySets>    
  10.         <dependencySet>    
  11.             <!--  
  12.                不使用项目的artifact,第三方jar不要解压,打包进zip文件的lib目录  
  13.            -->    
  14.             <useProjectArtifact>false</useProjectArtifact>    
  15.             <outputDirectory>lib</outputDirectory>    
  16.             <unpack>false</unpack>    
  17.         </dependencySet>    
  18.     </dependencySets>    
  19.     
  20.     <fileSets>    
  21.         <!-- 把项目相关的说明文件,打包进zip文件的根目录 -->    
  22.         <fileSet>    
  23.             <directory>${project.basedir}</directory>    
  24.             <outputDirectory>/</outputDirectory>    
  25.             <includes>    
  26.                 <include>README*</include>    
  27.                 <include>LICENSE*</include>    
  28.                 <include>NOTICE*</include>    
  29.             </includes>    
  30.         </fileSet>    
  31.             
  32.         <!-- 把项目的配置文件,打包进zip文件的config目录 -->    
  33.         <fileSet>    
  34.             <directory>${project.basedir}\conf</directory>    
  35.             <outputDirectory>conf</outputDirectory>    
  36.             <includes>    
  37.                 <include>*.xml</include>    
  38.                 <include>*.properties</include>  
  39.                 <include>*.key</include>   
  40.             </includes>    
  41.         </fileSet>    
  42.             
  43.         <!-- 把项目的脚本文件目录( src/main/scripts )中的启动脚本文件,打包进zip文件的跟目录 -->    
  44.         <fileSet>    
  45.             <directory>${project.build.scriptSourceDirectory}</directory>    
  46.             <outputDirectory></outputDirectory>    
  47.             <includes>    
  48.                 <include>startup.*</include>    
  49.             </includes>    
  50.         </fileSet>    
  51.     
  52.         <!-- 把项目自己编译出来的jar文件,打包进zip文件的根目录 -->    
  53.         <fileSet>    
  54.             <directory>${project.build.directory}</directory>    
  55.             <outputDirectory></outputDirectory>    
  56.             <includes>    
  57.                 <include>*.jar</include>    
  58.             </includes>    
  59.         </fileSet>    
  60.     </fileSets>    
  61. </assembly>  


(2)pom.xml [html] view plain copy
  1. <build>    
  2.       <plugins>  
  3.         <plugin>  
  4.               <groupId>org.apache.maven.plugins</groupId>  
  5.               <artifactId>maven-compiler-plugin</artifactId>  
  6.               <version>3.1</version>  
  7.               <configuration>  
  8.                   <compilerVersion>1.6</compilerVersion>  
  9.                   <source>1.6</source>  
  10.                   <target>1.6</target>  
  11.               </configuration>  
  12.          </plugin>    
  13.           <!-- The configuration of maven-jar-plugin -->    
  14.         <plugin>    
  15.             <groupId>org.apache.maven.plugins</groupId>    
  16.             <artifactId>maven-jar-plugin</artifactId>    
  17.             <version>2.4</version>    
  18.             <!-- The configuration of the plugin -->    
  19.             <configuration>    
  20.                 <!-- Configuration of the archiver -->    
  21.                 <archive>    
  22.     
  23.                     <!-- do not include pom.xml and pom.properties in the jar package -->    
  24.                     <addMavenDescriptor>false</addMavenDescriptor>    
  25.     
  26.                     <!-- Manifest specific configuration -->    
  27.                     <manifest>    
  28.                       
  29.                         <!-- put third party jar package into the classpath of manifest -->    
  30.                         <addClasspath>true</addClasspath>    
  31.                         <!-- the prefix of the jar items in the classpath, it depends on the location(folder) of jar files -->    
  32.                         <classpathPrefix>lib/</classpathPrefix>    
  33.                         <!-- main class of the jar package-->    
  34.                         <mainClass>com.tang.your-Main-class</mainClass>    
  35.                     </manifest>    
  36.                 </archive>    
  37.                 <!-- excludes some files -->    
  38.                 <excludes>    
  39.                     <exclude>${project.basedir}/xml/*</exclude>    
  40.                 </excludes>    
  41.             </configuration>    
  42.         </plugin>    
  43.     
  44.         <!-- The configuration of maven-assembly-plugin -->    
  45.         <plugin>    
  46.             <groupId>org.apache.maven.plugins</groupId>    
  47.             <artifactId>maven-assembly-plugin</artifactId>    
  48.             <version>2.4</version>    
  49.             <!-- The configuration of the plugin -->    
  50.             <configuration>    
  51.                 <!-- Specifies the configuration file of the assembly plugin -->    
  52.                 <descriptors>    
  53.                     <descriptor>conf/package.xml</descriptor>    
  54.                 </descriptors>    
  55.             </configuration>    
  56.             <executions>    
  57.                 <execution>    
  58.                     <id>make-assembly</id>    
  59.                     <phase>package</phase>    
  60.                     <goals>    
  61.                         <goal>single</goal>    
  62.                     </goals>    
  63.                 </execution>    
  64.             </executions>    
  65.         </plugin>     
  66.       </plugins>    
  67.   </build>    

mvn install后,此时会在target目录下生成一个zip文件,如:
 youArtifactId-0.0.1-bin.zip 解压后有: conf 文件夹, lib文件夹,  youArtifactId-0.0.1-bin.jar