Maven War包 POM配置文件
如何为你的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会帮我们覆盖掉。
Maven War包 POM配置文件的更多相关文章
- Maven将依赖包、jar/war包及配置文件输出到指定目录
使用Maven插件将依赖包 jar包 war包及配置文件输出到指定目录 写在前面 最近遇到一个朋友遇到一个项目需要将 maven 的依赖包和配置文件分开打包然后用脚本执行程序.这样的好处在于可以随 ...
- 利用Maven插件将依赖包、jar/war包及配置文件输出到指定目录
写在前面 最近遇到一个朋友遇到一个项目需要将maven的依赖包和配置文件分开打包然后用脚本执行程序.这样的好处在于可以随时修改配置文件内容及查看jar包.如果将所有打成一个jar包就会有个问题(例 ...
- maven中的pom配置文件标签的详细介绍
<span style="padding:0px; margin:0px"><project xmlns="http://maven.apache.or ...
- maven war包打包去除jar包瘦身
1.pom文件配置 <!-- war包 --> <plugin> <groupId>org.apache.maven.plugins</groupId> ...
- springboot项目打war包pom设置
<build> <finalName>PayManager</finalName><!--打包后的名字PayManager.war--> <plu ...
- maven中的pom配置文件一——spring,mybatis,oracle,jstl,json,文件上传
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- maven项目打包分析及打包后war包缺少配置文件报错的原因分析,使用progard混淆时配置分析
1.maven打包: 一直以来我都没太注意过在myeclipse下使用run as来clean居然对项目的target目录没有进行操作,要让操作有效,需要进入到maven build...选项下,进行 ...
- 如何正确读取war包中配置文件的路径
转自:http://free-chenwei.iteye.com/blog/1507480 下面展示一段代码String path = getClass().getResource("/&q ...
- maven打war包的过程中,都用了哪些插件呢?
一.maven生命周期 http://ifeve.com/introduction-to-the-lifecycle/ https://maven.apache.org/guides/introduc ...
随机推荐
- python引用,浅拷贝,深拷贝
1.引用 传递的是引用,原始列表改变,被赋值列表会同样改变,因为他们指向的是同一个地址. alist = [1,2,3,["a","b"]] blist = a ...
- 20个可能你不知道Linux网路工具
如果要在你的系统上监控网络,那么使用命令行工具是非常实用的,并且对于 Linux 用户来说,有着许许多多现成的工具可以使用,如: nethogs, ntopng, nload, iftop, iptr ...
- oracle中用SQL语句创建和管理表
表名和列名的命名规则: 必须以字母开头 必须在1-30个字符之间 只能包含A-Z,a-z,0-9,_,$,# 不能与用户定义的其它对象重名 不能使用ORACLE的保留字 创建前具备的条件: CREAT ...
- 使用yum时出现的404
今天使用yum升级nss yum update nss 结果爆出错误: http://people.centos.org/tru/devtools-2/6Server/x86_64/RPMS/repo ...
- Python的sys.argv使用说明 通过终端写入环境变量
刚开始使用这个参数的时候,很不明白其含义.网上搜索很多都是贴的官网上面的一则实例,说看懂,就明白.可是,我看不懂.现在在回头看这个参数使用,并不是很麻烦. 举几个小例子就明白了. 创建一个脚本,内容如 ...
- redis未授权
redis描述: Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开发工 ...
- Disable Maven Nature和disable workspace resolution
1.Disable Maven Nature myeclipse更新maven的时候,手抖了一下,点上了Disable Maven Nature,然后工程右键菜单中的Maven栏就不见了! 其实这是把 ...
- C语言 · 字符串逆序
算法训练 字符串逆序 时间限制:1.0s 内存限制:512.0MB 输入一个字符串,长度在100以内,按相反次序输出其中的所有字符. 样例输入 tsinghua 样例输出 auhgn ...
- JavaScrip——DOM操作(属性操作)
Attribute a.setAttribute("属性名","属性值")——设置属性 a.getSttribute("属性名")——获取属 ...
- 轻量级ORM框架Dapper应用三:使用Dapper实现In操作
IN 操作符允许我们在 WHERE 子句中规定多个值. 本篇文章中,还是使用和上篇文章中同样的实体类和数据库,Dapper使用in操作符的代码如下: using System; using Syste ...