在实际开发项目中,常常有几种环境,一般情况下最少有三种环境:开发测试正式。

各个环境之间的参数各不相同,比如mysql、等不同环境的host不一样,若每个环境都手动替换环境很容易出错,这里我们利用maven的profile功能切换环境。

项目结构如下

“dev” ---------------> 开发环境

“product”----------->生产环境          开发和生产加载不同的数据库配置文件  db.properties

pom.xml 如下

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<profiles>
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<activation>
<activeByDefault>true</activeByDefault> <!-- 默认加载开发环境的配置文件-->
</activation>
</profile>
<profile>
<id>product</id>
<properties>
<env>product</env>
</properties>
</profile>
<profile>
<id>sandbox</id>
<properties>
<env>sandbox</env>
</properties>
</profile>
</profiles> 。。。。。。。。。。。。。。 <build>
<finalName>ROOT</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
    <plugins>
      <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<resourceEncoding>${project.encoding}</resourceEncoding>
</configuration>
</plugin>
。。。。。。。。。。。。
    </plugins>
</build>

applicationContext.xml 配置如下

${env}  -----> 打包时传入对应的profiles id 就会加载对应的配置文件。

<context:property-placeholder ignore-resource-not-found="false" location="classpath:${env}/db.properties"/> 

通过eclipse 打包, Profiles 输入product ,就会加载 product下的配置文件,默认的是开发环境(在pom的profiles中配置)

使用maven  命令

使用  mvn install -P{profile} 命令打包war
example:     
1、mvn install               没有指定profile,默认为dev
2、mvn install -Pdev -Dmaven.test.skip=true   指定profile为dev并跳过测试

域名购买.com 后缀好域名 

https://mi.aliyun.com/shop/38040

Maven profile 打包分环境加载不同的资源文件的更多相关文章

  1. maven工程,java代码加载resources下面资源文件的路径

    1 通过类加载器加载器, 1. URL resource = TestMain.class.getResource("/18500228040.txt");File file = ...

  2. JAVA加载Properties配置资源文件

    JAVA加载Properties配置资源文件 制作人:全心全意 配置文件(资源文件):以properties作为拓展名的文件 Java代码是如何加载properties文件的? 必须使用Propert ...

  3. 在IIS上新发布的网站,样式与js资源文件加载不到(资源文件和网页同一个域名下)

    在IIS上新发布的网站,网站能打开,但样式与js资源文件加载不到(资源文件和网页是同一个域名下,例如:网页www.xxx.com/index.aspx,图片www.xxx.com/pic.png). ...

  4. 通过maven profile 打包指定环境配置

    背景 最近换了个新公司接手了一个老项目,然后比较坑的是这个公司的项目都没有没有做多环境打包配置,每次发布一个环境都要手动的去修改配置文件.今天正好有空就来配置下. 解决这个问题的方式有很多,我这里挑选 ...

  5. idea 设置加载多个资源文件,显示本地图片

    idea 经常只会设置一个资源路径,这个路径就是项目的路径.但是当要加载的文件处于其他位置时,则需要增加虚拟路径的配置. 如图:第一个是项目路径 第二个是图片路径

  6. maven 不同环境加载不同的properties 文件

    http://haohaoxuexi.iteye.com/blog/1900568 //参考文章 实际项目中pom配置如下 <profiles> <profile> <i ...

  7. Laravel 不同环境加载不同的.env文件

    假设有4个.env文件.分别为 .env.local .env.dev .env.test .env.prd 方式一 第一步:bootstrap\app.php 加入代码 $envs = ['loca ...

  8. spring-boot 加载本地静态资源文件路径配置

    1.spring boot默认加载文件的路径是 /META-INF/resources/ /resources/ /static/ /public/ 这些目录下面, 当然我们也可以从spring bo ...

  9. spring 加载多个资源文件

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.Prop ...

随机推荐

  1. 常用的windows注册表大全

    目录 使系统没有“运行”选项                        1让操作系统无“关闭系统” 选项                    2让操作系统无“注销”选项              ...

  2. 请用漂亮欢呼-------Day38

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/marSmile_tbo/article/details/31108557 周末,双休,疯了两天.敲了 ...

  3. jmeter之Java request报错:java.lang.NoClassDefFoundError: redis/clients/jedis/Jedis

    今天在学习Jmeter的java request,请求内容是连接redis并获取其中的一个字段值.结果在运行时报如下错误: 2018/05/24 13:08:20 ERROR - jmeter.thr ...

  4. Java-小技巧-002-String 转 long

    1.转化 long l = Long.parseLong([String]); 相当于 long l = Long.parseLong([String],10); long l = Long.valu ...

  5. 十天精通CSS3(1)

    什么是CSS3? CSS3是CSS2的升级版本,3只是版本号,它在CSS2.1的基础上增加了很多强大的新功能. 目前主流浏览器chrome.safari.firefox.opera.甚至360都已经支 ...

  6. java map.entry

    我希望要一个ArrayList<Entry>,类似C++中的pair, 但是Map.Entry是个接口,不能实例化,可以像下面这样写 HashMap<Integer, Integer ...

  7. [LeetCode] 101. Symmetric Tree_ Easy tag: BFS

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  8. SQL中常见语句

    SQL中常见语句笔记: --替换字段中的回车符和换行符 ) ), '') --删除表命令 DROP TABLE [dbo].[MGoods_Test] --删除表中数据命令 DELETE FROM [ ...

  9. Logistic Regression Using Gradient Descent -- Binary Classification 代码实现

    1. 原理 Cost function Theta 2. Python # -*- coding:utf8 -*- import numpy as np import matplotlib.pyplo ...

  10. 把 ElasticSearch 当成是 NoSQL 数据库

    Elasticsearch 可以被当成一个 “NoSQL”-数据库来使用么? NoSQL 意味着在不同的环境下存在不同的东西, 而erestingly 它并不是真的跟 SQL 有啥关系. 我们开始只会 ...