码迷,mamicode.com
首页 > 编程语言 > 详细

mvn生成runnablejar 的方法

时间:2015-10-12 10:42:13      阅读:878      评论:0      收藏:0      [点我收藏+]

标签:

1、生成runnable jar,但是最后并不是一个可执行的jar 而是一个目录

主要使用 三个插件 将所需要的资源拷贝到 /target/Crunchify目录下,注意并没有生成一个jar中去

maven-resources-pluginmaven-dependency-plugin & maven-jar-plugin to generate your complete executable Jar Project? As a result it creates / copies all required files to /target/Crunchify folder.

技术分享

几点说明:

  1. CrunchifyMavenBuildPlugins is a Maven Project. If you have Java project and wanted to convert it into Maven project then follow this tutorial.
  2. We do have two folders. src and resources.
  3. Inside resources folder we do have a folder called Scripts which contains one executableshell script file.
  4. CrunchifyMain.java is a main starting point which has main(String args[]) method inside.
  5. pom.xml file in which we will add Maven Plugins which will build executable .jar project with all included dependancies

pom文件如下:注意build标签下的3个plugins<build>

 

 1   <pluginManagement>
 2             <plugins>
 3                 <plugin>
 4                     <groupId>org.apache.maven.plugins</groupId>
 5                     <artifactId>maven-compiler-plugin</artifactId>
 6                     <version>2.3.1</version>
 7                     <configuration>
 8                         <source>1.7</source>
 9                         <target>1.7</target>
10                     </configuration>
11                 </plugin>
12             </plugins>
13         </pluginManagement>
14         <plugins>


<!--说明:The Resources Plugin 会将project resources拷贝到输出目录.project resources are the resources associated to the main source code-->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
 <executions>
 <execution>
 <id>copy-resources</id>
<phase>validate</phase>
<goals>
 <goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/Crunchify</outputDirectory>
 <resources>
<resource>
<directory>resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
 
  
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-dependency-plugin</artifactId>
 <!--说明:The dependency plugin provides the capability to manipulate artifacts. It can copy and/or unpack artifacts from local or remote repositories to a specified location-->
                <executions>
                     <execution>
                         <id>copy-dependencies</id>
                         <phase>prepare-package</phase>
                         <goals>
                             <goal>copy-dependencies</goal>
                         </goals>
                        <configuration>
                             <outputDirectory>${project.build.directory}/Crunchify/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>
 <!-- This plugin provides the capability to build and sign jars.-->
                 <configuration>
                     <archive>
                         <manifest>
                             <addClasspath>true</addClasspath>
                             <classpathPrefix>lib/</classpathPrefix>
                             <mainClass>com.crunchify.tutorial.CrunchifyMain</mainClass>
                         </manifest>
                         <manifestEntries>
                             <Class-Path>.</Class-Path>
                         </manifestEntries>
                     </archive>
  
                     <finalName>Crunchify/Crunchify</finalName>
                 </configuration>
             </plugin>
         </plugins>
     </build>

 最后看看结果

技术分享

2、生成到一个jar包中,这个时候需要在插件maven-shade-plugin上做文章,主要用到的技术是 Resource Transformers

技术分享

