需求:调试用jar运行,打包为war包,统一在tomcat上运行。
将遇到问题总结记录如下:
1、maven打包报错:No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
调试运行jar包是没问题的,maven打包有问题,这个maven引用有关,与eclipse实时编译是两个配置,细节不说,根据报错提示,编译成字节码文件有问题,需要引入jdk,而maven指向的仅是jre虚拟机,所以修改java installl配置即可。
2、tomcat启动war包,显示启动了两遍springboot
上图经典日志出现了两遍,怎么解决?
原因几种:打包时要去掉Springboot自带tomcat:
tomcat启动注意context标签会单独启动一次,同时host标签也会启动,默认就是熟悉的webapps:
最后一种,jar包启动类把SpringBootServletInitializer继承去掉,web启动类继承即可:
3、springboot打war包配置,项目建的时候是java project,但是不影响打war包,pom文件配置好即可:
<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>
<groupId>analysejar</groupId>
<artifactId>analysejar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 去除内嵌tomcat -->
<!-- <exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions> -->
</dependency>
<!--添加servlet的依赖-->
<!-- <dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>net.unicon.cas</groupId>
<artifactId>cas-client-autoconfig-support</artifactId>
<version>1.5.0-GA</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- 打两个包.orignal,出现这种情况是因为spring boot已经集成了打包的插件,如果pom.xml中又添加了spring-boot-maven-plugin就会造成重复打包 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<finalName>analyse</finalName>
</build>
</project>
上面代码是平时调试的pom文件配置,打包时,把注释的两段代码放开即可:
两种包的启动类不一样,如上解决两次启动问题的截图可见,不再赘述。