理解了你的需求,你希望在打包时指定配置文件,使得在运行时不需要再次指定配置文件。这可以通过使用Maven或Gradle的构建插件来实现。具体来说,你可以在构建过程中将特定的配置文件复制到最终的JAR包中,并将其设置为默认配置文件。
以下是详细的步骤:
使用 Maven
1. 创建配置文件
确保你在src/main/resources
目录下创建了相应的配置文件:
application-dev.properties
:开发环境配置application-test.properties
:测试环境配置application-pro.properties
:生产环境配置
例如:
application-dev.properties
spring.datasource.url=jdbc:mysql://localhost:3306/dev_db?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
application-test.properties
spring.datasource.url=jdbc:mysql://localhost:3306/test_db?useSSL=false&serverTimezone=UTC
spring.datasource.username=testuser
spring.datasource.password=testpass
spring.jpa.hibernate.ddl-auto=validate
application-pro.properties
spring.datasource.url=jdbc:mysql://prod-db-server:3306/prod_db?useSSL=false&serverTimezone=UTC
spring.datasource.username=produser
spring.datasource.password=prodpass
spring.jpa.hibernate.ddl-auto=none
2. 修改 pom.xml
你可以使用Maven的资源过滤功能来在打包时选择特定的配置文件。以下是一个示例配置:
<?xml version="1.0" encoding="UTF-8"?>
<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.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
<active.profile>dev</active.profile> <!-- 默认配置文件 -->
</properties>
<profiles>
<profile>
<id>dev</id>
<activation>
<property>
<name>active.profile</name>
<value>dev</value>
</property>
</activation>
<properties>
<activatedProperties>application-dev.properties</activatedProperties>
</properties>
</profile>
<profile>
<id>test</id>
<activation>
<property>
<name>active.profile</name>
<value>test</value>
</property>
</activation>
<properties>
<activatedProperties>application-test.properties</activatedProperties>
</properties>
</profile>
<profile>
<id>pro</id>
<activation>
<property>
<name>active.profile</name>
<value>pro</value>
</property>
</activation>
<properties>
<activatedProperties>application-pro.properties</activatedProperties>
</properties>
</profile>
</profiles>
<dependencies>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MyBatis Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<!-- MySQL Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Spring-Boot-Profiles>${active.profile}</Spring-Boot-Profiles>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
3. 打包并运行
-
打包
-
开发环境
mvn clean package -Dactive.profile=dev
-
测试环境
mvn clean package -Dactive.profile=test
-
生产环境
mvn clean package -Dactive.profile=pro
-
-
运行
打包完成后,直接运行生成的JAR文件,无需指定配置文件:
java -jar target/demo-0.0.1-SNAPSHOT.jar
使用 Gradle
1. 创建配置文件
确保你在src/main/resources
目录下创建了相应的配置文件:
application-dev.properties
:开发环境配置application-test.properties
:测试环境配置application-pro.properties
:生产环境配置
例如:
application-dev.properties
spring.datasource.url=jdbc:mysql://localhost:3306/dev_db?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
application-test.properties
spring.datasource.url=jdbc:mysql://localhost:3306/test_db?useSSL=false&serverTimezone=UTC
spring.datasource.username=testuser
spring.datasource.password=testpass
spring.jpa.hibernate.ddl-auto=validate
application-pro.properties
spring.datasource.url=jdbc:mysql://prod-db-server:3306/prod_db?useSSL=false&serverTimezone=UTC
spring.datasource.username=produser
spring.datasource.password=prodpass
spring.jpa.hibernate.ddl-auto=none
2. 修改 build.gradle
你可以使用Gradle的任务来在打包时选择特定的配置文件。以下是一个示例配置:
plugins {
id 'org.springframework.boot' version '2.7.5'
id 'io.spring.dependency-management' version '1.0.13.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.baomidou:mybatis-plus-boot-starter:3.4.3'
runtimeOnly 'mysql:mysql-connector-java'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.withType(ProcessResources) {
filesMatching('application*.properties') {
expand(project.properties)
}
}
task copyDevConfig(type: Copy) {
from 'src/main/resources/application-dev.properties'
into 'build/resources/main'
rename { 'application.properties' }
}
task copyTestConfig(type: Copy) {
from 'src/main/resources/application-test.properties'
into 'build/resources/main'
rename { 'application.properties' }
}
task copyProConfig(type: Copy) {
from 'src/main/resources/application-pro.properties'
into 'build/resources/main'
rename { 'application.properties' }
}
bootJar {
dependsOn tasks.named('copyDevConfig')
manifest {
attributes 'Spring-Boot-Profiles': 'dev'
}
}
task bootJarDev(type: Jar, dependsOn: bootJar) {
manifest {
attributes 'Spring-Boot-Profiles': 'dev'
}
}
task bootJarTest(type: Jar, dependsOn: bootJar) {
manifest {
attributes 'Spring-Boot-Profiles': 'test'
}
}
task bootJarPro(type: Jar, dependsOn: bootJar) {
manifest {
attributes 'Spring-Boot-Profiles': 'pro'
}
}
3. 打包并运行
-
打包
-
开发环境
./gradlew clean copyDevConfig bootJarDev
-
测试环境
./gradlew clean copyTestConfig bootJarTest
-
生产环境
./gradlew clean copyProConfig bootJarPro
-
-
运行
打包完成后,直接运行生成的JAR文件,无需指定配置文件:
java -jar build/libs/demo-0.0.1-SNAPSHOT.jar
完整示例
以下是完整的项目结构和关键文件内容:
项目结构
your-springboot-project/
├── src/
│ └── main/
│ ├── java/
│ │ └── com/example/demo/
│ │ └── DemoApplication.java
│ └── resources/
│ ├── application-dev.properties
│ ├── application-test.properties
│ └── application-pro.properties
└── pom.xml (for Maven)
└── build.gradle (for Gradle)
application-dev.properties
spring.datasource.url=jdbc:mysql://localhost:3306/dev_db?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
application-test.properties
spring.datasource.url=jdbc:mysql://localhost:3306/test_db?useSSL=false&serverTimezone=UTC
spring.datasource.username=testuser
spring.datasource.password=testpass
spring.jpa.hibernate.ddl-auto=validate
application-pro.properties
spring.datasource.url=jdbc:mysql://prod-db-server:3306/prod_db?useSSL=false&serverTimezone=UTC
spring.datasource.username=produser
spring.datasource.password=prodpass
spring.jpa.hibernate.ddl-auto=none
pom.xml
(适用于Maven)
<?xml version="1.0" encoding="UTF-8"?>
<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.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
<active.profile>dev</active.profile> <!-- 默认配置文件 -->
</properties>
<dependencies>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MyBatis Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<!-- MySQL Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<profiles>
<profile>
<id>dev</id>
<activation>
<property>
<name>active.profile</name>
<value>dev</value>
</property>
</activation>
<properties>
<activatedProperties>application-dev.properties</activatedProperties>
</properties>
</profile>
<profile>
<id>test</id>
<activation>
<property>
<name>active.profile</name>
<value>test</value>
</property>
</activation>
<properties>
<activatedProperties>application-test.properties</activatedProperties>
</properties>
</profile>
<profile>
<id>pro</id>
<activation>
<property>
<name>active.profile</name>
<value>pro</value>
</property>
</activation>
<properties>
<activatedProperties>application-pro.properties</activatedProperties>
</properties>
</profile>
</profiles>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Spring-Boot-Profiles>${active.profile}</Spring-Boot-Profiles>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
build.gradle
(适用于Gradle)
plugins {
id 'org.springframework.boot' version '2.7.5'
id 'io.spring.dependency-management' version '1.0.13.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.baomidou:mybatis-plus-boot-starter:3.4.3'
runtimeOnly 'mysql:mysql-connector-java'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.withType(ProcessResources) {
filesMatching('application*.properties') {
expand(project.properties)
}
}
task copyDevConfig(type: Copy) {
from 'src/main/resources/application-dev.properties'
into 'build/resources/main'
rename { 'application.properties' }
}
task copyTestConfig(type: Copy) {
from 'src/main/resources/application-test.properties'
into 'build/resources/main'
rename { 'application.properties' }
}
task copyProConfig(type: Copy) {
from 'src/main/resources/application-pro.properties'
into 'build/resources/main'
rename { 'application.properties' }
}
bootJar {
dependsOn tasks.named('copyDevConfig')
manifest {
attributes 'Spring-Boot-Profiles': 'dev'
}
}
task bootJarDev(type: Jar, dependsOn: bootJar) {
manifest {
attributes 'Spring-Boot-Profiles': 'dev'
}
}
task bootJarTest(type: Jar, dependsOn: bootJar) {
manifest {
attributes 'Spring-Boot-Profiles': 'test'
}
}
task bootJarPro(type: Jar, dependsOn: bootJar) {
manifest {
attributes 'Spring-Boot-Profiles': 'pro'
}
}
DemoApplication.java
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
总结
通过上述步骤,你可以在打包时指定配置文件,并且在运行时不需再指定配置文件。这样可以简化部署过程,并确保每个环境使用正确的配置。如果有任何问题或需要进一步的帮助,请随时告诉我!