`
sundful
  • 浏览: 1232165 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

maven 打包可执行jar的方法

 
阅读更多

1.修改pom.xml增加如下内容

[html] view plaincopy
  1. <plugin>  
  2.     <groupId>org.apache.maven.plugins</groupId>  
  3.     <artifactId>maven-jar-plugin</artifactId>  
  4.     <version>2.4</version>  
  5.     <configuration>  
  6.         <archive>  
  7.             <manifest>  
  8.                 <addClasspath>true</addClasspath>  
  9.                 <classpathPrefix>lib/</classpathPrefix>  
  10.                 <mainClass>com.sysware.HelloWorld</mainClass>  
  11.             </manifest>  
  12.         </archive>  
  13.     </configuration>  
  14. </plugin>  

运行mvn clean package即可

2.在pom.xml增加如下内容

[html] view plaincopy
  1. <plugin>  
  2.     <groupId>org.apache.maven.plugins</groupId>  
  3.     <artifactId>maven-assembly-plugin</artifactId>  
  4.     <version>2.3</version>  
  5.     <configuration>  
  6.         <appendAssemblyId>false</appendAssemblyId>  
  7.         <descriptorRefs>  
  8.             <descriptorRef>jar-with-dependencies</descriptorRef>  
  9.         </descriptorRefs>  
  10.         <archive>  
  11.             <manifest>  
  12.                 <mainClass>com.juvenxu.mvnbook.helloworld.HelloWorld</mainClass>  
  13.             </manifest>  
  14.         </archive>  
  15.     </configuration>  
  16.     <executions>  
  17.         <execution>  
  18.             <id>make-assembly</id>  
  19.             <phase>package</phase>  
  20.             <goals>  
  21.                 <goal>assembly</goal>  
  22.             </goals>  
  23.         </execution>  
  24.     </executions>  
  25. </plugin>  

运行mvn assembly:assembly

 

 

3.

 

[html] view plaincopy
  1. <build>  
  2.         <finalName>...</finalName>  
  3.         <sourceDirectory>src/main/java</sourceDirectory>  
  4.         <resources>  
  5.             <!-- 控制资源文件的拷贝 -->  
  6.             <resource>  
  7.                 <directory>src/main/resources</directory>  
  8.                 <targetPath>${project.build.directory}</targetPath>  
  9.             </resource>  
  10.         </resources>  
  11.         <plugins>  
  12.             <!-- 设置源文件编码方式 -->  
  13.             <plugin>  
  14.                 <groupId>org.apache.maven.plugins</groupId>  
  15.                 <artifactId>maven-compiler-plugin</artifactId>  
  16.                 <configuration>  
  17.                     <defaultLibBundleDir>lib</defaultLibBundleDir>  
  18.                     <source>1.6</source>  
  19.                     <target>1.6</target>  
  20.                     <encoding>UTF-8</encoding>  
  21.                 </configuration>  
  22.             </plugin>  
  23.             <!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->  
  24.             <plugin>  
  25.                 <groupId>org.apache.maven.plugins</groupId>  
  26.                 <artifactId>maven-jar-plugin</artifactId>  
  27.                 <configuration>  
  28.                     <archive>  
  29.                         <manifest>  
  30.                             <addClasspath>true</addClasspath>  
  31.                             <classpathPrefix>lib/</classpathPrefix>  
  32.                             <mainClass>.....MonitorMain</mainClass>  
  33.                         </manifest>  
  34.                     </archive>  
  35.                 </configuration>  
  36.             </plugin>  
  37.             <!-- 拷贝依赖的jar包到lib目录 -->  
  38.             <plugin>  
  39.                 <groupId>org.apache.maven.plugins</groupId>  
  40.                 <artifactId>maven-dependency-plugin</artifactId>  
  41.                 <executions>  
  42.                     <execution>  
  43.                         <id>copy</id>  
  44.                         <phase>package</phase>  
  45.                         <goals>  
  46.                             <goal>copy-dependencies</goal>  
  47.                         </goals>  
  48.                         <configuration>  
  49.                             <outputDirectory>  
  50.                                 ${project.build.directory}/lib  
  51.                             </outputDirectory>  
  52.                         </configuration>  
  53.                     </execution>  
  54.                 </executions>  
  55.             </plugin>  
  56.             <!-- 解决资源文件的编码问题 -->  
  57.             <plugin>  
  58.                 <groupId>org.apache.maven.plugins</groupId>  
  59.                 <artifactId>maven-resources-plugin</artifactId>  
  60.                 <version>2.3</version>  
  61.                 <configuration>  
  62.                     <encoding>UTF-8</encoding>  
  63.                 </configuration>  
  64.             </plugin>  
  65.             <!-- 打包source文件为jar文件 -->  
  66.             <plugin>  
  67.                 <artifactId>maven-source-plugin</artifactId>  
  68.                 <version>2.1</version>  
  69.                 <configuration>  
  70.                     <attach>true</attach>  
  71.                     <encoding>UTF-8</encoding>  
  72.                 </configuration>  
  73.                 <executions>  
  74.                     <execution>  
  75.                         <phase>compile</phase>  
  76.                         <goals>  
  77.                             <goal>jar</goal>  
  78.                         </goals>  
  79.                     </execution>  
  80.                 </executions>  
  81.             </plugin>  
  82.         </plugins>  
  83.     </build>  

 

4.

 

[html] view plaincopy
  1. <build>  
  2.     <resources>  
  3.         <resource>  
  4.             <targetPath>${project.build.directory}/classes</targetPath>  
  5.             <directory>src/main/resources</directory>  
  6.             <filtering>true</filtering>  
  7.             <includes>  
  8.                 <include>**/*.xml</include>  
  9.             </includes>  
  10.         </resource>  
  11.     </resources>  
  12.     <plugins>  
  13.         <plugin>  
  14.             <groupId>org.apache.maven.plugins</groupId>  
  15.             <artifactId>maven-compiler-plugin</artifactId>  
  16.             <version>3.0</version>  
  17.             <configuration>  
  18.                 <source>1.6</source>  
  19.                 <target>1.6</target>  
  20.                 <encoding>UTF-8</encoding>  
  21.             </configuration>  
  22.         </plugin>  
  23.         <plugin>  
  24.             <groupId>org.apache.maven.plugins</groupId>  
  25.             <artifactId>maven-shade-plugin</artifactId>  
  26.             <version>2.0</version>  
  27.             <executions>  
  28.                 <execution>  
  29.                     <phase>package</phase>  
  30.                     <goals>  
  31.                         <goal>shade</goal>  
  32.                     </goals>  
  33.                     <configuration>  
  34.                         <transformers>  
  35.                             <transformer  
  36.                                 implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
  37.                                 <mainClass>com.test.testguava.app.App</mainClass>  
  38.                             </transformer>  
  39.                             <transformer  
  40.                                 implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">  
  41.                                 <resource>applicationContext.xml</resource>  
  42.                             </transformer>  
  43.                         </transformers>  
  44.                         <shadedArtifactAttached>true</shadedArtifactAttached>  
  45.                         <shadedClassifierName>executable</shadedClassifierName>  
  46.                     </configuration>  
  47.                 </execution>  
  48.             </executions>  
  49.         </plugin>  
  50.     </plugins>  
  51. </build>  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics