1. 程式人生 > >mvn使用assembly打包所有的moudle為一個tgz檔案

mvn使用assembly打包所有的moudle為一個tgz檔案

1.檔案目錄如下

在這裡插入圖片描述

首先建立LccTest專案,然後建立兩個moudle,分別為test1,test2/Users/lcc/IdeaProjects/LccTest/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> <groupId>com.lcc</groupId> <artifactId>LCCTEST</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>test1</module> <
module
>
test2</module> <module>assembly</module> </modules> <properties> <all.test.version>1.0.0</all.test.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <lombok.version>1.12.6</
lombok.version
>
<junit.version>4.12</junit.version> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>

2.建立測試類

/Users/lcc/IdeaProjects/LccTest/test1/src/main/java/com/lcc/Test.java
/Users/lcc/IdeaProjects/LccTest/test2/src/main/java/com/lcc/Test.java
檔案內容一樣

package com.lcc;

/**
 * Created by lcc on 2018/10/15.
 */
public class Test {

    public static void main(String[] args){
        System.out.print("aa");
    }
}

3.建立assembly的moudle

刪除裡面的東西,只剩下pom.xml

/Users/lcc/IdeaProjects/LccTest/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">
    <parent>
        <artifactId>LCCTEST</artifactId>
        <groupId>com.lcc</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <name>assembly</name>

    <artifactId>assembly</artifactId>



    <properties>
        <assembly.name>AllTest.1.0</assembly.name>
        <assembly.format>tgz</assembly.format>
        <skipDeploy>true</skipDeploy>
    </properties>

    <dependencies>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <finalName>${assembly.name}</finalName>
                            <appendAssemblyId>false</appendAssemblyId>
                            <descriptors>
                                <descriptor>assembly.xml</descriptor>
                            </descriptors>
                            <outputDirectory>${project.parent.basedir}/dist</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

新增要打包的jar包

/Users/lcc/IdeaProjects/LccTest/assembly/assembly.xml
<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>${project.version}</id>
    <formats>
         <format>tgz</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>


        <!--assembly-->
        <fileSet>
            <directory>${project.basedir}/sbin</directory>
            <outputDirectory>${assembly.name}/sbin</outputDirectory>
            <includes>
                <include>*</include>
            </includes>
        </fileSet>


        <!---->
        <fileSet>
            <directory>${project.parent.basedir}/test1/target</directory>
            <outputDirectory>${assembly.name}/jars</outputDirectory>
            <includes>
                <include>test1-1.0-SNAPSHOT.jar</include>
            </includes>
        </fileSet>

        <fileSet>
            <directory>${project.parent.basedir}/test2/target</directory>
            <outputDirectory>${assembly.name}/jars</outputDirectory>
            <includes>
                <include>test2-1.0-SNAPSHOT.jar</include>
            </includes>
        </fileSet>



        <fileSet>
            <directory>${project.basedir}/version</directory>
            <outputDirectory>${assembly.name}/</outputDirectory>
            <includes>
                <include>version.txt</include>
            </includes>
        </fileSet>


    </fileSets>

</assembly>

版本資訊

/Users/lcc/IdeaProjects/LccTest/assembly/version/version.txt
v3.1

4.打包測試

[email protected] LccTest$ mvn clean -X  package    -DskipTests 

可以看到

/Users/lcc/IdeaProjects/LccTest/dist/AllTest.1.0.tgz

生成了整體的壓縮檔案