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根据不同的运行环境,打包不同的配置文件的更多相关文章

  1. maven根据不同的运行环境,打包不同的配置文件

    使用maven管理项目中的依赖,非常的方便.同时利用maven内置的各种插件,在命令行模式下完成打包.部署等操作,可方便后期的持续集成使用. 但是每一个maven工程(比如web项目),开发人员在开发 ...

  2. maven根据不同的运行环境,打包不同的配置文件(转载)

    使用maven管理项目中的依赖,非常的方便.同时利用maven内置的各种插件,在命令行模式下完成打包.部署等操作,可方便后期的持续集成使用. 但是每一个maven工程(比如web项目),开发人员在开发 ...

  3. Maven插件之portable-config-maven-plugin(不同环境打包)

    在大型的项目组中,分不同的开发环境,测试环境,生产环境(说白了就是配置文件不同,或者数据源,或者服务器,或者数据库等);问题来了,如何使用Maven针对不同的环境来打包呢? Maven提供了Profi ...

  4. maven 通过 profile 设置多环境打包

    maven 在设计之初就考虑到了业务代码和测试代码的分开存放.将业务代码默认存放在  src/main  下,将测试代码放在  src/test  下,然后在各自目录下再细分  java  与 res ...

  5. Maven 如何为不同的环境打包 —— 开发、测试和生产环境

    在开发过程中,我们的软件会面对不同的运行环境,比如开发环境.测试环境.生产环境,而我们的软件在不同的环境中,有的配置可能会不一样,比如数据源配置.日志文件配置.以及一些软件运行过程中的基本配置,那每次 ...

  6. Vue项目根据不同运行环境打包项目

    前提 项目是直接通过 vue-cli脚手架 生成的: 假设在项目开发中,分为三个环境 -- · 测试环境· 预生产环境· 生产环境 每个环境的接口地址都是不同的,而 vue-cli 给出的环境只有 d ...

  7. Maven根据不同的环境打包不同的配置

    前言: 在开发过程中,我们的软件会面对不同的运行环境,比如开发环境.测试环境.生产环境,而我们的软件在不同的环境中,有的配置可能会不一样,比如数据源配置.日志文件配置等等. 那么就需要借助maven提 ...

  8. Spring Boot项目使用maven-assembly-plugin根据不同环境打包成tar.gz或者zip

    spring-boot-assembly 在spring boot项目中使用maven profiles和maven assembly插件根据不同环境打包成tar.gz或者zip 将spring bo ...

  9. Maven适配多种运行环境的打包方案

    项目从开发到部署会历经多个运行环境,如开发环境.测试环境和生产环境,不同环境中项目的配置文件通常也会不同,典型的如数据库连接配置.我们当然不希望每次部署打包前都去修改配置文件以适配环境,利用Maven ...

随机推荐

  1. vue之initComputed模块源码说明

    要想理解原理就得看源码,最近网上也找了好多vue初始化方法(8个init恶魔...) 因为也是循序渐进的理解,对initComputed计算属性的初始化有几处看得不是很明白,网上也都是含糊其辞的(要想 ...

  2. 自定义checkbox, radio样式总结

    任务目的 深入了解html label标签 了解CSS边框.背景.伪元素.伪类(注意和伪元素区分)等属性的设置 了解CSS中常见的雪碧图,并能自己制作使用雪碧图 任务描述 参考 样例(点击查看),实现 ...

  3. 神奇的 SQL 之 ICP → 索引条件下推

    开心一刻 楼主:来,我们先排练一遍 小伙伴们:好 嘿.哈.嚯 楼主:非常好,就是这个节奏,我们开始吧 楼主:啊.啊.啊,疼 ! 你们是不是故意的 ? 回表与覆盖索引 正式讲 ICP 之前了,我们先将相 ...

  4. vmware企业虚拟化平台vSphere管理与配置

    ├─1-CCIE-DC课程介绍.avi ├─2-vSphere-简介.avi ├─3-vSphere-新功能介绍.avi ├─4-vSphere-授权介绍.avi ├─5-vSphere-课程拓扑介绍 ...

  5. 初学react

    React特点: 声明式设计:建议使用JSX来描述用户界面;构建组件:单向响应的数据流: JSX:JSX是一种JAVASCRIPT的语法扩展,元素是构成react的最小单位,JSX就是用来声明REAC ...

  6. 使用MySql对IdentityServer4进行持久化

    哈喽大家好,看见网上很少有使用MySql进行持久化的,毕竟又很多坑,说句实话,就连 MySql.Data.EntityFrameworkCore 都有问题,不知道是.net core更新太快还是其它的 ...

  7. 曹工杂谈:花了两天时间,写了一个netty实现的http客户端,支持同步转异步和连接池(1)--核心逻辑讲解

    背景 先说下写这个的目的,其实是好奇,dubbo是怎么实现同步转异步的,然后了解到,其依赖了请求中携带的请求id来完成这个连接复用:然后我又发现,redisson这个redis客户端,底层也是用的ne ...

  8. What is the difference between shades and shadows?

    Shade is the darkness of an object not in direct light, while shadows are the silhouette of an objec ...

  9. vue相关的前端UI库

    1,element-ui 这个笔者用的最多,但是官网不知道咋回事.打不开,难道被黑了?! 地址(http://element-ui.cn/#/zh-CN/component/installation) ...

  10. Centos7安装Elasticsearch和Kibana

    这里使用的6.6.0版本,ES需要JDK环境,对应1.8 Elasticsearch安装: 1.下载:https://elasticsearch.cn/download/ 2.解压: 3.修改配置:j ...