Maven配置分离 开发,测试,生产 环境配置
-
对应场景
-
在很多情况下,sql、redis、MongoDB等数据库分布在多台服务器上,根据使用情况分为 开发,测试,生产等,而我们需要将写好的代码进行打包,放在不同的服务器上,根据不同的情况,需要连接不同库,频繁的修改数据进行打包,比较麻烦,所以这里通过配置文件根据不同的命令进行打包,就更加方便了
-
-
实现方式:
-
代码实现:
-
1.首先在pom.xml文件中加入maven的插件
-
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.8</version> <executions> <execution> <!--打包前执行--> <id>attach-copy-dependencies</id> <phase>prepare-package</phase> </execution> </executions> </plugin> </plugins> <resources> <!-- 先指定 src/main/resources下所有文件及文件夹为资源文件 --> <resource> <directory>src/main/resources</directory> <includes> <include>**/*</include> </includes> </resource> <!-- 设置对redis.properties进行过虑,即这些文件中的${key}会被替换掉为真正的值 --> <resource> <directory>src/main/resources</directory> <includes> <!-- 这里是对应要修改的properties文件 --> <include>db.properties </include> </includes> <filtering>true</filtering> </resource> </resources> </build> <profiles> <profile> <id>dev</id> <activation> <!-- 这里dev为默认的加载文件 --> <activeByDefault>true</activeByDefault> </activation> <build> <filters> <!-- ${project.basedir} 是maven的标识,代表当前文件项目 --> <filter>${project.basedir}/src/main/filters/dev-filter.properties</filter> </filters> </build> </profile> <profile> <id>uat</id> <build> <filters> <filter>${project.basedir}/src/main/filters/uat-filter.properties</filter> </filters> </build> </profile> <profile> <id>prod</id> <build> <filters> <filter>${project.basedir}/src/main/filters/pro-filter.properties</filter> </filters> </build> </profile> </profiles>
-
这里介绍一哈:三个文件代表的意义:
-
dev: 表示开发环境,一般存储开发的数据
-
uat: 表示测试环境,用来存放测试数据,一般的话有其他两个就够了,
-
pro: 表示生产环境,是用来做上线使用的数据
-
-
这里的主要意思就是讲项目中的properties文件与插件中我们生成的文件进行关联,
-
-
2、将属性集文件进行关联
-
db.properties文件
-
jdbc.user=${jdbc.user} jdbc.password=${jdbc.password}
-
-
dev-filter.properties文件
-
jdbc.user=test jdbc.password=VozPS7GTAf33NcRdUlw==
-
-
pro-filter.properties文件
-
jdbc.user=test_one jdbc.password=VozPS7GTAf33NcsfdsfRdUlw==
-
-
这里的意义就是说,在我们需要打包的时候,可以通过maven的打包命令动态的将不同的数据库连接放入到我们打的war包中,那样就不需要我们手动去修改数据了
-
-
-
打包:
-
1.首先就是进入项目所在的目录,
-
2.根据项目层次进行依次打包
-
这里涉及到两个基本命令:
-
jar包:
-
D:\aiot-manager-web> mvn clean install -Dmaven.test.skip
-
-
war包:
-
D:\aiot-manager-web>mvn clean install -Dmaven.test.skip -Pprod
-
-
-
-
去对应的目录下找到war包就可以了
-
-