springboot ,idea对application-{profile}.properties 来管理不同环境的开发、打包和运行---打包指定配置文件,运行不需指定配置文件

理解了你的需求,你希望在打包时指定配置文件,使得在运行时不需要再次指定配置文件。这可以通过使用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. 打包并运行
  1. 打包

    • 开发环境

      mvn clean package -Dactive.profile=dev
      
    • 测试环境

      mvn clean package -Dactive.profile=test
      
    • 生产环境

      mvn clean package -Dactive.profile=pro
      
  2. 运行

    打包完成后,直接运行生成的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. 打包并运行
  1. 打包

    • 开发环境

      ./gradlew clean copyDevConfig bootJarDev
      
    • 测试环境

      ./gradlew clean copyTestConfig bootJarTest
      
    • 生产环境

      ./gradlew clean copyProConfig bootJarPro
      
  2. 运行

    打包完成后,直接运行生成的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);
    }
}

总结

通过上述步骤,你可以在打包时指定配置文件,并且在运行时不需再指定配置文件。这样可以简化部署过程,并确保每个环境使用正确的配置。如果有任何问题或需要进一步的帮助,请随时告诉我!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

慧香一格

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值