• 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就顺利通过,之后再子项目打包。

     

  • 相关阅读:
    2020年3月22日
    2021年3月21日
    2021年3月20日
    人件集阅读笔记02
    2021年3月19日
    2021年3月18日
    2021年3月17日
    2021年3月16日
    2021年3月15日
    梦断代码阅读笔记01
  • 原文地址:https://www.cnblogs.com/daxiong225/p/13943154.html
Copyright © 2020-2023  润新知