filter打包区分环境

项目目录结构:

父pom:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.test.env</groupId>
    <artifactId>test-environment</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>test-common</module>
        <module>environment-demo</module>
    </modules>
</project>

common pom:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>test-environment</artifactId>
        <groupId>com.test.env</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>test-common</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>test-common</name>
</project>  

 demo pom,filter区分环境:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>test-environment</artifactId>
        <groupId>com.test.env</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <properties>
        <java.version>1.8</java.version>
        <project.version>1.0.0-RELEASE</project.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <springboot.version>1.5.20.RELEASE</springboot.version>
    </properties>

    <artifactId>environment-demo</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${springboot.version}</version>
        </dependency>
        <dependency>
            <groupId>com.test.env</groupId>
            <artifactId>test-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>environment-demo</finalName>
        <filters>
            <filter>../filter/${env}/common.properties</filter>
        </filters>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.2</version>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>development</id>
            <properties>
                <env>dev</env>
                <build.level>compile</build.level>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>beta</id>
            <properties>
                <env>beta</env>
                <build.level>provided</build.level>
            </properties>
        </profile>
        <profile>
            <id>production</id>
            <properties>
                <env>production</env>
                <build.level>provided</build.level>
            </properties>
        </profile>
    </profiles>


</project>

 spring-beans.xml:EnvironmentBean在common中

 

spring-confog.xml

 

 

 

 

 springboot 通过BaseConfig导入spring配置

@SpringBootConfiguration
@ImportResource("classpath:spring/spring-config.xml")//多个bean的配置文件
public class BaseConfig {
}

springboot:

@SpringBootApplication
public class EnvironmentDemo implements CommandLineRunner{
    @Autowired
    EnvironmentBean config;
    @Override
    public void run(String... args) throws Exception {
        System.out.println("config:"+config);
    }
    public static void main(String[] args) {
        SpringApplication.run(EnvironmentDemo.class);
    }
}

在demo 目录下区分打包beta,production环境:mvn clean package -P beta/production

spring-beans.xml 中env.name/desc的值会被替换为相应环境的值。

注意:如果是第一次打包可能会遇到Could not find artifact XXX,这是因为需要父工程打包到本地仓库,整个大项目一期build install就顺利通过,之后再子项目打包。

 

 

posted on 2020-11-08 00:44  卖肾割阑尾  阅读(105)  评论(0编辑  收藏  举报

导航