上一篇介绍了JavaEE Web应用的js css images html等如何模块化, 里面讲到使用overlays来依赖合并其它war包,但是使用中发现一个问题,如果你使用tomcat7-maven插件,使用tomcat7:run来运行的时候,依赖的那些war里面的文件并不能被访问到,换成tomcat7:run-war就可以,但是用run-war的话,就不能热部署了,在eclipse里面改一个jsp不能马上看到效果,要重新启动tomcat才行。
网上找了很多资料,发现都不行,最后想着换jetty-maven插件试试,结果一试果然jetty可以解决问题,详细配置请看
http://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html
最主要是下面这段,也就是说不需要特别的配置, jetty是支持多个war的,像我下面这样配置就可以了
<plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.3.7.v20160115</version> <configuration> <webApp> <contextPath>/test</contextPath> </webApp> <httpConnector> <port>8081</port> </httpConnector> </configuration> </plugin>
If your webapp depends on other war files, thejetty:run and jetty:run-forked goals are able to merge resources from all of them. It can do so based on the settings of the maven-war-plugin, or if your project does not use the maven-war-plugin to handle the overlays, it can fall back to a simple algorithm to determine the ordering of resources.
The maven-war-plugin has a rich set of capabilities for merging resources. The jetty:run and jetty:run-forked goals are able to interpret most of them and apply them during execution of your unassembled webapp. This is probably best seen by looking at a concrete example.
Suppose your webapp depends on the following wars:
1
2
3
4
5
6
7
8
9
10
11
|
<
dependency
>
<
groupId
>com.acme</
groupId
>
<
artifactId
>X</
artifactId
>
<
type
>war</
type
>
</
dependency
>
<
dependency
>
<
groupId
>com.acme</
groupId
>
<
artifactId
>Y</
artifactId
>
<
type
>war</
type
>
</
dependency
>
|
Containing:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
WebAppX:
/foo.jsp
/bar.jsp
/WEB-INF/web.xml
WebAppY:
/bar.jsp
/baz.jsp
/WEB-INF/web.xml
/WEB-INF/special.xml
|
They are configured for the maven-war-plugin:
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
|
<
plugin
>
<
groupId
>org.apache.maven.plugins</
groupId
>
<
artifactId
>maven-war-plugin</
artifactId
>
<
version
>9.3.7.v20160115</
version
>
<
configuration
>
<
overlays
>
<
overlay
>
<
groupId
>com.acme</
groupId
>
<
artifactId
>X</
artifactId
>
<
excludes
>
<
exclude
>bar.jsp</
exclude
>
</
excludes
>
</
overlay
>
<
overlay
>
<
groupId
>com.acme</
groupId
>
<
artifactId
>Y</
artifactId
>
<
excludes
>
<
exclude
>baz.jsp</
exclude
>
</
excludes
>
</
overlay
>
<
overlay
>
</
overlay
>
</
overlays
>
</
configuration
>
</
plugin
>
|
Then executing jetty:run would yield the following ordering of resources: com.acme.X.war : com.acme.Y.war: ${project.basedir}/src/main/webapp
. Note that the current project's resources are placed last in the ordering due to the empty <overlay/> element in the maven-war-plugin. You can either use that, or specify the <baseAppFirst>false</baseAppFirst>
parameter to the jetty-maven-plugin.
Moreover, due to the exclusions
specified above, a request for the resource bar.jsp
would only be satisfied from com.acme.Y.war.
Similarly as baz.jsp
is excluded, a request for it would result in a 404 error.
The algorithm is fairly simple, is based on the ordering of declaration of the dependent wars, and does not support exclusions. The configuration parameter <baseAppFirst>
(see the section on Configuring Your Webapp for more information) can be used to control whether your webapp's resources are placed first or last on the resource path at runtime.
For example, suppose our webapp depends on these two wars:
1
2
3
4
5
6
7
8
9
10
|
<
dependency
>
<
groupId
>com.acme</
groupId
>
<
artifactId
>X</
artifactId
>
<
type
>war</
type
>
</
dependency
>
<
dependency
>
<
groupId
>com.acme</
groupId
>
<
artifactId
>Y</
artifactId
>
<
type
>war</
type
>
</
dependency
>
|
Suppose the webapps contain:
1
2
3
4
5
6
7
8
9
10
11
12
|
WebAppX:
/foo.jsp
/bar.jsp
/WEB-INF/web.xml
WebAppY:
/bar.jsp
/baz.jsp
/WEB-INF/web.xml
/WEB-INF/special.xml
|
Then our webapp has available these additional resources:
1
2
3
4
5
|
/foo.jsp (X)
/bar.jsp (X)
/baz.jsp (Y)
/WEB-INF/web.xml (X)
/WEB-INF/special.xml (Y)
|