coding++:maven根据不同的运行环境,打包不同的配置文件
1、使用maven管理项目中的依赖,非常的方便。同时利用maven内置的各种插件,在命令行模式下完成打包、部署等操作,可方便后期的持续集成使用。
2、但是每一个maven工程(比如web项目),开发人员在开发时,会使用一种配置文件,比如数据库配置,而测试环境可能使用另一种配置文件。
3、打包完成后,手动调整配置文件,工作重复度很高,因此查找方法,实现“maven根据不同的运行环境,打包不同的配置文件”的目的。
pom.xml中增加插件:
主要关注标红的插件maven-resources-plugin,以及标红的配置部分。
注意:目前经过测试,发现resources目录文件拷贝会在validation阶段之后compile阶段之前执行,为了保证指定环境的配置文件在resources目录拷贝之后执行,使用compile阶段;
overwrite设置为true,强制覆盖原有文件。
使用maven-resources-plugin插件,在compile阶段实现指定目录中配置文件的拷贝操作。
<build>
<finalName>Lantech</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 不同环境的配置文件选择 (也可忽略此插件:因为小编目前没发现有啥用)-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>compile</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<!-- 覆盖原有文件 -->
<overwrite>true</overwrite>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<!-- 也可以用下面这样的方式(指定相对url的方式指定outputDirectory) <outputDirectory>target/classes</outputDirectory> -->
<!-- 待处理的资源定义 -->
<resources>
<resource>
<!-- 指定resources插件处理哪个目录下的资源文件 -->
<directory>src/main/resources/${package.environment}</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
<inherited>true</inherited>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
pom.xml中增加profiles配置(必写)
使用profiles可为maven命令执行时,激活不同的变量,并依据此变量同上述的插件配合,完成指定目录中配置文件拷贝操作。
<profiles>
<profile>
<id>dev</id>
<properties>
<package.environment>dev</package.environment>
<com.send.ip>39.105.100.186</com.send.ip>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<package.environment>test</package.environment>
<com.send.ip>39.105.100.187</com.send.ip>
</properties>
</profile>
<profile>
<id>product</id>
<!-- 是否默认 true表示默认-->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<package.environment>product</package.environment>
<com.send.ip>39.105.100.189</com.send.ip>
</properties>
</profile>
执行打包命令:
mvn clean package
mvn clean package -Pdev
mvn clean package -Ptest
mvn clean package -Pproduct
执行命令,指定-P参数,启用指定的profile。
项目中测试:

