一、父模块配置
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</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>
三、子模块-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>
四、子模块-静态模块配置
1,packaging:设置为jar即可
2,文件打包目录
五、其他设置
(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包应该是可以使用的,但是可能需要额外的配置,最好不要使用。
原文地址:https://www.cnblogs.com/excellencesy/p/12037521.html