Spring boot maven多模塊打包踩坑

最近折騰了兩次spring boot在maven下的多模塊打包,踩了很多坑,現在記錄如下。

項目目錄:

  • 項目 P
  • 模塊 A
  • 模塊 B
  • 公有基礎模塊 C
  • Mybatis基礎模塊 M

父pom.xml文件:

  <!--版本號-->
  <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>

模塊A的pom.xml

  <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>

模塊B的pom.xml

同A即可

模塊C的pom.xml

如果是共有模塊的話,不需要打包,否則會報錯,因爲其他模塊在打包的時候會自動添加依賴進去,如果這裏打包了,其他的模塊就找不到該依賴了。

  <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>

模塊M的pom.xml

如果項目中使用的Mybatis的話,肯定是作爲一個單獨的模塊來處理的,這個Mybatis是需要打包的

  <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>

打包

按照上面的配置好以後,執行下面的命令就好了

  mvn clean package

但是如果使用了多個模塊,上面的命令是會吧全部的模塊都執行打包的,如果只是打包某個模塊的話,可以用

  mvn -pl A -am install
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章