使用maven打包jar或者war

使用maven-war-plugin 对Maven项目进行动态打包   http://nileader.blog.51cto.com/1381108/449956  
如何把配置文件打包到jar中  http://blog.csdn.net/ciedecem/article/details/10382275  
maven打包总结  http://blog.csdn.net/fireofjava/article/details/14447325  
maven打jar包  http://hy90171.iteye.com/blog/1916293  
使用maven打出独立应用程序的jar包  http://pipilu.iteye.com/blog/399816  
maven生成war包的两种方式 http://touchfu.iteye.com/blog/545708  
Maven 是怎样创建War 包?   http://my.oschina.net/u/939534/blog/173863  


打包参数: 
mvn package -P saas -Dwar.name=dfxd -Ddb.name=dfxd 
注意:D参数可以使用多次,匹配pom.xml里面定义的属性。 

1. 打包jar时,想排除所有的resource文件  
Xml代码   收藏代码
  1. <plugin>  
  2.                 <groupId>org.apache.maven.plugins</groupId>  
  3.                 <artifactId>maven-jar-plugin</artifactId>  
  4.                 <executions>  
  5.                     <execution>  
  6.                         <phase>package</phase>  
  7.                         <goals>  
  8.                             <goal>jar</goal>  
  9.                         </goals>  
  10.                         <configuration>  
  11.                             <classifier>lib</classifier>  
  12.                             <excludes>  
  13.                                 <exclude>*Main*</exclude>  
  14.                             </excludes>  
  15.                         </configuration>  
  16.                     </execution>  
  17.                 </executions>  
  18.             </plugin>  

maven 生成的jar文件指定main class  http://qinglangee.iteye.com/blog/709961  
Xml代码   收藏代码
  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/maven-v4_0_0.xsd">  
  3.   <modelVersion>4.0.0</modelVersion>  
  4.   <groupId>zhch.illq.time</groupId>  
  5.   <artifactId>time_tool</artifactId>  
  6.   <packaging>jar</packaging>  
  7.   <version>1.0-SNAPSHOT</version>  
  8.   <name>time_tool</name>  
  9.   <url>http://maven.apache.org</url>  
  10.   <!-- 指定属性 -->  
  11.   <properties>  
  12.     <cxf.version>2.2.8</cxf.version>  
  13.     <spring.version>3.0.2.RELEASE</spring.version>  
  14.   </properties>  
  15.   <dependencies>  
  16.     <dependency>  
  17.       <groupId>junit</groupId>  
  18.       <artifactId>junit</artifactId>  
  19.       <version>3.8.1</version>  
  20.       <scope>test</scope>  
  21.     </dependency>  
  22.   </dependencies>  
  23.   <build>  
  24.         <plugins>  
  25.             <plugin>  
  26.                 <groupId>org.apache.maven.plugins</groupId>  
  27.                 <artifactId>maven-jar-plugin</artifactId>  
  28.                 <configuration>  
  29.                     <archive>  
  30.                         <manifest>  
  31.                             <mainClass>zhch.illq.time.TimeTool</mainClass>  
  32.                         </manifest>  
  33.                     </archive>  
  34.                 </configuration>  
  35.             </plugin>  
  36.         </plugins>  
  37.     </build>  
  38.     
  39. </project>  

jar 命令: 
假设现在是在classes目录的同级目录中  
jar cvef zhch.illq.time.TimeTool timetool.jar -C classes .  
-C 是指定要打包的目录 
. (点) 是文件,表示当前目录,这样就把classes目录中及子目录中所有文件打包 
参数e f 分别指定main class和生成的jar文件名, 后面要按顺序来 
也可以写成这样  
jar cvfe timetool.jar zhch.illq.time.TimeTool -C classes .  

