`
ohunbeyond
  • 浏览: 17026 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Maven中如何配置WAR依赖WAR和JAR的多模块项目结构

 
阅读更多
要使用的Warpath插件包括以下插件配置元素在你的pom.xml
[...]
<build>
  <plugins>
    <plugin>
      <groupId>org.appfuse</groupId>
      <artifactId>maven-warpath-plugin</artifactId>
      <version>1.0-SNAPSHOT</version>
      <extensions>true</extensions>
      <executions>
        <execution>
          <goals>
            <goal>add-classes</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>
[...]

你要在classpath中包含的依赖关系,并添加warpath类型的war
[...]
<dependencies>
  <dependency>
    <groupId>org.appfuse</groupId>
    <artifactId>appfuse-web</artifactId>
    <version>2.0</version>
    <type>war</type>
  </dependency>
  <dependency>
    <groupId>org.appfuse</groupId>
    <artifactId>appfuse-web</artifactId>
    <version>2.0</version>
    <type>warpath</type>
  </dependency>
</dependencies>
[...]

注:war和warpath依赖类型是必需的:war类型是由使用Maven的war-plugin做战争war,warpath类型的Warpath插件,以确定项目的类路径正确的文件列入名单。

以下是还建议,以避免相同的JAR依赖的几个版本,在最后的war,建立覆盖过程中,由于工件(所有的传递依赖)
[...]
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-war-plugin</artifactId>
      <configuration>
        <dependentWarExcludes>WEB-INF/lib/*</dependentWarExcludes>
      </configuration>
    </plugin>
  </plugins>
</build>
[...]



先考虑如下实际情况:
war项目C和war项目B都依赖war项目A和JAR项目X. 项目A中保存了B和C项目通用的web资源,比如通用的javascript,CSS,jsp等. 项目X中保存了B和C项目中都依赖的一些class
开发人员希望每次都只面对一个项目,即Team A 开发项目A, Team B开发项目B, Team C开发项目C....以此类推
每个Team在开发自己项目时,都希望能直接进行调试,例如war项目A可以直接部署到TOMCAT上运行测试
最后实际交付给客户的却只有2个项目: B和C .也就是说,最后要打包B和C,而在B和C的war包中都要包含A中的web资源和X中的class
在纯Maven中的实现方案

纯MAVEN环境比较简单,经过一段曲折(先是修改maven-war-plguin源码,再是自定义一个插件),最后发现居然有一个现成的插件可以实现这个功能,示范如下:
<dependency>
   <groupId>com.isoftstone.gads</groupId>
   <artifactId>common-web</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <type>war</type>
</dependency>
<dependency>
   <groupId>com.isoftstone.gads</groupId>
   <artifactId>common-web</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <type>warpath</type>
</dependency>
 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.1-beta-1</version>
    <configuration>
     <!- 必须指定,否则默认会变成在target/war/work 导致被打包进war文件,指定后为target/work ->
     <workDirectory>${project.build.directory}/work</workDirectory>
     <webappDirectory>WebContent</webappDirectory>
     <useCache>false</useCache>
     <archive>
      <addMavenDescriptor>true</addMavenDescriptor>
     </archive>
     <overlays>
      <overlay>
       <groupId>com.isoftstone.gads</groupId>
       <artifactId>ebiz-common-web</artifactId>
      </overlay>
      <overlay>
       <!-- empty groupId/artifactId is detected as the current build -->
       <!-- 代表当前WAR项目,默认情况下当前WAR项目是先被拷贝的,如果要控制其顺序,则使用空的overlay -->
       <!-- any other overlay will be applied after the current build since they have not been configured in the overlays
        element -->
      </overlay>
     </overlays>
     <dependentWarExcludes>*/web.xml,WEB-INF/lib/*,/sql-map-config.xml,/jdbc.properties,/META-INF/*</dependentWarExcludes>
    </configuration>
   </plugin>
<plugin>
    <groupId>org.appfuse.plugins</groupId>
    <artifactId>maven-warpath-plugin</artifactId>
    <version>2.1.0-M1</version>
    <extensions>true</extensions>
    <executions>
     <execution>
      <goals>
       <goal>add-classes</goal>
      </goals>
     </execution>
    </executions>
    <configuration><!-- below WEB-INF/classes -->
     <warpathExcludes>**/logback-test.xml</warpathExcludes>
    </configuration>
</plugin>

注意红色部分,说明如下:
首先是使用了maven-warpath-plugin插件,处理所有<type>warpath</type>的artifact,这个插件可以将从依赖的WAR中传递的依赖都打包到当前的WAR中,没有这个插件时,当前WAR从所依赖的WAR artifact那所传递来的依赖在打包成WAR时都会被忽略.既然现在能将传递的依赖打包了,就不用copy依赖的war中的WEB-INF/lib/*,所以被加入到<dependentWarExcludes>中
<workDirectory>的设置看我写的注释
webappDirectory的指定需要额外注意.首先,我使用了MAVEN默认的资源路径,也就是 src/main/webapp,而这里却告诉maven-war-plugin另一个路径"WebContent",产生的结果就是,执行mvn package时,war-plugin和warpath-plugin会将当前war和所有依赖的war的web资源都拷贝到WebContent目录下.这样,WebContent目录包含的内容就是最终打包成WAR的内容了.
在eclipse集成了Maven插件时的实现方案

纯MAVEN确实很happy,但是我们开发项目可是要在eclipse中进行的,安装了M2E插件后 ,如何利用eclipse的WTP提供的SERVER功能进行快速的部署调式,是个需要解决的问题.
在eclipse的配置文件".settings/.jsdtscope"修改<classpathentry kind="src" path="src/main/webapp"/>为<classpathentry kind="src" path="WebContent"/>
在".settings/org.eclipse.wst.common.component"中把<wb-resource deploy-path="/" source-path="/src/main/webapp"/>修改<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/webapp"/>,并增加<wb-resource deploy-path="/" source-path="/WebContent"/>
OK,经过上述配置后,eclipse就把/WebContent目录认为web资源根路径了.这样在eclipse的Servers View中,把这个war项目增加到TOMCAT上时,实际部署的就是/WebContent ,可以直接在eclipse部署运行这个war项目了
但是....实现了可以在eclipse上直接部署的功能后,我发现丧失了eclipse的文件变化监视功能.比如,原本在eclipse中修改一个jsp后,就可以自动同步到TOMCAT上,但现在我实际的web资源路径是src/main/webapp, 而eclipse识别的web资源路径却是WebConten.我的文件修改都是在src/main/webapp,不执行mvn package是不会同步到WebContent中,eclipse也就不会把我修改的JSP同步到TOMCAT上.为了解决这个问题,我自定义了一个插件,示例如下:
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-websources-plugin</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <executions>
     <execution>
      <goals>
       <goal>webSources</goal>
      </goals>
     </execution>
    </executions>
</plugin>

   这个插件绑定了@phase process-resources,所以在src/main/webapp下的文件有变化时,会自动被调用,将src/main/webapp下有变化的文件拷贝到WebContent目录下.这时就会被eclipse发现这个变化,从而同步到TOMCAT上.
原文http://www.cnblogs.com/discuss/articles/1866924.html
分享到:
评论
1 楼 emilyzhanghong 2012-03-06  

你好,我现在有一个 webA, webCommon,jarCommon的三个模块的项目, 我需要把webCommon的web资源给webA,把jarCommon的class资源给webA, 现在能达到当只打包webA时可以也依赖到webCommon的web了,,但class资源怎么依赖给webA呢???
(最后打包webA,webA的war包中已经包含webCommon中的web资源了,,,但怎么包含jarCommon中的class资源了???

相关推荐

Global site tag (gtag.js) - Google Analytics