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. python库常用函数学习

    os.path #返回标准化的绝对路径,基本等同于normpath() os.path.abspath(path) #返回文件名 os.path.basename(path) #返回目录名 os.pa ...

  2. 初识 jquery.simulate.js 模拟键盘事件

    用jquery 和 jquery.simulate.js 实现模拟键盘事件,点击上下左右div相当于点击键盘的上下左右键 <!DOCTYPE html> <html> < ...

  3. Python读取和写入文件

    1 从文件中读取数据 1.1 读取整个文件 创建名为test的txt文本文件,添加内容如下所示: 123456789023456789013456789012 实现代码: with open('tes ...

  4. PySide2的This application failed to start because no Qt platform plugin could be initialized解决方式

    解决PySide2的This application failed to start because no Qt platform plugin could be initialized问题 今天在装 ...

  5. python之迭代器 生成器 枚举 常用内置函数 递归

    迭代器 迭代器对象:有__next__()方法的对象是迭代器对象,迭代器对象依赖__next__()方法进行依次取值 with open('text.txt','rb',) as f: res = f ...

  6. disruptor 核心概念 二

    一.Disruptor图解 二.disruptor核心概念 1.RingBuffer到底是啥?正如名字所说的一样,他是一个环(首尾相接的环)它用做在不同上下文(线程)间传递数据的buffer Ring ...

  7. 基于springcloud搭建项目-Hystrix篇(五)

    1.概述 (1).首先要知道分布式系统面临的问题复杂分布式体系结构中应用程序有数十个依赖关系,每个依赖关系在某些时候将不可避免的失败 (2).服务雪崩 多个服务之间相互调用的时候,假设微服务A调用微服 ...

  8. redis如何在spring里面的bean配置

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  9. 大数据篇:ElasticSearch

    ElasticSearch ElasticSearch是什么 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口. ...

  10. .Net Core 依赖注入手记

    .Net Core自身提供了一套简单的DI框架,能满足我们DI基本的需求.它依赖以下组件,需要从Nuget包下拉取. Microsoft.Extensions.DependencyInjection. ...