maven项目打包成tar.gz格式

maven项目打包成tar.gz格式

一、背景

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}-binid>
    <formats>
        <format>tar.gzformat>
    formats>
    <baseDirectory>xhh-test-${project.version}baseDirectory>
    
        
            
            
            
        
    
    <fileSets>
        <fileSet>
            <directory>${basedir}/bindirectory>
            <lineEnding>unixlineEnding>
            <fileMode>0744fileMode>
        fileSet>
        <fileSet>
            <directory>${basedir}/confdirectory>
            <excludes>
                <exclude>test-env.shexclude>
                <exclude>**/move/**exclude>
            excludes>
        fileSet>
        <fileSet>
            <directory>${basedir}/logsdirectory>
        fileSet>
        <fileSet>
            <directory>${basedir}/tomcatdirectory>
        fileSet>
        <fileSet>
            <directory>${basedir}/jdkdirectory>
        fileSet>
        <fileSet>
            <directory>${basedir}/softwaredirectory>
        fileSet>
        <fileSet>
            <directory>${basedir}/scriptsdirectory>
            <lineEnding>unixlineEnding>
            <fileMode>0744fileMode>
        fileSet>
        <fileSet>
            <directory>${basedir}/datadirectory>
        fileSet>
        <fileSet>
            <directory>${basedir}/../../xhh-testdirectory>
            <excludes>
                <exclude>**/classes/**exclude>
                <exclude>*/web.xmlexclude>
            excludes>
            <outputDirectory>/webapps/testoutputDirectory>
        fileSet>
    fileSets>
    <files>
        <file>
            <source>${basedir}/README.mdsource>
        file>
        <file>
            <source>${basedir}/conf/bdoc-env.shsource>
            <lineEnding>unixlineEnding>
            <outputDirectory>/confoutputDirectory>
        file>
    files>
assembly

然后定义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.0modelVersion>
    <parent>
        <groupId>com.xhh.testgroupId>
        <artifactId>xhh-testartifactId>
        <version>2.1.0version>
    parent>
    <artifactId>xhh-test-packagingartifactId>
    <name>Packaging TESTname>
    <packaging>pompackaging>
    
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>
    <dependencies>
    dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-assembly-pluginartifactId>
                <version>2.6version>
                <configuration>
                    <appendAssemblyId>trueappendAssemblyId>
                    
                    <finalName>bc-bdocfinalName>
                    <descriptors>
                        <descriptor>***test-assembly.xml***descriptor>                    descriptors>
                configuration>
                <executions>
                    <execution>
                        <id>make-assembly-tarid>
                        <phase>packagephase>
                        <goals>
                            <goal>singlegoal>
                        goals>
                    execution>
                executions>
            plugin>
        plugins>
    build>

    <profiles>
        <profile>
            <id>unixid>
            <activation>
                <property>
                    <name>envname>
                    <value>unixvalue>
                property>
            activation>
            <build>
            <plugins>
            <plugin>
                <artifactId>exec-maven-pluginartifactId>
                <groupId>org.codehaus.mojogroupId>
                <executions>
                    <execution>
                        <id>uncompressid>
                        <phase>prepare-packagephase>
                        <goals>
                            <goal>execgoal>
                        goals>
                        <configuration>
                            <executable>shexecutable>
                            <arguments>
                                <argument>${basedir}/scripts/unpack.shargument>
                            arguments>
                        configuration>
                    execution>
                    <execution>
                        <id>refreshid>
                        <phase>prepare-packagephase>
                        <goals>
                            <goal>execgoal>
                        goals>
                        <configuration>
                            <executable>shexecutable>
                            <arguments>
                                <argument>${basedir}/scripts/refresh.shargument>
                            arguments>
                        configuration>
                    execution>
                executions>
            plugin>
            plugins>
            build>
        profile>
    profiles>

project>

最后,执行:mvn package -DskipTests
生成xhh-test-1.1.0-bin.tar.gz包

你可能感兴趣的:(项目管理)