Spring boot maven multi-module packaging and stepping on the pit

Recently I have tossed about the multi-module packaging of spring boot under maven twice, and stepped on a lot of pits. Now the record is as follows.

Project directory:

  • Project P
  • Module A
  • Module B
  • Public Basic Module C
  • Mybatis basic module M

The parent pom.xml file:

  <!--版本号-->
  <groupId>com.parent</groupId>
  <artifactId>demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <!--管理所有的模块-->
  <modules>
    <modules>C</modules>
    <modules>M</modules>
  </modules>

  <!--指定项目的spring boot的版本-->
  <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.0.M5</version>
  </parent>

  <!--指定项目中公有的模块-->
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.module</groupId>
        <artifactId>c</artifactId>
        <version>${project.version}</version>
      </dependency>
      <dependency>
        <groupId>com.module</groupId>
        <artifactId>m</artifactId>
        <version>${project.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <!--指定jdk的版本为1.8,默认为1.6-->
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
  </properties>

  <!--指定项目中公有的依赖-->
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
  </dependencies>

  <!--指定使用maven打包-->
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
          <configuration>
            <source>${java.version}</source>
            <target>${java.version}</target>
            </configuration>
      </plugin>

      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.19.1</version>
            <configuration>
              <skipTests>true</skipTests>    <!--默认关掉单元测试 -->
            </configuration>
      </plugin>
    </plugins>
  </build>

Pom.xml of module A

  <groupId>com.module</groupId>
  <artifactId>a</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <!--指定父模块,需要注意的是,这里要指定父模块pom.xml的相对路径-->
  <parent>
    <groupId>com.parent</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
  </parent>

  <!--spring boot打包的话需要指定一个唯一的入门-->
  <build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
          <configuration>
            <!-- 指定该Main Class为全局的唯一入口 -->
            <mainClass>com.module.a.Application</mainClass>
            <layout>ZIP</layout>
          </configuration>
          <executions>
            <execution>
              <goals>
                <goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中-->
              </goals>
            </execution>
          </executions>
      </plugin>
		</plugins>
	</build>

Pom.xml of module B

Same as A

Pom.xml of module C

If it is a shared module, there is no need to package it, otherwise an error will be reported, because other modules will automatically add dependencies when they are packaged. If they are packaged here, other modules will not find the dependencies.

  <groupId>com.module</groupId>
	<artifactId>c</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

  <!--指定父模块,需要注意的是,这里要指定父模块pom.xml的相对路径-->
  <parent>
    <groupId>com.parent</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
  </parent>

Pom.xml of module M

If Mybatis is used in the project, it must be handled as a separate module. This Mybatis needs to be packaged

  <groupId>com.module</groupId>
  <artifactId>m</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <!--指定父模块,需要注意的是,这里要指定父模块pom.xml的相对路径-->
  <parent>
    <groupId>com.parent</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
  </parent>

  <!--mybatis的打包方式-->
  <build>
		<plugins>
			<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.3.5</version>
				<executions>
					<execution>
						<id>Generate MyBatis Artifacts</id>
						<phase>none</phase>
						<goals>
							<goal>generate</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<overwrite>true</overwrite>
				</configuration>
			</plugin>
		</plugins>
	</build>

Bale

After following the above configuration, just execute the following command

  mvn clean package

But if multiple modules are used, the above command will do all the modules are packaged, if you just package a module, you can use

  mvn -pl A -am install

Guess you like

Origin blog.csdn.net/Ser_Bad/article/details/78433340