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. 一文看懂js中元素的客户区大小(clientWidth,clientHeight)

    元素的客户区 元素的客户区大小,指的是元素内容及其内边距所占据的空间大小. 相关属性如下: 1. clientWidth:元素内容区宽度+元素左右内边距 2. clientHeight:元素内容区高度 ...

  2. 微信小程序学习简介

    如何向微信小程序导入DEMO源码: 参考方法 参考学习小程序官方文档 小程序官方文档 小程序目录简介 app.json :设置一些工程全局的量.js : 写一些函数逻辑.wxml: 调用.js中写的函 ...

  3. 关于Spring和SpringMVC的总结

    1.Spring中AOP的应用场景.AOP原理.好处? 答:AOP:Aspect Oriented Programming面向切面编程:用来封装横切关注点,具体可以在下面场景中使用: Authenti ...

  4. 基于Linux Lite 4.8制作“Windows 12 Lite”正式发布

    值得注意的是,这款Windows 12 Lite的宣传标语将矛头直指Windows 10,声称不会迫使用户更新.不会存在病毒或恶意软件.启动速度比Win 10快3倍.提供8万多款优质软件.允许与Win ...

  5. 032.核心组件-kube-proxy

    一 kube-proxy原理 1.1 kube-proxy概述 Kubernetes为了支持集群的水平扩展.高可用性,抽象出了Service的概念.Service是对一组Pod的抽象,它会根据访问策略 ...

  6. mysql那些事之索引篇

    mysql那些事之索引篇 上一篇博客已经简单从广的方面介绍了一下mysql整体架构以及物理结构的内容. 本篇博客的内容是mysql的索引,索引无论是在面试还是我们日常工作中都是非常的重要一环. 索引是 ...

  7. C结构体与链表

    今天来总结C语言的学习盲点--结构体,为了不显单一,也为了补足作者链表的编程缺陷,特更此博文,总结近段时间的学习成果.话不多说,先上一段代码 struct none{int item; link ne ...

  8. EOS基础全家桶(一)开篇

    简介 从今天开始我会在FishoPark上与大家分享EOS的一些技术经验和基础,如果大家在看文章的过程中有任何问题,欢迎在网站下方的评论里留言,我会尽力为大家解答,如果发现我内容中所写有错,欢迎指正, ...

  9. Servlet(简介,请求参数,页面跳转,生命周期,创建,配置,ServletContext,线程)

    1.Servlet简介 servlet是java servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序, 主要功能在于交互式浏览和修改数据,生成动态的web内容 服务端运行的 ...

  10. 2019HECTF总结_web题

    奇怪的编码 ♭|§∮♯♭|§∮♬♭|§§♫♭|§∮§♭|§♩§♭|♯♬¶♭|§§♫♭|§§¶♭|♯¶§♭|♯¶♫♭|§∮♭♭|§§♫♭|§§♬♭|♯♬♪♭|♯¶♪♭|♯¶|♭|♯¶♯♭|♯♬♬♭|♯♬ ...