Maven Profile
@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DemoApplication.class);
}
}
<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 https://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>2.7.18</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>1.0.0</version>
<name>demo</name>
<!-- 默认使用 jar 包 -->
<packaging>${packaging.type}</packaging>
<properties>
<java.version>8</java.version>
<!-- 默认打包类型 -->
<packaging.type>jar</packaging.type>
</properties>
<dependencies>
<!-- Web starter - 默认包含内嵌 Tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Tomcat 依赖,war 包时使用 provided 范围 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>${tomcat.scope}</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 当你需要引入本地 JAR 包时 -->
<dependency>
<groupId>com.example</groupId>
<artifactId>local-lib</artifactId>
<version>1.0</version>
<scope>system</scope> <!-- system scope 表示本地文件 -->
<systemPath>${project.basedir}/libs/local-lib.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- jar 包时需要可执行,war 包时不需要 -->
<configuration>
<skip>${spring-boot.skip}</skip>
<includeSystemScope>true</includeSystemScope> <!-- 包含本地 JAR -->
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<!-- Profile 配置:定义不同环境的属性 -->
<profiles>
<!-- Jar 包 Profile(默认) -->
<profile>
<id>jar</id>
<activation>
<activeByDefault>true</activeByDefault> <!-- IDEA 会默认激活 -->
</activation>
<properties>
<packaging.type>jar</packaging.type>
<tomcat.scope>compile</tomcat.scope>
<spring-boot.skip>false</spring-boot.skip> <!-- 执行 repackage,生成可执行 JAR -->
</properties>
</profile>
<!-- War 包 Profile -->
<profile>
<id>war</id>
<properties>
<packaging.type>war</packaging.type>
<tomcat.scope>provided</tomcat.scope>
<spring-boot.skip>true</spring-boot.skip>
</properties>
<dependencies>
<!-- war 包需要排除内嵌 Tomcat -->
<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>
</dependencies>
</profile>
</profiles>
</project>