用 maven filter 管理不同环境的配置文件
使用 maven profile
一个项目可以部署在不同的环境当中,maven 的 profile 针对不同的环境指定各自的编译方法。在 pom.xml 的 profile 中,可以根据不同的环境定制以下内容:
- <repositories>
- <pluginRepositories>
- <dependencies>
- <plugins>
- <properties>
- <dependencyManagement>
- <distributionManagement>
- <build>
- <defaultGoal>
- <resources>
- <testResources>
- <finalName>
可以设置默认激活的 profile
<profiles>
<profile>
<id>profile-1</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
...
</profile>
</profiles>
build 配置项
build 配置项可以在两处出现:
<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/xsd/maven-4.0.0.xsd">
...
<!-- 项目级别的构建,基础配置 -->
<build>...</build> <profiles>
<profile>
<!-- 特殊的构建 -->
<build>...</build>
</profile>
</profiles>
</project>
以下是 build 的详细配置
<build>
<defaultGoal>install</defaultGoal>
<directory>${basedir}/target</directory>
<finalName>${artifactId}-${version}</finalName>
<filters> <!-- 过滤器,用于过滤resource中的各个文件 -->
<filter>filters/filter1.properties</filter>
</filters>
<resources>
<resource>
<targetPath>META-INF/plexus</targetPath>
<filtering>false</filtering> <!-- 是否使用过滤器 -->
<directory>${basedir}/src/main/plexus</directory>
<includes>
<include>configuration.xml</include>
</includes>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
</resource>
</resources>
<testResources>
...
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.0</version>
<extensions>false</extensions> <!-- 是否使用扩展 -->
<inherited>true</inherited> <!-- 是否可继承 -->
<configuration> <!-- 当前插件的配置 -->
<classifier>test</classifier>
</configuration>
<dependencies>...</dependencies>
<executions> <!-- 配置插件在哪个阶段使用 -->
<execution>
<id>echodir</id>
<goals>
<goal>run</goal>
</goals>
<phase>verify</phase>
<inherited>false</inherited>
<configuration>
<tasks>
<echo>Build Dir: ${project.build.directory}</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<sourceDirectory>${basedir}/src/main/java</sourceDirectory>
<scriptSourceDirectory>${basedir}/src/main/scripts</scriptSourceDirectory>
<testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>
<outputDirectory>${basedir}/target/classes</outputDirectory>
<testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory>
<extensions> <!-- 通过扩展来修改插件的行为 -->
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ftp</artifactId>
<version>1.0-alpha-3</version>
</extension>
</extensions>
</build>
filter 规则
maven 通过过滤器来修改部署时的不同配置。部署时的所有资源的配置,如果根据环境不同,有不同的配置,则需要在资源中加上以下形式的标记:
${tag.subtag}
如,在 spring.xml 中要配置上传文件的路径:
<beans>
<bean id="uploadService" class="com.oist.project.service.UploadServiceImpl">
<property name="uploadDir" value="${spring.uploadDir}"/>
</bean>
</beans>
在 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/xsd/maven-4.0.0.xsd">
...
<build>
<filters> <!-- 指定 filter -->
<filter>src/main/filters/${deploy.env}.properties</filter>
</filters>
<resources>
<resource> <!-- spring.xml 应该在 src/main/resource 目录下 -->
<filtering>true</filtering> <!-- 是否使用过滤器 -->
</resource>
</resources>
</build> <profiles>
<profile>
<id>development</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<propertys>
<deploy.env>develop</deploy.env>
</propertys>
</profile> <profile>
<id>test</id>
<propertys>
<deploy.env>test</deploy.env>
</propertys>
</profile> <profile>
<id>production</id>
<propertys>
<deploy.env>production</deploy.env>
</propertys>
</profile> </profiles>
</project>
然后就可以针对不同的环境设置不同的目录了:
src/main/filters/develop.properties 文件
# 上传路径:
spring.uploadDir=c:/uploadDir
src/main/filters/test.properties 文件
# 上传路径:
spring.uploadDir=/tmp/upload_dir
src/main/filters/production.properties 文件
# 上传路径:
spring.uploadDir=/app/project/upload_dir
如果配置了多个 filter,并且两个 filter 中有相同的 key,则后面的 value 为最终取值。
<build>
<filters>
<filter>src/main/filters/production.properties</filter>
<filter>src/main/filters/test.properties</filter>
</filters>
</build>
如以上的配置,因为 test.properties 在最后,因而 spring.uploadDir 为 test.properties 的取值 /tmp/upload_dir
maven 的 properties 加载顺序
- <build><filters> 中的配置
- pom.xml 中的 <properties>
- mvn -Dproperty=value 中定义的 property
相同 key 的 property,以最后一个文件中的配置为最终配置。
利用这一规则,可以在打升级 war 包的时候,不将 lib/*.jar 打进 war 包。
<project>
...
<properties>
<lib.exclude>abc.jar</lib.exclude>
</properties>
...
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<packagingExcludes>WEB-INF/lib/${lib.exclude}.jar</packagingExcludes>
</configuration>
</plugin>
</build>
</project>
打第一个 war 包的时候,可以使用以下命令:
mvn clean compile war:war -Pproduction
今后要升级,不用再把 lib 打进 war 包(这样可以使得 war 包体积减少很多),可以使用以下的命令:
mvn clean compile war:war -Pproduction -Dlib.execlude=*
用 maven filter 管理不同环境的配置文件的更多相关文章
- jenkins配置maven工程指定不同环境的配置文件打包
打包命令 这里我们指定配置文件问test 这个是在pom.xml里面定义的, 里面有test,production和devlop三个定义 在不同环境使用Jenkins的时候,-P后面加上不同的参数 我 ...
- Maven Filter与Profile隔离生产环境与开发环境
Maven Filter与Profile隔离生产环境与开发环境 在不同的开发阶段,我们一般用到不同的环境,开发阶段使用开发环境的一套东西,测试环境使用测试环境的东西,可能有多个测试环境,生产环境使用的 ...
- 使用maven来管理您的java项目
maven是一个项目管理工具,使用maven可以自动管理java项目的整个生命周期,包括编译.构建.测试.发布和报告等.在大型项目开发中,使用maven来管理是必不可少的. 一.安装maven 1.W ...
- maven工程的多环境配置方案(profile)
前言: 写一篇水文来打发下时间吧^_^. 在应用开发中, 总会遇到开发/测试/预发布/线上环境, 其环境不同, 其具体的配置项也有所不同, 因此如何快速的切换各个环境配置, 进行打包配置, 成了一个小 ...
- maven filter插件只替换了部分变量问题
maven filter简介 maven的resources插件,有一个filter的作用,能够在打包的时候,从特定文件里读取key-value对,替换配置文件中的占位符变量.很多线上线下有不同环境的 ...
- Maven根据不同的环境打包不同的配置
前言: 在开发过程中,我们的软件会面对不同的运行环境,比如开发环境.测试环境.生产环境,而我们的软件在不同的环境中,有的配置可能会不一样,比如数据源配置.日志文件配置等等. 那么就需要借助maven提 ...
- 【Tool】Windows系统安装Maven依赖管理工具
安装Maven依赖管理工具 官网下载地址:http://maven.apache.org/download.cgi 系统环境要求: [JDK]Maven3.3版本+需要JDK1.7版本以上支持 [内存 ...
- 使用maven profile实现多环境可移植构建(转自CSDN)
使用maven profile实现多环境可移植构建 标签: maven profilemaven自动构建maven自动部署maven可移植构建持续集成 2014-04-25 23:37 26905人阅 ...
- Eclipse+Tomcat+MAVEN+SVN项目完整环境搭建
1.JDK的安装 首先下载JDK,这个从sun公司官网可以下载,根据自己的系统选择64位还是32位,安装过程就是next一路到底.安装完成之后当然要配置环境变量了. ————————————————— ...
随机推荐
- 如何快速找到排好序的数组中最先不连续的数字N
现在有一大堆自然数组成的小到大数组arr,其中会有123456910 这样就要找到6(最先不连续的数字) 举例:[12356789] 找到3 [012345678] 找到8 第一种:遍历数组判断是否 ...
- java 读写properties (配置)文件
Properties属性文件在Java应用程序中是经常可以看得见的,也是特别重要的一类文件.它用来配置应用程序的一些信息,不过这些信息一般都是比较少的数据,没有必要使用数据库文件来保存,而使用一般的文 ...
- Theano 学习笔记(一)
Theano 学习笔记(一) theano 为什么要定义共享变量? 定义共享变量的原因在于GPU的使用,如果不定义共享的话,那么当GPU调用这些变量时,遇到一次就要调用一次,这样就会花费大量时间在数据 ...
- 利用Photoshop修改图片以达到投稿要求
摘自:http://www.dxy.cn/bbs/thread/8602152#8602152 利用Photoshop修改图片以达到投稿要求 软件版本为Photoshop CS V8.0.1(中文版) ...
- 2019年台积电进军AR芯片,将用于下一代iPhone
近日,有报道表示台积电10nm 芯片可怜的收益率可能会对 2017 年多款高端移动设备的推出产生较大的影响,其中自然包括下一代 iPhone 和 iPad 机型.不过,台积电正式驳斥了这一说法,表明1 ...
- 读取hdfs文件之后repartition 避免数据倾斜
场景一: api: textFile("hfds://....").map((key,value)).reduceByKey(...).map(实际的业务计算逻辑) 场景:hdf ...
- CodeSimth-.NetFrameworkDataProvider可能没有安装。解决方法
原文地址:http://www.haogongju.net/art/2561889 1.下载System.Data.SQLite驱动:注意:根据自己的CPU选择是32位还是64位的驱动.建议选择4.0 ...
- SqlServer windowss身份登陆和sa身份登陆
今天重新装了系统,但是计算机名变了,于是修改了计算机名,然后装了SQLSEVER,安装完成后登录,发现无论用WINDOWS身份还是SQLSERVER身份都登录不了 1.先说说sqlserver身份登录 ...
- e.preventDefault() e.stopPropagation()和return false的区别
e.preventDefault(); //阻止事件的默认行为,比如a标签的转向,但不阻止事件的冒泡传播e.stopPropagation() //阻止事件的冒泡传播,但不阻止其默认行为returne ...
- 深入理解JavaScript中 fn() 和 return fn() 的区别
在js中,经常会遇到在函数里调用其它函数的情况,这时候会有 fn() 这种调用方式,还有一种是 return fn() 这种调用方式,一些初学者经常会一脸萌逼地被这两种方式给绕晕了.这里用一个优雅的面 ...