springboot多模块项目打war包
Posted song.yan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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>
二、子模块-启动模块配置
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以上是关于springboot多模块项目打war包的主要内容,如果未能解决你的问题,请参考以下文章