Maven 多模块项目打 jar 包

一、用 Assembly Plugin 打 ZIP/JAR 包 1. 新建一个打包模块 ,比如 distribution : 2. 在 distribution/pom.xml 中: \ jar\ 3. 打包命令 (在项目根或指定模块下都行): 结果在 distribution/target/distribution-1.0-SNAPSHOT.jar...

一、用 Assembly Plugin 打 ZIP/JAR 包

  1. 新建一个打包模块,比如 distribution:
    1
    2
    3
    4
    5
    6
    7
    
    parent
    ├─ api-model
    ├─ api-path
    ├─ service
    ├─ example
    └─ distribution    ← packaging=jar,用来做 assembly
        └─ pom.xml
    
  2. 在 distribution/pom.xml 中: <packaging>jar</packaging>
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    
    <project>
      <parent>
        <groupId>com.github</groupId>
        <artifactId>OmniModel</artifactId>
        <version>1.0-SNAPSHOT</version>
      </parent>
      <artifactId>distribution</artifactId>
      <packaging>jar</packaging>
    
      <dependencies>
        <!-- 把所有要打包的模块都作为依赖 -->
        <dependency>
          <groupId>com.github</groupId>
          <artifactId>api-model</artifactId>
          <version>${project.version}</version>
        </dependency>
        <dependency>
          <groupId>com.github</groupId>
          <artifactId>api-path</artifactId>
          <version>${project.version}</version>
        </dependency>
        <dependency>
          <groupId>com.github</groupId>
          <artifactId>service</artifactId>
          <version>${project.version}</version>
        </dependency>
        <dependency>
          <groupId>com.github</groupId>
          <artifactId>example</artifactId>
          <version>${project.version}</version>
        </dependency>
      </dependencies>
    
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.6.0</version>
            <configuration>
              <descriptorRefs>
                <!-- 内置的 jar-with-dependencies -->
                <descriptorRef>jar-with-dependencies</descriptorRef>
              </descriptorRefs>
              <!-- 也可以自定义 assembly.xml 放 src/assembly 下 -->
              <appendAssemblyId>false</appendAssemblyId>
              <finalName>${project.artifactId}-${project.version}</finalName>
            </configuration>
            <executions>
              <execution>
                <id>make-assembly</id>
                <phase>package</phase>
                <goals><goal>single</goal></goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </project>
    
  3. 打包命令(在项目根或指定模块下都行):
    1
    
    mvn clean package -pl distribution
    
    结果在 distribution/target/distribution-1.0-SNAPSHOT.jar,里面包含所有子模块的 classes 和依赖。
最后更新于 2025年04月28日
使用 Hugo 构建
主题 Stack 由 Jimmy 设计