欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

Maven 打包方法(多个模块)

最编程 2024-07-18 15:27:36
...

1.概述

这篇 IntelliJ IDEA自身以及maven项目打包方式(单模块)博文主要是描述项目下单个模块的打包方式,但是现在很多项目往往是多模块组成的,单模块打包方式与多模块差异比较大,所以我们还是有必要学下如何配置多模块打包。

2. Maven打包方式(多模块)

这里我们主要采用的是maven-assembly-plugins插件进行zip打包。以下面为例:

 assembly为打包模块,红框为各大子模块

2.1 maven-assembly-plugins多模块zip打包

2.1.1 父类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.swordfall</groupId>
  <artifactId>aiplatform</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>aiplatform</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <parent>
    <groupId>org.apache</groupId>
    <artifactId>apache</artifactId>
    <version>21</version>
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <AssemblyPackage.version>1.0-SNAPSHOT</AssemblyPackage.version>
    <!--java-->
    <java.version>1.8</java.version>

    <!--common base-->
    <commons.collections.version>3.2.2</commons.collections.version>
    <commons.httpclient>3.0.1</commons.httpclient>
    <commons.beanutils.version>1.7.0</commons.beanutils.version>
    <commons.configuration.version>1.10</commons.configuration.version>
    <commons.collections4.version>4.1</commons.collections4.version>
    <commons.io.version>2.4</commons.io.version>
    <commons.codec.version>1.6</commons.codec.version>
    <commons.logging.version>1.1.1</commons.logging.version>
    <guava.version>20.0</guava.version>

    <!--maven plugin-->
    <maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
    <maven-assembly-plugin.version>3.2.0</maven-assembly-plugin.version>
    <maven-shade-plugin.version>3.2.2</maven-shade-plugin.version>
    <maven-release-plugin.version>2.5.3</maven-release-plugin.version>
    <maven-dependency-plugin.version>3.1.1</maven-dependency-plugin.version>
    <rpm-maven-plugion.version>2.2.0</rpm-maven-plugion.version>
    <maven-gpg-plugin.version>1.6</maven-gpg-plugin.version>
    <maven.deploy.skip>false</maven.deploy.skip>
  </properties>

  <dependencyManagement>
    <dependencies>

      <!--base-->
      <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>${commons.codec.version}</version>
      </dependency>
      <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>${commons.logging.version}</version>
      </dependency>
      <dependency>
        <groupId>commons-collections</groupId>
        <artifactId>commons-collections</artifactId>
        <version>${commons.collections.version}</version>
      </dependency>
      <dependency>
        <groupId>commons-httpclient</groupId>
        <artifactId>commons-httpclient</artifactId>
        <version>${commons.httpclient}</version>
      </dependency>
      <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>${commons.beanutils.version}</version>
      </dependency>
      <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils-core</artifactId>
        <version>${commons.beanutils.version}</version>
      </dependency>
      <dependency>
        <groupId>commons-configuration</groupId>
        <artifactId>commons-configuration</artifactId>
        <version>${commons.configuration.version}</version>
      </dependency>
      <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-collections4</artifactId>
        <version>${commons.collections4.version}</version>
      </dependency>
      <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>${commons.io.version}</version>
      </dependency>
    
      <!--module-->
      <dependency>
        <groupId>com.swordfall</groupId>
        <artifactId>aiplatform-api</artifactId>
        <version>${project.version}</version>
      </dependency>
      <dependency>
        <groupId>com.swordfall</groupId>
        <artifactId>aiplatform-common</artifactId>
        <version>${project.version}</version>
      </dependency>
      <dependency>
        <groupId>com.swordfall</groupId>
        <artifactId>aiplatform-dao</artifactId>
        <version>${project.version}</version>
      </dependency>
      <dependency>
        <groupId>com.swordfall</groupId>
        <artifactId>aiplatform-service</artifactId>
        <version>${project.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <build>
    <finalName>aiplatform-${project.version}</finalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>rpm-maven-plugin</artifactId>
          <version>${rpm-maven-plugion.version}</version>
          <inherited>false</inherited>
        </plugin>

        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <configuration>
            <source>${java.version}</source>
            <target>${java.version}</target>
            <testSource>${java.version}</testSource>
            <testTarget>${java.version}</testTarget>
          </configuration>
          <version>${maven-compiler-plugin.version}</version>
        </plugin>

        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>${maven-assembly-plugin.version}</version>
        </plugin>

        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>${maven-dependency-plugin.version}</version>
        </plugin>

        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-gpg-plugin</artifactId>
          <version>${maven-gpg-plugin.version}</version>
        </plugin>
      </plugins>
    </pluginManagement>

    <plugins>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-gpg-plugin</artifactId>
        <configuration>
          <skip>true</skip>
        </configuration>
      </plugin>

      <!--编译插件-->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>${maven-compiler-plugin.version}</version>
        <configuration>
          <source>${java.version}</source>
          <target>${java.version}</target>
          <encoding>${project.build.sourceEncoding}</encoding>
          <skip>true</skip><!--not skip compile test classes-->
        </configuration>
        <executions>
          <execution>
            <phase>compile</phase>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <modules>
    <!-- 父类pom.xml中配置子模块 -->
    <module>aiplatform-rpc</module>
    <module>aiplatform-common</module>
    <module>aiplatform-dao</module>
    <module>aiplatform-service</module>
    <module>aiplatform-api</module>
   <!-- <module>aiplatform-scheduler</module>-->
    <module>assembly</module> <!--打包模块放在最后-->
  </modules>
</project>

2.1.2 子模块aiplatform-api 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>aiplatform</artifactId>
        <groupId>com.swordfall</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>aiplatform-api</artifactId>
    <name>${project.artifactId}</name>
    <packaging>jar</packaging>

    <dependencies>
        <!-- 依赖的其他module -->
        <dependency>
            <groupId>com.swordfall</groupId>
            <artifactId>aiplatform-rpc</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.swordfall</groupId>
            <artifactId>aiplatform-service</artifactId>
        </dependency>
        <dependency>
            <groupId>com.swordfall</groupId>
            <artifactId>aiplatform-dao</artifactId>
        </dependency>
        <dependency>
            <groupId>com.swordfall</groupId>
            <artifactId>aiplatform-common</artifactId>
        </dependency>
    </dependencies>

    <build>
        <!-- 扫描项目下所有的文件 -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</
																				
															

推荐阅读