1. 假设场景

有公司自己的项目jar包, 也有很多第三方的jar包。 我需要将打包后的内容分别放到不同的 jar包目录中, 最后打成一个linux 下的 tar.gz 文件, 上传到linux 之后通过脚本文件能直接启动

2. 演示工程

2.1 idea 中创建父工程

父工程中可以定义, 子模块中需要的包的版本信息

(1) pom 文件如下 (创建过程省略)

<?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.sansui.share</groupId>
    <artifactId>share-code</artifactId>
    <packaging>pom</packaging>


    <version>1.0-SNAPSHOT</version>
    <description>三岁的演示分享代码</description>
    <modules>
        <module>share-platform</module>
        <module>share-business</module>
        <module>share-iobus</module>
        <module>share-mscore</module>
        <module>share-deploy</module>
    </modules>

    <!-- 定义相关参数 -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <junit.test.version>4.11</junit.test.version>
        <log4j.core.version>2.8.2</log4j.core.version>
        <commons.lang3.version>3.11</commons.lang3.version>
    </properties>


    <!-- 引用包导入管理 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.test.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>${log4j.core.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>${commons.lang3.version}</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
            <dependency>
                <groupId>commons-lang</groupId>
                <artifactId>commons-lang</artifactId>
                <version>2.6</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>
2.2 idea 中创建子模块

(1) 创建代码模块

假设有 四个模块 分别是 share-platform,share-business,share-iobus,share-mscore

<module>share-platform</module> <!-- 相关底层代码, 比如相关类型定义, 工具类 等等 -->
<module>share-business</module> <!-- 业务相关代码 -->
<module>share-iobus</module>    <!-- 网络相关组件, 如自定义rpc -->
<module>share-mscore</module>   <!-- 核心服务启停相关 -->

(2)创建打包模块

打包模块需要聚合,业务代码模块以及第三方依赖包,配置文件,启停脚本等等

<module>share-deploy</module> <!-- 专门负责打包 部署 -->

(3)项目结构图示

maven的snapshot和release_xml

2.3 打包模块的配置

(1)需要在打包模块中增加 assembly 配置文件目录,另外增加assembly配置文件 用来分别配置增量包和全量包规则

我这里设置的是make-business-assembly.xml 是 增量包,make-full-assembly.xml 是全量包

maven的snapshot和release_xml_02


(2)make-business-assembly.xml 文件配置内容

<assembly xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0">
  <formats>
    <format>zip</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <directoryMode>0755</directoryMode>
      <fileMode>0644</fileMode>
      <useProjectArtifact>false</useProjectArtifact>

      <!-- 只需要包含业务相关的包即可 -->
      <includes>
        <include>share-*</include>
      </includes>
      <outputDirectory>/${app-project.version}/</outputDirectory>
    </dependencySet>
  </dependencySets>
</assembly>

(3)make-full-assembly.xml 文件配置内容

<assembly
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2">
    <id>linux</id>
    <includeBaseDirectory>true</includeBaseDirectory>

    <formats>
        <format>tar.gz</format>
    </formats>

    <fileSets>
        <!-- 将src/bin目录下的所有文件输出到打包后的bin目录中 -->
        <fileSet>
            <directory>${basedir}/src/main/bin</directory>
            <outputDirectory>bin</outputDirectory>
            <fileMode>0755</fileMode>
        </fileSet>

        <fileSet>
            <directory>${basedir}/src/main/resources/</directory>
            <outputDirectory>conf</outputDirectory>
        </fileSet>

        <!-- 将第三方依赖打包到lib目录中 -->
        <fileSet>
            <directory>${basedir}/target/share/lib</directory>
            <outputDirectory>lib/share/</outputDirectory>
            <includes>
                <include>share-*.jar</include>
            </includes>
            <fileMode>0755</fileMode>
        </fileSet>

        <!-- 将第三方依赖打包到lib目录中 -->
        <fileSet>
            <directory>${basedir}/target/share/lib</directory>
            <outputDirectory>lib/third/</outputDirectory>
            <fileMode>0755</fileMode>
            <excludes>
                <exclude>share-*.jar</exclude>
            </excludes>
        </fileSet>
    </fileSets>
</assembly>

(4)需要在打包模块中增加修改pom配置文件, 用来支持assembly 打包

<?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>share-code</artifactId>
        <groupId>com.sansui.share</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>share-deploy</artifactId>
    <description>打包和脚本生成</description>

    <properties>
        <app-project.version>${project.version}</app-project.version>
        <package-full.filename>share-mscore-full-${app-project.version}</package-full.filename>
        <package-busi.filename>share-mscore-business-${app-project.version}</package-busi.filename>
        <mscore.version>1.0-SNAPSHOT</mscore.version>
    </properties>

    <dependencies>
        <!-- 引入第三方依赖组件 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-configuration2</artifactId>
            <version>2.7</version>
        </dependency>


        <!-- 引入项目依赖 -->
        <dependency>
            <groupId>com.sansui.share</groupId>
            <artifactId>share-business</artifactId>
            <version>${mscore.version}</version>
        </dependency>

        <dependency>
            <groupId>com.sansui.share</groupId>
            <artifactId>share-platform</artifactId>
            <version>${mscore.version}</version>
        </dependency>

        <dependency>
            <groupId>com.sansui.share</groupId>
            <artifactId>share-iobus</artifactId>
            <version>${mscore.version}</version>
        </dependency>

        <dependency>
            <groupId>com.sansui.share</groupId>
            <artifactId>share-mscore</artifactId>
            <version>${mscore.version}</version>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <!-- 指定jar包拷贝到那个目录中 -->
                            <outputDirectory>${basedir}/target/share/lib</outputDirectory>
                            <overWriteReleases>true</overWriteReleases>
                            <overWriteSnapshots>true</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-full-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>${basedir}/assembly/make-full-assembly.xml</descriptor>
                            </descriptors>
                            <finalName>${package-full.filename}</finalName>
                        </configuration>
                    </execution>
                    <execution>
                        <id>make-business-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>${basedir}/assembly/make-business-assembly.xml</descriptor>
                            </descriptors>
                            <finalName>${package-busi.filename}</finalName>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

(5)部署模块下增加启停脚本

maven的snapshot和release_apache_03

2.4 打包测试

(1) 执行命令 mvn package

maven的snapshot和release_xml_04

(2) 打出的包

maven的snapshot和release_apache_05


(2)查看tar.gz 包的内容

这个tar.gz 是可以直接部署在环境中运行的模型

maven的snapshot和release_maven_06

(3)查看lib包的内容

maven的snapshot和release_xml_07


(4) 结束了, 开心下 ~