maven项目打包成tar.gz文件-tar文件

一、背景

java项目由maven管理构建时,最常见的是打成jar包或war包,但实际项目中,有可能构建出zip包或tar包,本文介绍使用maven-assembly构建tar.gz文件形式。

二、构建步骤

通常一个项目可能还包含其他的外部配置文件,或者自定义的shell脚本或者bat命令等,此时应该使用assemble命令来进行构建。assembly,即组合构建的意思,使用此插件可以整合你想要的文件到最终的tar包中。

使用到的配置:pom.xml,test-assembly.xml

调用方式:pom.xml文件中调用test-assembly.xml

首先定义test-assembly.xml:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>test-${project.version}-bin</id>
<formats>
<format>tar.gz</format>
</formats>
<baseDirectory>xhh-test-${project.version}</baseDirectory>
<!--<dependencySets>-->
<!--<dependencySet>-->
<!--<useProjectArtifact>false</useProjectArtifact>-->
<!--<outputDirectory>lib</outputDirectory>-->
<!--<scope>runtime</scope>-->
<!--</dependencySet>-->
<!--</dependencySets>-->
<fileSets>
<fileSet>
<directory>${basedir}/bin</directory>
<lineEnding>unix</lineEnding>
<fileMode>0744</fileMode>
</fileSet>
<fileSet>
<directory>${basedir}/conf</directory>
<excludes>
<exclude>test-env.sh</exclude>
<exclude>**/move/**</exclude>
</excludes>
</fileSet>
<fileSet>
<directory>${basedir}/logs</directory>
</fileSet>
<fileSet>
<directory>${basedir}/tomcat</directory>
</fileSet>
<fileSet>
<directory>${basedir}/jdk</directory>
</fileSet>
<fileSet>
<directory>${basedir}/software</directory>
</fileSet>
<fileSet>
<directory>${basedir}/scripts</directory>
<lineEnding>unix</lineEnding>
<fileMode>0744</fileMode>
</fileSet>
<fileSet>
<directory>${basedir}/data</directory>
</fileSet>
<fileSet>
<directory>${basedir}/../../xhh-test</directory>
<excludes>
<exclude>**/classes/**</exclude>
<exclude>*/web.xml</exclude>
</excludes>
<outputDirectory>/webapps/test</outputDirectory>
</fileSet>
</fileSets>
<files>
<file>
<source>${basedir}/README.md</source>
</file>
<file>
<source>${basedir}/conf/bdoc-env.sh</source>
<lineEnding>unix</lineEnding>
<outputDirectory>/conf</outputDirectory>
</file>
</files>
</assembly

然后定义pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>com.xhh.test</groupId>
<artifactId>xhh-test</artifactId>
<version>2.1.0</version>
</parent>
<artifactId>xhh-test-packaging</artifactId>
<name>Packaging TEST</name>
<packaging>pom</packaging>
<!--<url>http://maven.apache.org</url>-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<appendAssemblyId>true</appendAssemblyId>
<!--<finalName>${project.name}</finalName>-->
<finalName>bc-bdoc</finalName>
<descriptors>
<descriptor>***test-assembly.xml***</descriptor> </descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly-tar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>unix</id>
<activation>
<property>
<name>env</name>
<value>unix</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<artifactId>exec-maven-plugin</artifactId>
<groupId>org.codehaus.mojo</groupId>
<executions>
<execution>
<id>uncompress</id>
<phase>prepare-package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>sh</executable>
<arguments>
<argument>${basedir}/scripts/unpack.sh</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>refresh</id>
<phase>prepare-package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>sh</executable>
<arguments>
<argument>${basedir}/scripts/refresh.sh</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

最后,执行:mvn package -DskipTests

生成xhh-test-1.1.0-bin.tar.gz包

可以用来当成小工具使用。

maven项目打包成tar.gz文件

推荐阅读