maven 将代码打成可执行jar包   http://my.oschina.net/u/147181/blog/164938  
在Eclipse中编写代码,如果没有用到第三方jar可以直接export成jar包,但是如果用到第三方jar包,就需要手动将拷贝依赖的jar包,也可以编写ant脚本自动打包。更方便的是使用maven,现在maven管理项目很方便,如下面将自己编写的类打成可执行jar包,并自动拷贝依赖的jar包。 下面是pom.xml文件: 
Xml代码   收藏代码
  1. <project xmlns="http://maven.apache.org/POM/4.0.0"    
  2.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  3.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0             
  4.     http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  5.     <modelVersion>4.0.0</modelVersion>  
  6.   
  7.     <groupId>com.yeetrack</groupId>  
  8.     <artifactId>httpclient</artifactId>  
  9.     <version>0.0.1-SNAPSHOT</version>  
  10.     <packaging>jar</packaging>  
  11.   
  12.     <name>httpclient</name>  
  13.     <url>http://maven.apache.org</url>  
  14.   
  15.     <properties>  
  16.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  17.     </properties>  
  18.     <dependencies>  
  19.             <dependency>  
  20.                 <groupId>junit</groupId>  
  21.                 <artifactId>junit</artifactId>  
  22.                 <version>3.8.1</version>  
  23.                 <scope>test</scope>  
  24.             </dependency>  
  25.             <dependency>  
  26.                 <groupId>org.apache.httpcomponents</groupId>  
  27.                 <artifactId>httpclient</artifactId>  
  28.                 <version>4.2.3</version>  
  29.             </dependency>  
  30.     </dependencies>  
  31.   
  32.     <build>  
  33.         <plugins>  
  34.                 <plugin>  
  35.                     <groupId>org.apache.maven.plugins</groupId>  
  36.                     <artifactId>maven-jar-plugin</artifactId>  
  37.                     <configuration>  
  38.                         <archive>  
  39.                     <manifest>  
  40.                             <addClasspath>true</addClasspath>  
  41.                             <classpathPrefix>lib/</classpathPrefix>  
  42.                     <!--指明main方法所在的类-->  
  43.                             <mainClass>com.baidu.httpclient.SVNTest</mainClass>  
  44.                     </manifest>  
  45.                         </archive>  
  46.                     </configuration>  
  47.                 </plugin>  
  48.                 <plugin>  
  49.                     <groupId>org.apache.maven.plugins</groupId>  
  50.                     <artifactId>maven-dependency-plugin</artifactId>  
  51.                     <executions>  
  52.                             <execution>  
  53.                         <id>copy</id>  
  54.                         <phase>package</phase>  
  55.                         <goals>  
  56.                          <goal>copy-dependencies</goal>  
  57.                         </goals>  
  58.                         <configuration>  
  59.                             <outputDirectory>  
  60.                             ./target/lib  
  61.                          </outputDirectory>  
  62.                          </configuration>  
  63.                             </execution>  
  64.                     </executions>  
  65.                 </plugin>  
  66.         </plugins>  
  67.     </build>  
  68. </project>  

执行 maven install就会在项目target文件夹下生成jar包,和依赖的jar包(在lib文件夹中),直接运行  java -jar *.jar 即可。 


2. 打war包时排除resource  
Xml代码   收藏代码
  1. <build>     
  2.   <plugins>  
  3.    <plugin>     
  4.     <groupId>org.apache.maven.plugins</groupId>     
  5.     <artifactId>maven-war-plugin</artifactId>     
  6.     <version>2.0.2</version>     
  7.     <configuration>     
  8.      <warSourceExcludes>src/main/resources/**</warSourceExcludes>     
  9.     </configuration>     
  10.    </plugin>     
  11.   </plugins>     
  12.  </build>  


打包时过滤和包含文件例子 
Xml代码   收藏代码
  1. <plugin>  
  2.                 <groupId>org.apache.maven.plugins</groupId>  
  3.                 <artifactId>maven-war-plugin</artifactId>  
  4.                 <configuration>  
  5.                     <escapeString>\</escapeString>  
  6.                     <warName>${war.name}-${version.num}</warName>  
  7.                     <warSourceExcludes>src/main/resources/packaged/**</warSourceExcludes>  
  8.                     <webResources>  
  9.                         <resource>  
  10.                             <directory>src/main/resources/packaged</directory>  
  11.                             <targetPath>WEB-INF/classes</targetPath>  
  12.                             <filtering>true</filtering>  
  13.                             <includes>  
  14.                                 <include>hibernate.properties</include>  
  15.                             </includes>  
  16.                         </resource>  
  17.                         <resource>  
  18.                             <directory>src/main/resources/packaged/tmp</directory>  
  19.                             <targetPath>WEB-INF/classes</targetPath>  
  20.                             <filtering>true</filtering>  
  21.                             <includes>  
  22.                                 <include>applicationContext-init.xml</include>  
  23.                             </includes>  
  24.                         </resource>  
  25.                     </webResources>  
  26.                     <warSourceDirectory>src/main/webapp</warSourceDirectory>  
  27.                     <webXml>src/main/resources/packaged/tmp/web.xml</webXml>  
  28.                 </configuration>  
  29.             </plugin>  


注意:pom.xml定义的一些属性,是可以被使用到applicationContext.xml里面的, 

比如:pom.xml存在<hibernate.dialect>org.hibernate.dialect.SQLServerDialect</hibernate.dialect>,然后jdbc.properties存在hibernate.dialect=${hibernate.dialect},applicationContext.xml存在<prop key="hibernate.dialect">${hibernate.dialect}</prop>,那么打包得到的war,applicationContext.xml会变为:<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>


原文:http://panyongzheng.iteye.com/blog/2013070

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值