SpringBoot 多模块项目打包
开发工具为idea
多模块项目有一个或多个web子工程,负责工程的启动运行;
有jar、war选项,选择哪种打包方式会打包成哪种(web工程为启动工程,所以web工程打包成哪种即项目以哪种方式运行)
一.使用idea运行项目 :
TODO:此文章不讲解配置Maven,默认已maven已可以成功打包为例-.-
1.使用idea运行时,默认即为jar方式运行,找到web工程中的application类main方法运行即可。
2.用idea嵌入tomcat,将项目打war包运行:
先将web子工程改成war包形式
2.1:web子工程pom中加入:
<packaging>war</packaging>
2.2:父工程中pom中
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-legacy</artifactId>
<version>1.1.0.RELEASE</version>
</dependency>
2.3:找到生成的项目编译出来的war添加到tomcat运行:
二.外部运行项目:
1.使用内置tomcat运行(很简单):
根据生成目录找到jar ,运行命令为:java -jar xxx.jar
根据生成目录找到war,运行命令为:java -jar xxx.war
2.使用外置tomcat在外部运行项目:
一般都是因为要多个项目共存于一个tomcat的需求,或前后台置于一个tomcat中
2.1:
一种是自己根据tomcat运行的规则建造出项目结构相同的文件夹,将项目所产生的class和lib一一对应的置换,如果还运行不了就需要手动写web.xml放入其中,web.xml模板如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.example.demo.DemoApplication</param-value>
</context-param>
<listener>
<listenerclass>
org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener
</listener-class>
</listener>
<!--<filter>
<filter-name>metricFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>metricFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextAttribute</param-name>
<param-value>
org.springframework.web.context.WebApplicationContext.ROOT
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
写的时候需要注意,所有用到servlet的地方例如filter、listener都需要在其中配置,按照javaweb规范书写,我最开始就是用这种方式,亲测可行,只是有点麻烦而已。
2.2:
使用父项目(root)打包,执行完后找到web子工程目录中所生成的.war和.war.original
最后:.war是没用的-是没用的-是没用的,把.war.original复制到tomcat里面,改名为xxx.war,亲测可用,访问时命名是xxx,就在端口后加/xxx/
注意:
1.使用内置tomcat时,所有tomcat配置项配置于Spring配置文件中;
2.如果需要运行于外置tomcat ,则tomcat版本需在8以上,否则报错
Unable to process Jar entry [META-INF/versions/9/module-info.class] from Jar