由于在工作中,经常需要把项目制作成war包,然后部署到容器中,而在IDEA中可以很方便的直接调试,便有了如下的两种情形
内容参见配置SpringBoot方便的切换jar和war
<packaging>${pom.package}packaging>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-tomcatartifactId>
exclusion>
exclusions>
dependency>
<build>
<finalName>ROOTfinalName>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.1version>
<configuration>
<source>1.8source>
<target>1.8target>
<skip>trueskip>
<encoding>UTF-8encoding>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-war-pluginartifactId>
<version>3.1.0version>
<configuration>
<failOnMissingWebXml>falsefailOnMissingWebXml>
configuration>
plugin>
plugins>
build>
<profiles>
<profile>
<id>jarid>
<activation>
<activeByDefault>trueactiveByDefault>
activation>
<properties>
<pom.package>jarpom.package>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-tomcatartifactId>
<version>2.0.1.RELEASEversion>
dependency>
dependencies>
profile>
<profile>
<id>warid>
<properties>
<pom.package>warpom.package>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-tomcatartifactId>
<version>2.0.1.RELEASEversion>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
dependencies>
profile>
profiles>
添加ServletInitializer
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Profile;
/**
* 启动类
*
* @Owner: SongQuanHeng
* @Time: 2019/6/11-14:24
* @Version:
* @Change:
*/
@SpringBootApplication
@ComponentScan(value = {"com.example.profile"})
public class App extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
System.out.println("程序启动成功");
}
@Profile(value = {"war"})
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application){
return application.sources(App.class);
}
}
完整的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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.1.RELEASEversion>
<relativePath/>
parent>
<groupId>com.examplegroupId>
<artifactId>profileartifactId>
<version>1.3-SNAPSHOTversion>
<name>profilename>
<packaging>${pom.package}packaging>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.apache.mavengroupId>
<artifactId>maven-plugin-apiartifactId>
<version>3.5.0version>
dependency>
<dependency>
<groupId>org.codehaus.cargogroupId>
<artifactId>cargo-maven2-pluginartifactId>
<version>1.7.6version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-tomcatartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
dependencies>
<repositories>
<repository>
<id>nexusid>
<name>Nexus3name>
<url>http://localhost:8081/repository/maven-central/url>
<snapshots>
<enabled>falseenabled>
snapshots>
<releases>
<enabled>trueenabled>
releases>
repository>
repositories>
<distributionManagement>
<repository>
<id>nexus-releaseid>
<name>Local Nexus Repositoryname>
<url>http://localhost:8081/repository/maven-releases/url>
repository>
<snapshotRepository>
<id>nexus-snapshotid>
<name>Local Nexus Repositoryname>
<url>http://localhost:8081/repository/maven-snapshots/url>
snapshotRepository>
distributionManagement>
<properties>
<java.version>1.8java.version>
properties>
<build>
<finalName>ROOTfinalName>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.1version>
<configuration>
<source>1.8source>
<target>1.8target>
<skip>trueskip>
<encoding>UTF-8encoding>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-war-pluginartifactId>
<version>3.1.0version>
<configuration>
<failOnMissingWebXml>falsefailOnMissingWebXml>
configuration>
plugin>
plugins>
build>
<profiles>
<profile>
<id>jarid>
<activation>
<activeByDefault>trueactiveByDefault>
activation>
<properties>
<pom.package>jarpom.package>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-tomcatartifactId>
<version>2.0.1.RELEASEversion>
dependency>
dependencies>
profile>
<profile>
<id>warid>
<properties>
<pom.package>warpom.package>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-tomcatartifactId>
<version>2.0.1.RELEASEversion>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
dependencies>
profile>
profiles>
project>
"C:\Program Files\Java\jdk1.8.0_131\bin\java" -Dmaven.multiModuleProjectDirectory=D:\600-Git\profile\profile "-Dmaven.home=C:\Program Files\apache-maven-3.5.4" "-Dclassworlds.conf=C:\Program Files\apache-maven-3.5.4\bin\m2.conf" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.1\lib\idea_rt.jar=53936:C:\Program Files\JetBrains\IntelliJ IDEA 2018.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\apache-maven-3.5.4\boot\plexus-classworlds-2.5.2.jar" org.codehaus.classworlds.Launcher -Didea.version=2018.1 -s C:\Users\lenovo\.m2\settings.xml package -f pom.xml -P war
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.example:profile:war:1.3-SNAPSHOT
[WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.springframework.boot:spring-boot-starter-web:jar -> duplicate declaration of version (?) @ line 52, column 15
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------< com.example:profile >-------------------------
[INFO] Building profile 1.3-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) @ profile ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 4 resources
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ profile ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to D:\600-Git\profile\profile\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:testResources (default-testResources) @ profile ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\600-Git\profile\profile\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ profile ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ profile ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-war-plugin:3.1.0:war (default-war) @ profile ---
[INFO] Packaging webapp
[INFO] Assembling webapp [profile] in [D:\600-Git\profile\profile\target\ROOT]
[INFO] Processing war project
[INFO] Webapp assembled in [3895 msecs]
[INFO] Building war: D:\600-Git\profile\profile\target\ROOT.war
[INFO]
[INFO] --- spring-boot-maven-plugin:2.0.1.RELEASE:repackage (default) @ profile ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 55.344 s
[INFO] Finished at: 2019-08-24T15:37:08+08:00
[INFO] ------------------------------------------------------------------------
Process finished with exit code 0
在演示时,使用Tomcat容器来进行war正常工作
拷贝ROOT.war到Tomcat的webapps目录下
执行bin目录下的startup.bat
特别注意:当改成war包的时候,application.properties配置的server.port和server.servlet.context-path就无效了,遵从war容器的安排。
上述执行图示也说明了该问题。虽然在application.properties中指定了端口为8086,但实际项目运行时,使用的端口为8080
上述的过程就是通过在POM中添加profiles的方式,通过外部命令指定profile的方式灵活的生成war包和jar包,并且方便快速的在IDEA中调试的过程。
工作在于积累和提高效率,如果一个问题,你可以在第一次解决的时候把解决的过程记录下来,那么下次当你遇到相同的问题,能够使问题快速得到解决,那么这就是成长。
关于态度,当领导突然给你一个任务时,千万不要顺口就反对和排斥,工作时应该抱持的态度是开放,而不是抵制,所以先不要急着反对,而是静下来,当你遇到相同的情境时,没有张口就反对,那么这就是成长。
看电视剧《全职高手》,昨天兴欣战队失利,叶修千机伞被毁,团队成员都变得失落,沮丧,吃饭时大家相互推诿责任,彼此指责,这个时候不要也随着自己的情绪而相互职责,而是静下来承受,因为只有做的不好的时候,才会出现抱怨彼此,不妨静下来,不让自己陷入抱怨别人的情境中,就算被别人抱怨,也别急着反击,而是承担下来,增加自己的心量,让自己的胸怀更加宽广,我一直相信,一个人胸怀有多大,就会有多大的前景,所以无论是工作,还是生活,都最好不要抱怨和牢骚,因为抱怨和牢骚对于破冰毫无益处,而只会拉低自己的能量状态,不如好好反思检讨。
Git地址