maven 处理资源文件的方式

maven 默认情况下,对/main/resources/下的资源文件全部复制到target/classes下,然后打包,对于需要依赖环境或可变参数进行打包时,
如下的环境信息:
dev 开发环境
sit sit测试环境
uat uat测试环境
pre 预生产测试环境
prod 线上的生产环境
各环境的数据库配置完全不一样,还有一些配置,是依赖对应环境的内部系统,如在uat环境布署
的电子商务系统,就需要配置uat环境的订单处理系统,而不能调用dev环境的订单系统.

对于这种需求,有两种处理方式:


[b]第一种,在proterties文件里使用${}变量,使用maven的filter进行赋值[/b]
[url]http://maven.apache.org/shared/maven-filtering/[/url]
[url]http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html[/url]

//test.properties
jdbcname=${user_name}
jdbcpasswd=${user_passwd}

这种变量在打包时,由maven从另一个依赖环境的配置文件里读出并赋值
比如


//在目录main/resources/sit/下
//env.properties
user_name=user_sit
user_passwd=passwd_sit
ctx=somepath



//在目录main/resources/uat/下
//env.properties
user_name=user_uat
user_passwd=passwd_uat
ctx=

......
配置五个对应的配置,然后在build配置里配置filter

<build>

...

<filters>
<filter>src/main/resources/${env}/env.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>

...

</build>



然后添加maven profile配置,每个环境对应一个profile

<profiles>
<!-- dev开发环境,默认激活 -->
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<activation>
<activeByDefault>true</activeByDefault><!--默认启用的是dev环境配置 -->
</activation>
</profile>
<!-- sit测试环境 -->
<profile>
<id>sit</id>
<properties>
<env>sit</env>
</properties>
</profile>
<!-- uat测试环境 -->
<profile>
<id>uat</id>
<properties>
<env>uat</env>
</properties>
</profile>
<!-- pre预生产测试环境 -->
<profile>
<id>pre</id>
<properties>
<env>pre</env>
</properties>
</profile>
<!-- prod生产环境 -->
<profile>
<id>prod</id>
<properties>
<env>prod</env>
</properties>
</profile>
</profiles>



//打包命令 打包uat环境的配置
mvn clean package -Puat

这时最终的配置文件test.properties里的属性会被替换为


//test.properties
jdbcname=user_uat
jdbcpasswd=passwd_uat



[url=http://stackoverflow.com/questions/27813405/maven-filtering-files-under-src-main-webapp-jsp-while-building-the-war]stackoverflow里web资源的替换[/url]
对于web资源,比如放到webapp目录下的js文件,不同的环境,需要用到不同的文件,可以使用
maven-war-plugin在打包时进行配置,
比如,我们在src/main/resources/目录下新建web目录,用于存放需要复制到webapp目录里的文件
比如我有个config.json文件,这个文件在dev sit uat prod 环境里都不一样
最终webapp目录结构如下:

├─META-INF
├─static
│ ├─bootstrap
│ │ └─2.3.2
│ │ ├─css
│ │ │ bootstrap-responsive.min.css
│ │ │ bootstrap.min.css
│ │ │
│ │ ├─img
│ │ │ glyphicons-halflings-white.png
│ │ │ glyphicons-halflings.png
│ │ │
│ │ └─js
│ │ bootstrap.min.js
│ │
│ ├─images
│ │ favicon.ico
│ │
│ ├─jquery
│ │ config.json //该文件在各个目录里的配置都不一样
│ │ jquery-1.9.1.min.js
│ │
│ ├─jquery-validation
│ │ └─1.11.1
│ │ │ jquery.validate.min.js
│ │ │ messages_bs_zh.js
│ │ │ validate.css
│ │ │
│ │ └─images
│ │ unchecked.gif
│ │
│ └─styles
│ default.css


config.json文件内容为

{
prefix:"old value",
path:"/upload/path"
}




创建src/main/resources/web/资源目录,目录结构如下:

└─static
└─jquery
config.json

config.json文件内容为

{
prefix:"${ctx}",
path:"/upload/path"
}




<!-- war打包插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warName>${project.artifactId}</warName>
<filters>
<filter>src/main/resources/env/application-${env}.properties</filter>
</filters>
<webResources>
<resource>
<directory>src/main/resources/web</directory>
<filtering>true</filtering>
</resource>
</webResources>

</configuration>
</plugin>


在执行 mvn package -Pprofile命令时,config.json里变量的值会被相应的替换和合适的值

第二种,使用maven-resources-plugin 的copy方式,将对应环境的配置对整个目录和文件进行覆盖。
[url]http://maven.apache.org/plugins/maven-resources-plugin/examples/copy-resources.html[/url]

<profiles>
<!-- dev开发环境,默认激活 -->
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<activation>
<activeByDefault>true</activeByDefault><!--默认启用的是dev环境配置 -->
</activation>
</profile>
<!-- sit测试环境 -->
<profile>
<id>sit</id>
<properties>
<env>sit</env>
</properties>
</profile>
<!-- uat测试环境 -->
<profile>
<id>uat</id>
<properties>
<env>uat</env>
</properties>
</profile>
<!-- pre预生产测试环境 -->
<profile>
<id>pre</id>
<properties>
<env>pre</env>
</properties>
</profile>
<!-- prod生产环境 -->
<profile>
<id>prod</id>
<properties>
<env>prod</env>
</properties>
</profile>
</profiles>



<build>

....

<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes/</outputDirectory>
<resources>
<resource>
<directory>src/main/resources/${env}/</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

....

</build>


//打包命令 打包uat环境的配置
mvn clean package -Puat
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值