PropertyConfig:引入 *.properties 文件
package com.mlq.coding.sendenvironmentproject.configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource; @Configuration
@PropertySource(ignoreResourceNotFound = true, value = {"classpath:config/remote-settings.properties"})
//@ImportResource({"classpath:dubbo/*.xml"})
public class PropertyConfig {
}
TestController:访问测试
package com.mlq.coding.sendenvironmentproject.controller; import com.mlq.coding.sendenvironmentproject.Service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class TestController { @Autowired
private TestService testService; @RequestMapping("/iptest")
public void ipTest() {
testService.show();
} }
TestService:测试接口
package com.mlq.coding.sendenvironmentproject.Service;
public interface TestService {
void show();
}
TestServiceImpl:测试接口实现
package com.mlq.coding.sendenvironmentproject.Service.impl; import com.mlq.coding.sendenvironmentproject.Service.TestService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; @Service
public class TestServiceImpl implements TestService{ //获取配置文件中的 ip
@Value("${com.send.test.ip}")
private String IP; @Override
public void show() {
System.out.println("返回参数为:{}"+IP);
}
}
remote-settings.properties:获取不同环境下的 ip
############################################
# system config
############################################
# @com.send.ip@ 是在pom文件中配置的
com.send.test.ip=@com.send.ip@
coding++:maven根据不同的运行环境,打包不同的配置文件的更多相关文章
- maven根据不同的运行环境,打包不同的配置文件
使用maven管理项目中的依赖,非常的方便.同时利用maven内置的各种插件,在命令行模式下完成打包.部署等操作,可方便后期的持续集成使用. 但是每一个maven工程(比如web项目),开发人员在开发 ...
- maven根据不同的运行环境,打包不同的配置文件(转载)
使用maven管理项目中的依赖,非常的方便.同时利用maven内置的各种插件,在命令行模式下完成打包.部署等操作,可方便后期的持续集成使用. 但是每一个maven工程(比如web项目),开发人员在开发 ...
- Maven插件之portable-config-maven-plugin(不同环境打包)
在大型的项目组中,分不同的开发环境,测试环境,生产环境(说白了就是配置文件不同,或者数据源,或者服务器,或者数据库等);问题来了,如何使用Maven针对不同的环境来打包呢? Maven提供了Profi ...
- maven 通过 profile 设置多环境打包
maven 在设计之初就考虑到了业务代码和测试代码的分开存放.将业务代码默认存放在 src/main 下,将测试代码放在 src/test 下,然后在各自目录下再细分 java 与 res ...
- Maven 如何为不同的环境打包 —— 开发、测试和生产环境
在开发过程中,我们的软件会面对不同的运行环境,比如开发环境.测试环境.生产环境,而我们的软件在不同的环境中,有的配置可能会不一样,比如数据源配置.日志文件配置.以及一些软件运行过程中的基本配置,那每次 ...
- Vue项目根据不同运行环境打包项目
前提 项目是直接通过 vue-cli脚手架 生成的: 假设在项目开发中,分为三个环境 -- · 测试环境· 预生产环境· 生产环境 每个环境的接口地址都是不同的,而 vue-cli 给出的环境只有 d ...
- Maven根据不同的环境打包不同的配置
前言: 在开发过程中,我们的软件会面对不同的运行环境,比如开发环境.测试环境.生产环境,而我们的软件在不同的环境中,有的配置可能会不一样,比如数据源配置.日志文件配置等等. 那么就需要借助maven提 ...
- Spring Boot项目使用maven-assembly-plugin根据不同环境打包成tar.gz或者zip
spring-boot-assembly 在spring boot项目中使用maven profiles和maven assembly插件根据不同环境打包成tar.gz或者zip 将spring bo ...
- Maven适配多种运行环境的打包方案
项目从开发到部署会历经多个运行环境,如开发环境.测试环境和生产环境,不同环境中项目的配置文件通常也会不同,典型的如数据库连接配置.我们当然不希望每次部署打包前都去修改配置文件以适配环境,利用Maven ...
随机推荐
- APPium+Python+iOS屏幕滑动方法对比
最近在学习appium自动化,对iOS手机进行滑动操作进行总结: 1.mobile:scroll;该方法在实际使用调用时,会滚动2次.执行时间很长. 向下滚动整个屏幕driver.execute_sc ...
- Django+Nginx+Uwsgi环境配置
nginx.conf : server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.acces ...
- SIP 协议详解
SIP 协议详解 2013年参与过一个"视频通讯的App"项目,使用Sip协议通信.当时通信协议这块不是自己负责,加上时间紧.任务重等方面的原因,一直未对Sip协议进行过深入的了解 ...
- JZOJ 1154. 【GDOI2003】购物
1154. [GDOI2003]购物 (Standard IO) Time Limits: 1000 ms Memory Limits: 65536 KB Description GDOI商场推出优惠 ...
- 个人理解TCP中SYN Cookie
说起SYN Cookie还是得从TCP3次握手开始说起,先给出计网的体系结构图 然后解释一下SYN,seq,ack,ACK的相关名词 SYN(建立连接) ACK(确认后全部为1) PSH(传送) FI ...
- Js逆向-滑动验证码图片还原
本文列举两个例子:某象和某验的滑动验证 一.某验:aHR0cHM6Ly93d3cuZ2VldGVzdC5jb20vZGVtby9zbGlkZS1mbG9hdC5odG1s 未还原图像: 还原后的图: ...
- python小白入门
阅读目录 一python介绍 二安装python解释器 三第一个python程序 四变量 五用户与程序交互 六基本数据类型 七格式化输出 八基本运算符 九流程控制之if...else 十流程控制之wh ...
- 2019-2020-2 20175226 王鹏雲 网络对抗技术 Exp2 后门原理与实践
2019-2020-2 20175226 王鹏雲 网络对抗技术 Exp2 后门原理与实践 实验内容 使用netcat获取主机操作Shell,cron启动: 使用socat获取主机操作Shell, 任务 ...
- 【小程序】---- input获得焦点时placeholder重影BUG
问题小程序的input组件有个自身的bug,即当输入框获取焦点时placeholder内容会出现重影现象. 解决思路原理:将placeholder内容单独写在另外的标签里,控制其显示隐藏.操作:将代表 ...
- django 从零开始 9 自定义密码验证加密
先上想法,想对数据库账号的密码进行一个加密,但是django文档中的加密方法set_password貌似是只针对他们默认的user模型 或者继承 AbstractBaseUser的模型有效 from ...