主要看下pom文件

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3     <modelVersion>4.0.0</modelVersion>
  4     <groupId>CrunchifyMavenBuildPlugins</groupId>
  5     <artifactId>CrunchifyMavenBuildPlugins</artifactId>
  6     <version>0.0.1-SNAPSHOT</version>
  7     <name>CrunchifyMavenBuildPlugins</name>
  8  
  9     <properties>
 10         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 11     </properties>
 12  
 13     <dependencies>
 14         <dependency>
 15             <groupId>log4j</groupId>
 16             <artifactId>log4j</artifactId>
 17             <version>1.2.17</version>
 18         </dependency>
 19         <dependency>
 20             <groupId>com.googlecode.json-simple</groupId>
 21             <artifactId>json-simple</artifactId>
 22             <version>1.1</version>
 23         </dependency>
 24         <dependency>
 25             <groupId>axis</groupId>
 26             <artifactId>axis</artifactId>
 27             <version>1.4</version>
 28         </dependency>
 29         <dependency>
 30             <groupId>commons-beanutils</groupId>
 31             <artifactId>commons-beanutils</artifactId>
 32             <version>1.8.3</version>
 33         </dependency>
 34         <dependency>
 35             <groupId>commons-collections</groupId>
 36             <artifactId>commons-collections</artifactId>
 37             <version>3.2.1</version>
 38         </dependency>
 39         <dependency>
 40             <groupId>commons-configuration</groupId>
 41             <artifactId>commons-configuration</artifactId>
 42             <version>1.10</version>
 43         </dependency>
 44         <dependency>
 45             <groupId>commons-io</groupId>
 46             <artifactId>commons-io</artifactId>
 47             <version>2.4</version>
 48         </dependency>
 49         <dependency>
 50             <groupId>javax.mail</groupId>
 51             <artifactId>mail</artifactId>
 52             <version>1.4.7</version>
 53         </dependency>
 54         <dependency>
 55             <groupId>javax.servlet</groupId>
 56             <artifactId>servlet-api</artifactId>
 57             <version>2.5</version>
 58         </dependency>
 59         <dependency>
 60             <groupId>org.json</groupId>
 61             <artifactId>json</artifactId>
 62             <version>20140107</version>
 63         </dependency>
 64         <dependency>
 65             <groupId>axis</groupId>
 66             <artifactId>axis-saaj</artifactId>
 67             <version>1.4</version>
 68         </dependency>
 69         <dependency>
 70             <groupId>wsdl4j</groupId>
 71             <artifactId>wsdl4j</artifactId>
 72             <version>1.6.3</version>
 73         </dependency>
 74         <dependency>
 75             <groupId>com.google.zxing</groupId>
 76             <artifactId>core</artifactId>
 77             <version>2.0</version>
 78         </dependency>
 79     </dependencies>
 80  
 81     <build>
 82         <pluginManagement>
 83             <plugins>
 84                 <plugin>
 85                     <groupId>org.apache.maven.plugins</groupId>
 86                     <artifactId>maven-compiler-plugin</artifactId>
 87                     <version>2.3.1</version>
 88                     <configuration>
 89                         <source>1.7</source>
 90                         <target>1.7</target>
 91                     </configuration>
 92                 </plugin>
 93             </plugins>
 94         </pluginManagement>
 95         <plugins>
 96             <plugin>
 97                 <groupId>org.apache.maven.plugins</groupId>
 98                 <artifactId>maven-shade-plugin</artifactId>
<!-- 主要使用了Resource Transformers 做 Aggregating classes/resourcesfrom several artifacts into one jar-->
99 <version>1.7</version> 100 <executions> 101 <execution> 102 <phase>package</phase> 103 <goals> 104 <goal>shade</goal> 105 </goals> 106 <configuration> 107 <!-- Optional Start --> 108 <finalName>Crunchify</finalName> 109 <shadedArtifactAttached>true</shadedArtifactAttached> 110 <shadedClassifierName>jar-with-dependencies</shadedClassifierName> 111 <!-- Optional End --> 112 113 <transformers> 114 <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> 115 <mainClass>com.crunchify.tutorial.CrunchifyMain</mainClass> 116 </transformer> 117 <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> 118 <resource>META-INF/spring.handlers</resource> 119 </transformer> 120 <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> 121 <resource>META-INF/spring.schemas</resource> 122 </transformer> 123 <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> 124 <resource>META-INF/spring.tooling</resource> 125 </transformer> 126 </transformers> 127 </configuration> 128 </execution> 129 </executions> 130 </plugin>
131 132 </plugins> 133 </build> 134 </project>

 

mvn生成runnablejar 的方法

标签:

原文地址:http://www.cnblogs.com/amazement/p/4870833.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!