網上關於如何切換,其實說的很明確,本文主要經過profile進行快速切換已實如今不一樣場合下,用不一樣的打包方式。java
pom文件修改linux
入口類修改web
特別注意:當改爲war包的時候,application.properties
配置的server.port
和server.servlet.context-path
就無效了,聽從war
容器的安排。spring
<packaging>${pom.package}</packaging>
<!-- 做用是打war包的時候,不帶版本號 --> <finalName>${pom.packageName}</finalName> <!--加入plugin--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> <configuration> <!--若是想在沒有web.xml文件的狀況下構建WAR,請設置爲false。--> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>
<profiles> <profile> <!-- 開發環境 --> <id>jar</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <pom.package>jar</pom.package> <pom.packageName>${project.artifactId}-${project.version}</pom.packageName> <pom.profiles.active>dev</pom.profiles.active> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> </dependencies> </profile> <profile> <id>war</id> <properties> <pom.package>war</pom.package> <pom.packageName>${project.artifactId}</pom.packageName> <pom.profiles.active>linux</pom.profiles.active> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> </dependencies> </profile> </profiles>
SpringBootServletInitializer
configure
方法使用@Profile註解,當啓用war配置的時候,初始化Servlet。apache
public class Application extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Profile(value = {"war"}) @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } }