springboot多模塊項目打war包


一、父模塊配置

1,指定pakaging:pom

2,指定編譯的版本:如下圖:

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.7</java.version>
    </properties>

3,屏蔽內置的tomcat:

  (1)這個內置的tomcat是在spring-boot-starter-web這個start中引入的,所以說:

      1)如果你的項目里面用到了這個starter(如下),就加上下面藍色框中的代碼將內置的tomcat屏蔽掉

      2)如果你的項目里面沒用到這個starter,就不會有內置的tomcat,這一條就可以跳過。 

      3)在大多數項目中都是有用到這個starter中的,而且可能很多子模塊中也用到了這個starter,所以可以在父模塊中添加(子模塊中不需要在添加),並屏蔽內置tomcat,如下:   

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!--忽略內嵌tomcat,打包部署到tomcat。注*本地運行的時候要把這一段忽略引入個注釋掉,要不然項目啟動不了 -->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

4, 關於對子模塊的依賴:

  (1)沒有用到子模塊可以不寫

  (2)用到哪個配哪個:如下:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.googosoft.microservice</groupId>
                <artifactId>micro-servcice-static</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>com.googosoft.microservice</groupId>
                <artifactId>micro-servcice-infrasture</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>com.googosoft.microservice</groupId>
                <artifactId>micro-servcice-user</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>com.googosoft.microservice</groupId>
                <artifactId>micro-service-autho</artifactId>
                <version>${project.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement> 

5,子模塊配置:

    <modules>
        <module>micro-service-static</module>
        <module>micro-service-infrasture</module>
        <module>micro-servcice-user</module>
        <module>micro-service-autho</module>
        <module>micro-service-run</module>
    </modules>

6,配置文件配置(該設置可在父模塊中統一設置,也可以在子模塊中單獨設置)

在沒有配置的時候報錯,找不到transaction.xml。

出錯原因:配置文件沒有打進war包

 

需要做如下配置:

        <!-- 解決讀不到配置文件的問題,將指定的文件打進war包 -->
        <resources>
            <resource>
                <!-- 要打進war包的文件所在的目錄 -->
                <directory>src/main/resorce</directory>
                <includes>
                    <include>**.*</include>
                    <include>**/*.*</include>
                    <include>**/*/*.*</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>

 

7,父模塊完整的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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.18.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.googosoft.microservice</groupId>
    <artifactId>googosoft-micro-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>googosoft-micro-service</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.7</java.version>
    </properties>
    <!--  只配置依賴到的即可 -->
<!--     <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.googosoft.microservice</groupId>
                <artifactId>micro-servcice-static</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>com.googosoft.microservice</groupId>
                <artifactId>micro-servcice-infrasture</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>com.googosoft.microservice</groupId>
                <artifactId>micro-servcice-user</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>com.googosoft.microservice</groupId>
                <artifactId>micro-service-autho</artifactId>
                <version>${project.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement> -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- 在父模塊中配置,子模塊無需在配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!--忽略內嵌tomcat,打包部署到tomcat。注*本地運行的時候要把這一段忽略引入個注釋掉,要不然項目啟動不了 -->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <!--用於編譯jsp -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <!-- 子模塊配置 -->
    <modules>
        <module>micro-service-static</module>
        <module>micro-service-infrasture</module>
        <module>micro-servcice-user</module>
        <module>micro-service-autho</module>
        <module>micro-service-run</module>
    </modules>
</project>
View Code

二、子模塊-啟動模塊配置

1,指定父模塊:

    <parent>
        <groupId>com.googosoft.microservice</groupId>
        <artifactId>googosoft-micro-service</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

2,packaging設置:

這里我需要一個war包,所i一指定成war

<packaging>war</packaging>

3,指定依賴的子模塊

如果依賴的模塊的packaging為war,就必須配置type,classifier,否則會報下錯:

正確的配置:

        <dependency>
            <groupId>com.googosoft.microservice</groupId>
            <artifactId>micro-servcice-user</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <!-- 該模塊的packaging為war,所以下面兩項必須設置,否則打包會出出錯  -->
            <type>jar</type>
            <classifier>classes</classifier>
        </dependency>
        <dependency>
            <groupId>com.googosoft.microservice</groupId>
            <artifactId>micro-service-autho</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <!-- 該模塊的packaging為war,所以下面兩項必須設置,否則打包會出出錯  -->
            <type>jar</type>
            <classifier>classes</classifier>
        </dependency>
        <dependency>
            <groupId>com.googosoft.microservice</groupId>
            <artifactId>micro-servcice-infrasture</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <!-- 該模塊的packaging為war,所以下面兩項必須設置,否則打包會出出錯  -->
            <type>jar</type>
            <classifier>classes</classifier>
        </dependency>

4,打war包插件設置

        <plugins>
            <!-- war包插件 -->
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <!-- 把class打包jar作為附件 -->
                    <attachClasses>true</attachClasses>
                </configuration>
            </plugin>
            <!-- 指定啟動入口 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.googosoft.UserBootApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>

 5,指定啟動入口

            <!-- 指定啟動入口 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.googosoft.UserBootApplication</mainClass>
                </configuration>
            </plugin>

 除了上面的配置還需要加Initializer繼承SpringBootServletInitializer重寫SpringApplicationBuilder方法,如下:

package com.googosoft;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(UserBootApplication.class);
    }
}

 不重寫報錯:

 

 

重寫后:

 

 

 6,啟動模塊完整pom.xml

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.googosoft.microservice</groupId>
        <artifactId>googosoft-micro-service</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>com.googosoft.microservice</groupId>
    <artifactId>micro-service-run</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>micro-service-run</name>
    <packaging>war</packaging>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.googosoft.microservice</groupId>
            <artifactId>micro-servcice-user</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <!-- 該模塊的packaging為war,所以下面兩項必須設置,否則打包會出出錯  -->
            <type>jar</type>
            <classifier>classes</classifier>
        </dependency>
        <dependency>
            <groupId>com.googosoft.microservice</groupId>
            <artifactId>micro-service-autho</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <!-- 該模塊的packaging為war,所以下面兩項必須設置,否則打包會出出錯  -->
            <type>jar</type>
            <classifier>classes</classifier>
        </dependency>
        <dependency>
            <groupId>com.googosoft.microservice</groupId>
            <artifactId>micro-servcice-infrasture</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <!-- 該模塊的packaging為war,所以下面兩項必須設置,否則打包會出出錯  -->
            <type>jar</type>
            <classifier>classes</classifier>
        </dependency>
    </dependencies>
    <build>
        <!-- 為jar包取名 -->
        <finalName>micro-service-run</finalName>
        <plugins>
            <!-- war包插件 -->
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <!-- 把class打包jar作為附件 -->
                    <attachClasses>true</attachClasses>
                </configuration>
            </plugin>
            <!-- 指定啟動入口 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.googosoft.UserBootApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
View Code

 

三、子模塊-packaging為war的模塊設置

1,packaging設置:war

設置為war后必須添加plugin

            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <!-- 把class打包jar作為附件 -->
                    <attachClasses>true</attachClasses>
                </configuration>
            </plugin>

2,模塊依賴設置

        <dependency>
            <groupId>com.googosoft.microservice</groupId>
            <artifactId>micro-servcice-infrasture</artifactId>
            <type>jar</type>
            <classifier>classes</classifier>
            <version>0.0.1-SNAPSHOT</version>
            <scope>provided</scope>
        </dependency>

3,packaging為war的子模塊的完整的pom.xml

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.googosoft.microservice</groupId>
        <artifactId>googosoft-micro-service</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <artifactId>micro-servcice-user</artifactId>
    <name>micro-servcice-user</name>
    <url>http://maven.apache.org</url>
    <packaging>war</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.15</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans -->
        <dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>3.1.0</version>
        </dependency>
        <!-- poi-ooxml XSSF is our port of the Microsoft Excel XML (2007+) file 
            format (OOXML) to pure Java -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/net.sf.ezmorph/ezmorph -->
        <dependency>
            <groupId>net.sf.ezmorph</groupId>
            <artifactId>ezmorph</artifactId>
            <version>1.0.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.wso2.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.wso2.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.1.wso2v1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl -->
        <dependency>
            <groupId>net.sourceforge.jexcelapi</groupId>
            <artifactId>jxl</artifactId>
            <version>2.6.12</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/at.bestsolution.efxclipse.eclipse/com.google.gson -->
        <!-- <dependency> -->
        <!-- <groupId>at.bestsolution.efxclipse.eclipse</groupId> -->
        <!-- <artifactId>com.google.gson</artifactId> -->
        <!-- <version>2.2.4</version> -->
        <!-- <scope>system</scope> -->
        <!-- <systemPath>${basedir}/lib/com.google.gson-2.2.4.jar</systemPath> -->
        <!-- </dependency> -->
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.9</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/eu.bitwalker/UserAgentUtils -->
        <dependency>
            <groupId>eu.bitwalker</groupId>
            <artifactId>UserAgentUtils</artifactId>
            <version>1.21</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <!-- <version>1.9.4</version> -->
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
        <!-- <dependency> -->
        <!-- <groupId>org.apache.commons</groupId> -->
        <!-- <artifactId>commons-collections4</artifactId> -->
        <!-- <version>4.4</version> -->
        <!-- </dependency> -->
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <!-- <dependency> -->
        <!-- <groupId>org.apache.commons</groupId> -->
        <!-- <artifactId>commons-lang3</artifactId> -->
        <!-- <version>3.9</version> -->
        <!-- </dependency> -->
        <!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->
        <!-- <dependency> -->
        <!-- <groupId>net.sf.json-lib</groupId> -->
        <!-- <artifactId>json-lib</artifactId> -->
        <!-- <version>2.2.3</version> -->
        <!-- <scope>system</scope> -->
        <!-- <systemPath>${basedir}/lib/json-lib-2.2.3-jdk13.jar</systemPath> -->
        <!-- </dependency> -->

        <!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-core -->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-core</artifactId>
            <version>1.4.2</version>
        </dependency>
        <dependency>
            <groupId>com.googosoft.microservice</groupId>
            <artifactId>micro-service-static</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!--單測 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--jdbc -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!-- mysql驅動 -->
        <!-- <dependency> -->
        <!-- <groupId>mysql</groupId> -->
        <!-- <artifactId>mysql-connector-java</artifactId> -->
        <!-- </dependency> -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>com.jslsolucoes</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.2</version>
        </dependency>
        <dependency>
            <groupId>com.googosoft.microservice</groupId>
            <artifactId>micro-servcice-infrasture</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <!-- 該模塊的packaging為war,所以下面兩項必須設置,否則打包會出出錯 -->
            <type>jar</type>
            <classifier>classes</classifier>
        </dependency>
    </dependencies>
    <build>
        <!-- 為jar包取名 -->
        <finalName>micro-servcice-user</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <!-- 把class打包jar作為附件 -->
                    <attachClasses>true</attachClasses>
                </configuration>
            </plugin>
            <!-- 熱布署 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!-- <configuration> -->
                <!-- fork : 如果沒有該項配置,這個devtools不會起作用,即應用不會restart -->
                <!-- <fork>true</fork> -->
                <!-- <mainClass>com.googosoft.UserBootApplication</mainClass> -->
                <!-- <layout>ZIP</layout> -->
                <!-- </configuration> -->
                <!-- <executions> -->
                <!-- <execution> -->
                <!-- <goals> -->
                <!-- <goal>repackage</goal>可以把依賴的包都打包到生成的Jar包中 -->
                <!-- </goals> -->
                <!-- </execution> -->
                <!-- </executions> -->
            </plugin>
        </plugins>
    </build>
</project>
View Code

四、其他設置

(1)依賴中盡量不要引用外部jar包

(我在開始使用的下面的方式,打包)

        <dependency> 
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.15</version>
            <scope>system</scope>
            <systemPath>${basedir}/lib/poi-ooxml-schemas-3.15.jar</systemPath>
        </dependency>

項目運行時報錯:classNotFound

將后兩行去掉,項目就可以正常運行了,引用外部jar包應該是可以使用的,但是可能需要額外的配置,最好不要使用。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM