使用Maven assembly打多个自定义包及War包简介

一.Maven基本命令

创建java项目:
mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.mycompany.app -DartifactId=myProject

创建web项目:
mvn archetype:create -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=com.mycompany.app  -DartifactId=myProject

增加eclipse项目信息:
cd myProject
mvn eclipse:eclipse

编译:
mvn compile

运行测试:
mvn test

打包(java项目打出jar;web项目打出war):
mvn package


安装jar到本地仓库:
mvn install


清除生成的文件:
mvn clean

========================================================
War 简单介绍
Xml代码   收藏代码
  1. <plugin>  
  2.                 <groupId>org.apache.maven.plugins</groupId>  
  3.                 <artifactId>maven-war-plugin</artifactId>  
  4.                 <configuration>  
  5.                     <!-- 释放将项目的类文件打成jar放到lib目录中。  
  6.                 打成jar的好处是:只修改class时,可以只更新jar。 -->  
  7.                     <archiveClasses>true</archiveClasses>  
  8.                     <webResources>  
  9.                         <!--  
  10.                             src/main/resources是默认的资源目录。  
  11.                             当archiveClasses=true时,resources中的文件资源会被打到jar中;  
  12.                             当archiveClasses=false时,resources中的文件资源在war的根目录。  
  13.                         -->  
  14.                         <resource>  
  15.                             <!-- config作为source folder,不会被打到jar中。 -->  
  16.                             <directory>src/main/config</directory>  
  17.                             <targetPath>WEB-INF/classes</targetPath>  
  18.                             <filtering>true</filtering>  
  19.                         </resource>  
  20.                     </webResources>  
  21.                     <archive>  
  22.                         <manifest>  
  23.                             <addDefaultImplementationEntries>true</addDefaultImplementationEntries>  
  24.                             <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>  
  25.                         </manifest>  
  26.                         <manifestEntries>  
  27.                             <Built-On>${timestamp}</Built-On>  
  28.                         </manifestEntries>  
  29.                     </archive>  
  30.                 </configuration>  
  31.                 <executions>  
  32.                     <execution>  
  33.                         <id>generate-manifest</id>  
  34.                         <phase>prepare-package</phase>  
  35.                         <goals>  
  36.                             <goal>manifest</goal>  
  37.                         </goals>  
  38.                     </execution>  
  39.                 </executions>  
  40.             </plugin>  

需要的属性配置:
Xml代码   收藏代码
  1. <properties>  
  2.         <maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format>  
  3.         <timestamp>${maven.build.timestamp}</timestamp>  
  4. </properties>  


========================================================
Assembly打多个自定义包

首先了解两个参数:构建目录/最终项目构建名称
${project.build.directory}/${project.build.finalName}
对应target\myProject

打多个包
每个包有自己的执行器,都绑定到某个生命周期上,有各自的描述符文件。
Pom中配置如下:
Xml代码   收藏代码
  1. <plugin>  
  2.                 <groupId>org.apache.maven.plugins</groupId>  
  3.                 <artifactId>maven-assembly-plugin</artifactId>  
  4.                 <version>2.2-beta-5</version>  
  5.                 <executions>  
  6.                     <execution>  
  7.                         <id>make-jar</id><!-- 随便一个名字 -->  
  8.                         <phase>package</phase><!-- 绑定到package生命周期阶段上 -->  
  9.                         <goals>  
  10.                             <goal>single</goal><!-- 只运行一次 -->  
  11.                         </goals>  
  12.                         <configuration>  
  13.                             <skipAssembly>false</skipAssembly><!-- 跳过这个执行器 -->  
  14.                             <!-- 自定义输出目录(finalName为最终项目构建名称) -->  
  15.                             <!--  
  16.                                 <outputDirectory>  
  17.                                 ${project.build.directory}/${project.build.finalName}/WEB-INF/lib  
  18.                                 </outputDirectory>  
  19.                             -->  
  20.                             <!-- 描述符文件 -->  
  21.                             <descriptors>  
  22.                                 <descriptor>src/main/assembly/jar.xml</descriptor>  
  23.                             </descriptors>  
  24.                         </configuration>  
  25.                     </execution>  
  26.                                         <execution>  
  27.                         <id>make-zip</id>  
  28.                         <phase>package</phase>  
  29.                         <goals>  
  30.                             <goal>single</goal>  
  31.                         </goals>  
  32.                         <configuration>  
  33.                             <skipAssembly>true</skipAssembly>  
  34.                             <descriptors>  
  35.                                 <descriptor>src/main/assembly/zip.xml</descriptor>  
  36.                             </descriptors>  
  37.                         </configuration>  
  38.                     </execution>  
  39.   
  40.                 </executions>  
  41.             </plugin>  


jar.xml文件
Xml代码   收藏代码
  1. <assembly  
  2.     xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">  
  5.     <id>jar-release</id>  
  6.     <formats>  
  7.         <format>jar</format>  
  8.     </formats>  
  9.     <includeBaseDirectory>false</includeBaseDirectory>  
  10.     <fileSets>  
  11.         <fileSet>  
  12.             <directory>${project.build.directory}/classes</directory>  
  13.             <includes>  
  14.                 <include>**</include>  
  15.             </includes>  
  16.             <excludes>  
  17.                 <exclude>server.properties</exclude>  
  18.             </excludes>  
  19.             <outputDirectory>/</outputDirectory>  
  20.         </fileSet>  
  21.     </fileSets>  
  22. </assembly>  


zip.xml文件
Xml代码   收藏代码
  1. <assembly  
  2.     xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">  
  5.       
  6.     <id>release</id>  
  7.     <formats>  
  8.         <format>zip</format>  
  9.     </formats>  
  10.     <includeBaseDirectory>false</includeBaseDirectory>    
  11.     <fileSets>  
  12.         <fileSet>  
  13.             <directory>${project.build.directory}/${project.build.finalName}</directory>  
  14.             <includes>  
  15.                 <include>**</include>  
  16.             </includes>     
  17.             <outputDirectory>/project</outputDirectory>  
  18.         </fileSet>  
  19.           
  20.     </fileSets>  
  21. </assembly> 

你可能感兴趣的:(使用Maven assembly打多个自定义包及War包简介)