1. 程式人生 > >springboot一個專案多模組打包踩過的坑

springboot一個專案多模組打包踩過的坑

其實主要幾點:
切記父檔案不需要打包和對應的模組不要寫下面一段,在主模組寫就行。
因為對應的模組檔案不需要打包,因為我的主模組需要到對應包中的依賴,打包了就找不到了

  <build>
         <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId
>
</plugin> </plugins> </build>

詳情:
專案結構
這裡寫圖片描述

core不依賴誰

user依賴core

hr依賴user和core

hr是主模組,application的入口放在這裡的

如果以後我在做一個專案比如hr1,我可以再次去依賴core和user。

core和user。就可以公用了。。。。

其次我的pom.xml的父檔案

<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> <packaging>pom</packaging> <!-- 專案說明:這裡作為聚合工程的父工程 --> <groupId>com.ewe</groupId>
<artifactId>ewe-hr</artifactId> <version>1.0.0.RELEASE</version> <!-- 繼承說明:這裡繼承SpringBoot提供的父工程 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.7</java.version> </properties> <!-- 模組說明:這裡宣告多個子模組 --> <modules> <module>core</module> <module>hr</module> <module>user</module> </modules> <dependencyManagement> <!-- 模組說明:這裡宣告多個子模組 --> <dependencies> <!--子版本依賴--> <dependency> <groupId>com.ewe</groupId> <artifactId>core</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.ewe</groupId> <artifactId>user</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.ewe</groupId> <artifactId>hr</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </dependencyManagement> </project>

切記父檔案不需要打包。
不需要寫

    <build>
         <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                 </plugin>   
        </plugins>    
    </build>

再次我的core檔案

<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>
     <packaging>pom</packaging>
     <!-- 專案說明:這裡作為聚合工程的父工程 -->
     <groupId>com.ewe</groupId>
     <artifactId>ewe-hr</artifactId>
     <version>1.0.0.RELEASE</version>

      <!-- 繼承說明:這裡繼承SpringBoot提供的父工程 -->
     <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
     </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.7</java.version>
    </properties>
     <!-- 模組說明:這裡宣告多個子模組 -->
     <modules>
        <module>core</module>
        <module>hr</module>
        <module>user</module>
     </modules>
     <dependencyManagement>
      <!-- 模組說明:這裡宣告多個子模組 -->
         <dependencies>
            <!--子版本依賴-->
            <dependency>
                <groupId>com.ewe</groupId>
                <artifactId>core</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>  
            <dependency>
                <groupId>com.ewe</groupId>
                <artifactId>user</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>com.ewe</groupId>
                <artifactId>hr</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
        </dependencies>
     </dependencyManagement>
</project>

我的core檔案不需要打包,因為我的主專案hr需要core包中的依賴,打包了就找不到了
然後我的user檔案

<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>
  <parent>
    <groupId>com.ewe</groupId>
    <artifactId>ewe-hr</artifactId>
    <version>1.0.0.RELEASE</version>
     <relativePath>../pom.xml</relativePath>
  </parent>
  <artifactId>user</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>user</name>
  <description>user information</description>
  <dependencies>
      <dependency>
          <groupId>com.ewe</groupId>
          <artifactId>core</artifactId>
      </dependency>  

  </dependencies>
 <!-- Package as an executable jar -->
    <build>
          <resources>
           <resource>
               <directory>src/main/java</directory>
               <includes>
                   <include>**/*.xml</include>
               </includes>
           </resource>
           <resource>
               <directory>src/main/resources</directory>
               <includes>
                   <include>**.*</include>
                   <include>**/*.*</include><!-- i18n能讀取到 -->
                    <include>**/*/*.*</include>
               </includes>
           </resource>
         </resources>
    </build>
</project>

為什麼user有build呢,因為依賴的打包的時候我只是指明瞭需要打包的目錄resources
最後我的住主專案hr

<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>
  <artifactId>hr</artifactId>
  <name>hr</name>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

   <!-- 繼承本專案的父工程 -->
    <parent>
        <groupId>com.ewe</groupId>
        <artifactId>ewe-hr</artifactId>
        <version>1.0.0.RELEASE</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <dependencies>
      <dependency>
          <groupId>com.ewe</groupId>
          <artifactId>core</artifactId>
      </dependency>  
      <dependency>
          <groupId>com.ewe</groupId>
          <artifactId>user</artifactId>
      </dependency>  
    </dependencies>
     <!-- Package as an executable jar -->
    <build>
          <resources>
           <resource>
               <directory>src/main/java</directory>
               <includes>
                   <include>**/*.xml</include>
               </includes>
           </resource>
           <resource>
               <directory>src/main/resources</directory>
               <includes>
                   <include>**.*</include>
                   <include>**/*.*</include><!-- i18n能讀取到 -->
                    <include>**/*/*.*</include>
               </includes>
           </resource>
         </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>

            <configuration>
                 <fork>true</fork> <!-- 如果沒有該配置,devtools不會生效 -->
                <!-- 指定該Main Class為全域性的唯一入口 -->
                <mainClass>com.ewe.Application</mainClass>
                <layout>ZIP</layout>
          </configuration>
          <executions>
            <execution>
              <goals>
                <goal>repackage</goal><!--可以把依賴的包都打包到生成的Jar包中-->
              </goals>
            </execution>
          </executions>
          </plugin>
        </plugins>
        <finalName>hr</finalName>
    </build>
</project>

需要進行打包並指明main函式的入口
以上是解析如果有幫助你,點個贊,我也是從痛苦中過來的