Skip to main content

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



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

在纯Maven中的实现方案

纯MAVEN环境比较简单,经过一段曲折(先是修改maven-war-plguin源码,再是自定义一个插件),最后发现居然有一个现成的插件可以实现这个功能,示范如下:

   com.isoftstone.gads
   common-web
   0.0.1-SNAPSHOT
   war


   com.isoftstone.gads
   common-web
   0.0.1-SNAPSHOT
   warpath
 
    org.apache.maven.plugins
    maven-war-plugin
    2.1-beta-1
    
     <!- 必须指定,否则默认会变成在target/war/work 导致被打包进war文件,指定后为target/work ->
     ${project.build.directory}/work
     WebContent
     false
     
      true
     

     
      
       com.isoftstone.gads
       ebiz-common-web
      

      
       
       
       
      

     

     */web.xml,WEB-INF/lib/*,/sql-map-config.xml,/jdbc.properties,/META-INF/*
    

   

    org.appfuse.plugins
    maven-warpath-plugin
    2.1.0-M1
    true
    
     
      
       add-classes
      

     

    

    
     **/logback-test.xml
    
注意红色部分,说明如下:
  1. 首先是使用了maven-warpath-plugin插件,处理所有warpath的artifact,这个插件可以将从依赖的WAR中传递的依赖都打包到当前的WAR中,没有这个插件时,当前WAR从所依赖的WAR artifact那所传递来的依赖在打包成WAR时都会被忽略.既然现在能将传递的依赖打包了,就不用copy依赖的war中的WEB-INF/lib/*,所以被加入到
  2. 的设置看我写的注释
  3. 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功能进行快速的部署调式,是个需要解决的问题.
  1. 在eclipse的配置文件".settings/.jsdtscope"修改src/main/webapp"/>为WebContent"/>
  2. 在".settings/org.eclipse.wst.common.component"中把/" source-path="/src/main/webapp"/>修改WEB-INF/classes" source-path="/src/main/webapp"/>,并增加
  3. OK,经过上述配置后,eclipse就把/WebContent目录认为web资源根路径了.这样在eclipse的Servers View中,把这个war项目增加到TOMCAT上时,实际部署的就是/WebContent ,可以直接在eclipse部署运行这个war项目了
  4. 但是....实现了可以在eclipse上直接部署的功能后,我发现丧失了eclipse的文件变化监视功能.比如,原本在eclipse中修改一个jsp后,就可以自动同步到TOMCAT上,但现在我实际的web资源路径是src/main/webapp, 而eclipse识别的web资源路径却是WebConten.我的文件修改都是在src/main/webapp,不执行mvn package是不会同步到WebContent中,eclipse也就不会把我修改的JSP同步到TOMCAT上.为了解决这个问题,我自定义了一个插件,示例如下:

    org.apache.maven.plugins
    maven-websources-plugin
    0.0.1-SNAPSHOT
    
     
      
       webSources
      

     
    
   这个插件绑定了@phase process-resources,所以在src/main/webapp下的文件有变化时,会自动被调用,将src/main/webapp下有变化的文件拷贝到WebContent目录下.这时就会被eclipse发现这个变化,从而同步到TOMCAT上.

Comments

Popular posts from this blog

Regression Testing

From  http://www.softwaretestinghelp.com/regression-testing-tools-and-methods/ The selective retesting of a  software  system that has been modified to ensure that any  bugs  have been fixed and that no other previously working functions have failed as a result of the reparations and that newly added features have not created problems with previous versions of the  software . Also referred to as  verification testing , regression testing is initiated after a  programmer  has attempted to fix a recognized problem or has added  source code  to a program that may have inadvertently introduced errors. It is a  quality  control measure to ensure that the newly modified code still complies with its specified requirements and that unmodified code has not been affected by the maintenance activity. What is Regression Software Testing? Regression means retesting the unchanged parts of the application. Test cases are re-execu...

Spring MVC 3.2 Preview: Introducing Servlet 3, Async Support

Continuing the Spring 3.0 "simplification series" started by Keith and Chris , I would like to provide a quick overview of simplifications in scheduling and task execution enabled by Spring 3.0. I will be walking through a basic sample application that you can checkout from the spring-samples Subversion repository. It has been designed to be as simple as possible while showcasing both annotation-driven and XML-based approaches to scheduling tasks in Spring 3.0. Let's begin with the annotation-driven approach. You can run it directly via the main() method in AnnotationDemo. If you take a look, you'll see that it's nothing more than a bootstrap for a Spring ApplicationContext: public static void main(String[] args) {      new ClassPathXmlApplicationContext( "config.xml" , AnnotationDemo. class ); } The reason nothing else is necessary is that the ApplicationContext contains an "active" component, which we will...

How to install JSVC on Linux WidenHome Log | WidenHome Log

How to install JSVC on Linux WidenHome Log | WidenHome Log In our team, we have a lot of Java standalone applications which should be run as daemon on Unix/Linux system, and we found JSVC is the best choice for us to wrap Java programs to daemons. This article records the steps on how to install JSVC executable on Linux, which is our stage/prod environment. Download JSVC source package First of all, we need to download JSVC source package from this URL: http://commons.apache.org/daemon/download_daemon.cgi , for example, I downloaded commons-daemon-1.0.5-src.tar.gz file. Or, download it via wget: wget -c http://apache.etoak.com/commons/daemon/source/commons-daemon-1.0.5-src.tar.gz Build JSVC executable Unzip the source package and build JSVC executable. chmod 755 commons-daemon-1.0.5-src.tar.gz tar zxvf commons-daemon-1.0.5-src.tar.gz cd commons-daemon-1.0.5-src/src/native/unix Before building the JSVC executable, please make sure you have set JAVA_HOME variable correctly. And make sur...