[color=red]使用maven-war-plugin 对Maven项目进行动态打包[/color] [url]http://nileader.blog.51cto.com/1381108/449956[/url]
如何把配置文件打包到jar中 [url][/url]
maven打包总结 [url][/url]
maven打jar包 [url]http://hy90171.iteye.com/blog/1916293[/url]
使用maven打出独立应用程序的jar包 [url]http://pipilu.iteye.com/blog/399816[/url]
[color=red]maven生成war包的两种方式[/color][url]http://touchfu.iteye.com/blog/545708[/url]
[color=red][b]Maven 是怎样创建War 包?[/b][/color] [url][/url]


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

[size=x-large][color=red]1. 打包jar时,想排除所有的resource文件[/color][/size]

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>
                            <classifier>lib</classifier>
                            <excludes>
                                <exclude>*Main*</exclude>
                            </excludes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>


maven 生成的jar文件指定main class [url]http://qinglangee.iteye.com/blog/709961[/url]


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>zhch.illq.time</groupId>
  <artifactId>time_tool</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>time_tool</name>
  <url>http://maven.apache.org</url>
  <!-- 指定属性 -->
  <properties>
    <cxf.version>2.2.8</cxf.version>
    <spring.version>3.0.2.RELEASE</spring.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<configuration>
					<archive>
						<manifest>
							<mainClass>zhch.illq.time.TimeTool</mainClass>
						</manifest>
					</archive>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>


jar 命令:


假设现在是在classes目录的同级目录中


[color=darkblue]jar cvef zhch.illq.time.TimeTool timetool.jar -C classes . [/color]


-C 是指定要打包的目录


. (点) 是文件,表示当前目录,这样就把classes目录中及子目录中所有文件打包


参数e f 分别指定main class和生成的jar文件名, 后面要按顺序来


也可以写成这样


[color=darkblue]jar cvfe timetool.jar zhch.illq.time.TimeTool -C classes . [/color]



[b]maven 将代码打成可执行jar包[/b] [url][/url]


在Eclipse中编写代码,如果没有用到第三方jar可以直接export成jar包,但是如果用到第三方jar包,就需要手动将拷贝依赖的jar包,也可以编写ant脚本自动打包。更方便的是使用maven,现在maven管理项目很方便,如下面将自己编写的类打成可执行jar包,并自动拷贝依赖的jar包。 下面是pom.xml文件:


<project xmlns="http://maven.apache.org/POM/4.0.0"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0           
    http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.yeetrack</groupId>
    <artifactId>httpclient</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>httpclient</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.2.3</version>
            </dependency>
    </dependencies>

    <build>
        <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <configuration>
                        <archive>
                    <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                    <!--指明main方法所在的类-->
                            <mainClass>com.baidu.httpclient.SVNTest</mainClass>
                    </manifest>
                        </archive>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <executions>
                            <execution>
                        <id>copy</id>
                        <phase>package</phase>
                        <goals>
                         <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>
                            ./target/lib
                         </outputDirectory>
                         </configuration>
                            </execution>
                    </executions>
                </plugin>
        </plugins>
    </build>
</project>


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




[size=x-large][color=red]2. 打war包时排除resource[/color][/size]


<build>   
  <plugins>
   <plugin>   
    <groupId>org.apache.maven.plugins</groupId>   
    <artifactId>maven-war-plugin</artifactId>   
    <version>2.0.2</version>   
    <configuration>   
     <warSourceExcludes>src/main/resources/**</warSourceExcludes>   
    </configuration>   
   </plugin>   
  </plugins>   
 </build>



打包时过滤和包含文件例子


<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<configuration>
					<escapeString>\</escapeString>
					<warName>${war.name}-${version.num}</warName>
					<warSourceExcludes>src/main/resources/packaged/**</warSourceExcludes>
					<webResources>
						<resource>
							<directory>src/main/resources/packaged</directory>
							<targetPath>WEB-INF/classes</targetPath>
							<filtering>true</filtering>
							<includes>
								<include>hibernate.properties</include>
							</includes>
						</resource>
                        <resource>
                            <directory>src/main/resources/packaged/tmp</directory>
                            <targetPath>WEB-INF/classes</targetPath>
                            <filtering>true</filtering>
                            <includes>
                                <include>applicationContext-init.xml</include>
                            </includes>
                        </resource>
					</webResources>
                    <warSourceDirectory>src/main/webapp</warSourceDirectory>
                    <webXml>src/main/resources/packaged/tmp/web.xml</webXml>
				</configuration>
			</plugin>



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


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