1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>book</groupId>
    <artifactId>book</artifactId>
    <packaging>war</packaging>
    <version>1.0</version>
    <name>book</name>
 
    <repositories>
        <repository>
            <id>eap</id>
            <url>http://maven.repository.redhat.com/techpreview/all</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
 
    <pluginRepositories>
        <pluginRepository>
            <id>eap</id>
            <url>http://maven.repository.redhat.com/techpreview/all</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.compiler.target>1.6</maven.compiler.target>
 
        <package.environment>local</package.environment>
    </properties>
 
    <dependencies>
 
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.2-1003-jdbc4</version>
        </dependency>
 
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.25</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.2.0.RELEASE</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.2.0.RELEASE</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.2.0.RELEASE</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.2.0.RELEASE</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.2.0.RELEASE</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>3.2.0.RELEASE</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>3.2.0.RELEASE</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>3.2.0.RELEASE</version>
        </dependency>
 
 
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
 
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
 
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
 
    </dependencies>
 
 
    <profiles>
        <profile>
            <id>local</id>
            <properties>
                <package.environment>local</package.environment>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>product</id>
            <properties>
                <package.environment>product</package.environment>
            </properties>
        </profile>
    </profiles>
 
    <build>
        <finalName>book</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <excludes>
                    <exclude>local/*</exclude>
                    <exclude>product/*</exclude>
                    <exclude>public/*</exclude>
                </excludes>
            </resource>
        </resources>
 
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <archive>
                        <addMavenDescriptor>false</addMavenDescriptor>
                    </archive>
                    <warName>book</warName>
                    <webResources>
                        <resource>
                            <directory>src/main/resources/${package.environment}</directory>
                            <targetPath>WEB-INF/classes</targetPath>
                            <filtering>true</filtering>
                        </resource>
                        <resource>
                            <directory>src/main/resources/public</directory>
                            <targetPath>WEB-INF/classes</targetPath>
                            <filtering>true</filtering>
                        </resource>
                        <!--<resource>-->
                            <!--<directory>${project.build.directory}/classes</directory>-->
                            <!--<includes>-->
                                <!--<include>**/*.properties</include>-->
                                <!--<include>**/*.xml</include>-->
                            <!--</includes>-->
                        <!--</resource>-->
                    </webResources>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
</project>

其中, 我们注意的地方: 我解释一下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<profiles>
        <profile>
            <id>local</id>
            <properties>
                <package.environment>local</package.environment>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>product</id>
            <properties>
                <package.environment>product</package.environment>
            </properties>
        </profile>
    </profiles>

这里就是不同的resources文件夹, 我这里只区分本地 产品; 设置为true的就是默认被激活的. 所以后面打包默认就是本地;

1
2
3
4
5
6
7
8
9
10
11
<resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <excludes>
                    <exclude>local/*</exclude>
                    <exclude>product/*</exclude>
                    <exclude>public/*</exclude>
                </excludes>
            </resource>
        </resources>

这里就是我的资源文件, public里面存放我的公用的, 比如Spring的配置文件, 就是本地与产品都一样的;local 与 product一看就知道了;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <archive>
                        <addMavenDescriptor>false</addMavenDescriptor>
                    </archive>
                    <warName>book</warName>
                    <webResources>
                        <resource>
                            <directory>src/main/resources/${package.environment}</directory>
                            <targetPath>WEB-INF/classes</targetPath>
                            <filtering>true</filtering>
                        </resource>
                        <resource>
                            <directory>src/main/resources/public</directory>
                            <targetPath>WEB-INF/classes</targetPath>
                            <filtering>true</filtering>
                        </resource>
                        <!--<resource>-->
                            <!--<directory>${project.build.directory}/classes</directory>-->
                            <!--<includes>-->
                                <!--<include>**/*.properties</include>-->
                                <!--<include>**/*.xml</include>-->
                            <!--</includes>-->
                        <!--</resource>-->
                    </webResources>
                </configuration>
            </plugin>

这里就是war的插件了,

1
${package.environment}

就是动态指定文件夹了.

1
<filtering>true</filtering>

这里一定需要设置为true才行.

然后就差不多了. 最后执行 mvn clean ; mvn compile; mvn package; 这里是Maven的生命周期, 其他介绍的文章太多了, 我就不再具体讲. 如果说我要打product的war包;

mvn clean ; mvn compile; mvn -Pproduct package;

那就激活了product 的资源文件;

就这样子, 很简单吧~

最新文章

  1. WdatePicker.js开始日期和结束日期比较
  2. ArcMap常用VBA
  3. linux heartbeat v2/v3 的一点资料
  4. STM32F4_USART配置及细节描述
  5. IOS设计模式之二(门面模式,装饰器模式)
  6. 3 years in Tencent game
  7. 将多个.a库合并为一个.a库的方法
  8. Yar - Yet Another RPC framework for PHP
  9. 【BZOJ】1015: [JSOI2008]星球大战starwar
  10. asp.net关于Cookie跨域(域名)的问题
  11. 栈和队列的面试题Java实现
  12. C - Surprising Strings
  13. HDU 4081 MST
  14. JAVA_SE基础——8.基本数据类型
  15. MVC ModelState.AddModelError使用
  16. xml文档格式学习笔记
  17. mysql 按照月份自动创建表,以年和月为表明,动态生成。
  18. WPF 矩形框8个控制点伸缩及拖拽
  19. Jsp运行原理
  20. 【Java】 剑指offer(20) 表示数值的字符串

热门文章

  1. [LeetCode] Remove Element (三种解法)
  2. ereg/eregi报错处理办法
  3. linux 读写锁应用实例
  4. Linux文本比较-diff&amp;awk
  5. .NET中的六个重要概念:栈、堆、值类型、引用类型、装箱和拆箱
  6. Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树 矩阵面积并
  7. 偷师--先留着。。ssh,nginx,防火墙相关命令
  8. 【转】HTML网页中插入视频各种方法
  9. 简单记录在Visual Studio 2013中创建ASP.NET Web API 2
  10. RDLC的部署(无法找到Microsoft.ReportViewer.ProcessingObjectModel.dll文件)