如何为你的Web程序(war包设定配置文件)

约定

http://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html

上面链接说了:

The default resource directory for all Maven projects is src/main/resources which will end up in target/classes and in WEB-INF/classes in the WAR. The directory structure will be preserved in the process.

The WAR Plugin is also capable of including resources not found in the default resource directory through the webResources parameter.

默认是找 src/main/resource 目录的配置作为War包的配置,如果没找到,则可以使用用户自定义的,如下面,用户定义了目录 resource2 作为自定义目录。

<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>src/main/resource</directory>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
...
</project>

例如,如果你的项目目录如下:

 .
|-- pom.xml
|-- resource2
| |-- external-resource.jpg
| `-- image2
| `-- external-resource2.jpg
`-- src
`-- main
|-- java
| `-- com
| `-- example
| `-- projects
| `-- SampleAction.java
|-- resources
| `-- images
| `-- sampleimage.jpg
`-- webapp
|-- WEB-INF
| `-- web.xml
|-- index.jsp
`-- jsp
`-- websource.jsp

那么,执行上面的POM之后,WAR包内容应该如下:

documentedproject-1.0-SNAPSHOT.war
|-- META-INF
| |-- MANIFEST.MF
| `-- maven
| `-- com.example.projects
| `-- documentedproject
| |-- pom.properties
| `-- pom.xml
|-- WEB-INF
| |-- classes
| | |-- com
| | | `-- example
| | | `-- projects
| | | `-- SampleAction.class
| | `-- images
| | `-- sampleimage.jpg
| `-- web.xml
|-- external-resource.jpg
|-- image2
| `-- external-resource2.jpg
|-- index.jsp
`-- jsp
`-- websource.jsp

大家可以看到,这个WAR包不仅有 src/main/resource 目录下的配置,还有 resource2下的配置。

说明,WAR的配置,来自两个地方,一个是默认的,一个是用户添加的。

如何覆盖?

我们最常见的用户场景是:

src/main/resource 目录下有开发的所有配置

src/main/online-resource 目录下有线上配置。它不包含所有配置。它只包含 src/main/resource 目录下那些需要被替换成线上配置的配置文件。也就是 开发和线上配置 的公同配置文件 是不会存在这个目录下的。

那么,我们的想法就很简单了,线上WAR的配置应该包括:

  • 首先,应该包括 src/main/resource 目录下所有文件
  • 然后,使用 src/main/online-resource 覆盖上面这个目录,将线下配置覆盖成线上配置,共同配置保留下来。

如何完成 以上操作呢?

https://github.com/knightliao/disconf/blob/master/disconf-web/pom.xml 这里提供了一个最佳实践:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources> <resource>
<directory>${project.build.online.sourceDir}</directory>
<targetPath>WEB-INF/classes</targetPath>
<includes>
<include>**/*.*</include>
</includes>
</resource> </webResources>
</configuration>
</plugin>

只需要像上面这样配置,Maven帮我们默认执行了第一步(首先,应该包括 src/main/resource 目录下所有文件),我们自定义了第二步,Maven会帮我们覆盖掉。

最新文章

  1. TextView跑马灯效果
  2. visual studio 两个以上sln 引用同一个project ,生成时会改变projectguid问题
  3. Node.js与Sails~自定义响应体responses
  4. android AsyncTask介绍(转)
  5. Android实现简单音乐播放器(MediaPlayer)
  6. PostgreSQL的 initdb 源代码分析之十九
  7. Servlet课程0425(四) Servlet实现简单用户登录验证
  8. jQuery中get与eq的区别
  9. 在Docker上部署使用Azure CLI镜像
  10. ACM博弈问题小试
  11. 查增删改MySQL数据库固定模式
  12. JavaScript的8行代码搞定js文件引入问题
  13. jmeter--001.介绍、下载与安装
  14. 11.c#类的成员初始化顺序
  15. 【转】一个域名是用哪里的DNS来解析的,电脑怎么知道找哪一个DNS呢? 我注册域名的时候会在服务商那里配置DNS解析,一般需要24小时后才能访问,我想知道,解析后的这个数据是不是会同步到世界上所有的DNS服务器呢!如果不是,当我访问我的这个域名的时候,电脑怎么知道去找到我注册的这一家的DNS服务器呢,谁告诉他的呢?
  16. CodeForces - 907A Masha and Bears
  17. rhel7 ifconfig command not found
  18. mysql ssh 端口转发
  19. 深入研究java.lang.Process类
  20. 【BZOJ】1537: [POI2005]Aut- The Bus

热门文章

  1. css3实现jquery mobile的页面过度原理
  2. eclipse偶尔会反映迟钝,直接无视其报错
  3. Linux五种I/O模型性能分析
  4. mysql 开启慢查询记录
  5. Unity又称Unity Application Block
  6. if的另一个实现思路
  7. C语言 &#183; 阿尔法乘积
  8. 基于Discuz的原生态MVC框架Uxf
  9. linux防火墙开关以及端口规则添加
  10. Web API(五):Web API跨域问题