之前一直是用ant进行打包,使用过的人都知道,其灵活性是不用置疑的。
现在公司新建的工程序都采用一maven来打包了,以于java或web工程来说,用maven打包也是很方便的,特别是其对jar包依赖的管理。但我现在的需求是打一个特殊的包,非java也非web工程序。
需求是这样的:对tomcat过行打包,并且附带一安装脚本,目录结构如下
其中pom.xml和tar.xml是打包时maven要使用的文件,主要是想将目录apache-tomcat-6.0.41和install.sh文件打包到一下后辍名为.tar.gz的压缩包中。
最开始无从下手,后来在网上搜了一把,发现有个叫maven-assembly-plugin的插件可以实现自定义打包,其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>server</groupId>
<artifactId>tomcat</artifactId>
<version>6</version>
<!-- <packaging>jar</packaging> -->
<name>tomcat</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.build.timestamp.format>yyyyMMdd</maven.build.timestamp.format>
<buildTime>${maven.build.timestamp}</buildTime>
<version>6</version>
<createTime>${buildNumber}</createTime>
</properties>
<build>
<plugins>
<plugin>
<groupId>com.google.code.maven-svn-revision-number-plugin</groupId>
<artifactId>maven-svn-revision-number-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<!-- 这个阶段应该写maven打包之前的生命周期,否则打包时候获取不到版本号 -->
<phase>validate</phase>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
<configuration>
<entries>
<entry>
<prefix>SVN</prefix>
</entry>
</entries>
</configuration>
<!-- svn版本1.7以上才需要配这个依赖,1.6及以下使用默认即可 -->
<dependencies>
<dependency>
<groupId>org.tmatesoft.svnkit</groupId>
<artifactId>svnkit</artifactId>
<version>1.8.2</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
<executions> <!--执行器 mvn assembly:assembly -->
<execution>
<id>tar-tomcat</id><!--名字任意 -->
<phase>package</phase><!-- 绑定到package生命周期阶段上 -->
<goals>
<goal>single</goal><!-- 只运行一次 -->
</goals>
<configuration>
<finalName>Tomcat_${version}_${SVN.revision}_${buildTime}</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptors> <!--描述文件路径 -->
<descriptor>tar.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
其中maven-svn-revision-number-plugin主要是用于从svn中获取工程的svn版本号,可以去掉,只是打包的文件名中没有svn版本号而也。
tar.xml是maven-assembly-plugin插件执行时要用到的配置文件,具体如下:
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>release</id>
<formats>
<format>tar.gz</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.basedir}/apache-tomcat-6.0.41</directory>
<outputDirectory>/apache-tomcat-6.0.41</outputDirectory>
</fileSet>
<fileSet>
<directory>${project.basedir}/</directory>
<lineEnding>unix</lineEnding>
<fileMode>777</fileMode>
<includes>
<include>install.sh</include>
</includes>
<outputDirectory>\</outputDirectory>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<useProjectArtifact>false</useProjectArtifact>
<!-- 将scope为runtime的依赖包打包到lib目录下。 -->
<!-- <outputDirectory>lib</outputDirectory> -->
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>