使用Maven自动打包配置文件生成开发环境,测试环境,生产环境

来源:互联网 发布:linux服务器宕机原因 编辑:程序博客网 时间:2024/05/22 02:30

写在前面

项目中往往有开发环境、测试环境、生产环境,这几个环境有些配置文件可能是不一样的,三个环境切换时以前基本是靠手动注释文件来打成不同的包。如果不同的配置文件过多,粗心打包后,就会导致配置文件出错,从而使项目部署出问题。例如Mysql配置文件在项目中有可能会使用如下情况,通过手动注释切换配置文件。

#本地数据库#url:jdbc:mysql://***#username:dev#password:root#线上数据库url:jdbc:mysql://***username:formalpassword:admin#测试数据库#url:jdbc:mysql://**#username:test#password:test

为了解决这个问题,可以使用Maven不同的打包命令生成不同的Jar包

  • 开发环境打包命令
mvn clean install -P dev 
  • 测试环境打包命令
mvn clean install -P test
  • 生产环境打包命令
mvn clean install -P formal

创建普通Maven项目

项目结构如下图所示

这里写图片描述

System.properties 配置文件

app.mode=${app.mode}

app-develop.properties 开发配置文件

app.mode=develop

app-formal.properties 生产配置文件

app.mode=formal

app.test.properties 测试配置文件

app.mode=test

以上配置文件中,System.properties为项目打包后真正使用的文件,其它三个分别存放各自配置文件,当打包时,Maven会将对应的参数填充到System.properties中
注意事项
项目中使用的配置参数,必需在System.properties 中有,并通过${参数名}进行引用

POM 配置文件

 <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.zhaochao</groupId>    <artifactId>TestDemo</artifactId>    <version>1.0-SNAPSHOT</version>    <packaging>jar</packaging>    <name>TestDemo</name>    <url>http://maven.apache.org</url>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    </properties>    <dependencies>        <dependency>            <groupId>com.alibaba</groupId>            <artifactId>fastjson</artifactId>            <version>1.2.21</version>        </dependency>    </dependencies>    <profiles>        <profile>            <id>test</id>            <activation>                <activeByDefault>true</activeByDefault>            </activation>            <properties>                <build.profile.id>test</build.profile.id>            </properties>        </profile>        <profile>            <id>develop</id>            <properties>                <build.profile.id>develop</build.profile.id>            </properties>        </profile>        <profile>            <id>formal</id>            <properties>                <build.profile.id>formal</build.profile.id>            </properties>        </profile>    </profiles>    <build>        <filters>            <filter>src/app-${build.profile.id}.properties</filter>        </filters>        <sourceDirectory>src/main/java</sourceDirectory>        <!-- 定义打包资源文件-->        <resources>            <resource>                <filtering>true</filtering>                <!--加上filter会过滤该资源路径中的文件-->                <directory>${project.basedir}/src/main/resources</directory>            </resource>        </resources>        <plugins>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-shade-plugin</artifactId>                <version>2.4.2</version>                <configuration>                    <createDependencyReducedPom>true</createDependencyReducedPom>                    <filters>                        <filter>                            <artifact>*:*</artifact>                            <excludes>                                <exclude>META-INF/*.SF</exclude>                                <exclude>META-INF/*.DSA</exclude>                                <exclude>META-INF/*.RSA</exclude>                            </excludes>                        </filter>                    </filters>                </configuration>                <executions>                    <execution>                        <phase>package</phase>                        <goals>                            <goal>shade</goal>                        </goals>                        <configuration>                            <transformers>                                <transformer                                        implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>                                <transformer                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">                                    <mainClass>com.zhaochao.Main</mainClass>                                </transformer>                            </transformers>                        </configuration>                    </execution>                </executions>            </plugin>        </plugins>    </build></project>

Main测试函数

public class Main {    public Properties properties;    public String loadProperties(String key) throws IOException {        if (properties == null) {            properties = new Properties();            properties.load(this.getClass().getClassLoader().getResourceAsStream("System.properties"));        }        return properties.getProperty(key);    }    public static void main(String[] args) throws IOException {        Main main = new Main();        System.out.println(main.loadProperties("app.mode"));    }}

测试结果

在Idea中,勾选Maven Projects中->Profiles->develop运行main则是使用开发环境的配置,如下图所示

这里写图片描述

这里写图片描述

这里写图片描述

也可以通过以下命令生成对就的Jar包

mvn clean install -P dev mvn clean install -P testmvn clean install -P formal 

这里写图片描述

阅读全文
1 0
原创粉丝点击