如何使用maven-war-plugin在项目之间共享类,配置文件和Web工件......?

时间:2012-08-07 17:55:17

标签: spring web-applications maven spring-security maven-war-plugin

有人可以帮我配置maven-war-plugin吗?

出于安全考虑,我想在两台服务器上为两个不同的用户组运行两个非常相似的Web应用程序,而不是仅为所有用户组部署一个。 因此,我想在它们之间共享尽可能多的代码。

我打算创建一个战争,也可以单独运行以进行开发,并在两个单独的Web项目中重用其大部分项目。我想重用,例如spring配置文件(applicationContext-XX.xml),jspx,css,...和Java代码。但我还需要编写一些扩展共享类的Java类。

我想尝试使用spring roo生成的测试项目来测试maven-war-plugin。我已经尝试了几件事,但我仍然遇到部署问题。

如果我只使用war,那么我就无法扩展任何Java类,因为代码无法编译:

<dependency>
    <groupId>com.test.pizzashop</groupId>
    <artifactId>PizzaShopCommons_WEB</artifactId>
    <scope>compile</scope>
    <version>0.1.0.BUILD-SNAPSHOT</version>
    <type>war</type>
</dependency>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <overlays>
            <overlay>
                <groupId>com.test.pizzashop</groupId>
                <artifactId>PizzaShopCommons</artifactId>
            </overlay>
        </overlays>
    </configuration>
</plugin>

即使我没有扩展任何Java类,我的Tomcat(mvn tomcat:run)也没有启动,因为它没有找到正确位于lib文件夹中的jar。
我还尝试使用第二个共享项目,该项目仅包含Java类和Spring配置文件以及所有依赖项,然后打包为jar。在Tomcat中运行主Web应用程序仍会导致异常:
org.springframework.beans.factory.BeanDefinitionStoreException:从ServletContext资源解析XML文档的IOException [/WEB-INF/spring/webmvc-config.xml];嵌套异常是FileNotFoundException:无法打开ServletContext资源[/WEB-INF/spring/webmvc-config.xml] 这个文件webmvc-config.xml位于它应该在战争中的位置(由maven war插件复制)。

<dependency>
    <groupId>com.test.pizzashop</groupId>
    <artifactId>PizzaShopCommons_WEB</artifactId>
    <scope>compile</scope>
    <version>0.1.0.BUILD-SNAPSHOT</version>
    <type>war</type>
</dependency>
<dependency>
    <groupId>com.test.pizzashop</groupId>
    <artifactId>PizzaShopCommons_SpringConf_Classes_Dependencies</artifactId>
    <scope>compile</scope>
    <version>0.0.1-SNAPSHOT</version>
    <type>jar</type>
</dependency>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <overlays>
            <overlay>
                <groupId>com.test.pizzashop</groupId>
                <artifactId>PizzaShopCommons_WEB</artifactId>
                <excludes>
                    <exclude>*.java</exclude>
                </excludes>
                <excludes>
                    <exclude>*.jar</exclude>
                </excludes>
            </overlay>
        </overlays>
    </configuration>
    <!-- <executions> <execution> <phase>generate-resources</phase> </execution> </executions> -->
</plugin>

生成的战争中的文件结构如下所示:

+---pizzashop-1.0.0-BUILD-SNAPSHOT
|   +---images
|   |       add.png ...       
|   +---META-INF
|   |   |   MANIFEST.MF   
|   |   \---maven
|   |       \---com.test.pizzashop
|   |           \---PizzaShop
|   |                   pom.properties
|   |                   pom.xml                 
|   +---styles
|   |       standard.css ...   
|   \---WEB-INF
|       |   web.xml
|       +---classes
|       |   |   builddef.lst ...  
|       |   +---com
|       |   |   \---test
|       |   |       \---pizzashop   
|       |   |           +---domain
|       |   |           |       Pizza.class ...
|       |   |           +---service
|       |   |           |       PizzaService.class ...      
|       |   |           \---web
|       |   |                   PizzaController.class ...                  
|       |   \---META-INF
|       |       |   aop-ajc.xml
|       |       |   persistence.xml  
|       |       \---spring
|       |               applicationContext-security.xml
|       |               applicationContext.xml
|       |               database.properties             
|       +---i18n
|       |       application.properties ...    
|       +---layouts
|       |       default.jspx ...  
|       +---lib
|       |       antlr-2.7.6.jar ...      
|       +---spring
|       |   |   root-context.xml
|       |   |   webmvc-config.xml 
|       |   \---appServlet
|       |           xx_servlet-context_xx.xml         
|       +---tags
|       |   |   create.tagx ...        
|       \---views
|           |   dataAccessFailure.jspx ...

我该如何运行?有没有可能只用一个共享项目来实现这个目标 - 一个罐子或战争?

1 个答案:

答案 0 :(得分:2)

我将Project Inheritance和Aggregation集成在一起,因为两者都有意义。

希望得到一些反馈我将描述我如何尝试两个解决2个问题(a)将公共代码分离为war和jar项目,b)由于弹簧配置文件而无法运行它: / p>

我尝试了一些更多的maven选项,但仍未找到令人满意的解决方案:

我在PizzaShopCommons_WEB的pom.xml中的 maven-war-plugin 中添加了一个带有 attachClasses 的配置元素,以直接创建jar和来自公共区域的war叠加层代码:

 <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-war-plugin</artifactId>
         <version>2.1.1</version>
         <configuration>  
                <attachClasses>true</attachClasses>
         </configuration>   
 </plugin>

弹簧资源的问题现在似乎是它们被加载了两次(一次来自jar,一次来自战争)。这尤其不适用于弹簧安全。

这基本上是我的下一个问题。

我已经尝试过但不起作用的是:
1。在上述配置中添加包装会排除有问题的弹簧配置文件。但这导致战争是在没有applicationContext文件的情况下构建的,而是包含它们的jar。现在commons项目不能再单独部署了,所以我没有尝试运行它:

 <packagingExcludes>**/applicationContext*</packagingExcludes> 

2。自定义maven-jar-plugin只包含在构建jar时有效的类文件,但如果使用如上所示的附加jar构建war则不会。

 <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-jar-plugin</artifactId>
         <version>2.3.1</version>
         <configuration>
           <includes>
            <include>com/**</include>
           </includes>
         </configuration>
  </plugin>

3. 从最终战争中排除生成的jar并使用叠加层中的类文件。但我还是

我得到的例外情况: 无论出于何种原因,无论我改变什么运行

  

mvn tomcat:运行

始终会导致相同的异常:

  

DispatcherServlet - 上下文初始化失败   org.springframework.beans.factory.BeanDefinitionStoreException:   IOException从ServletContext资源解析XML文档   [/WEB-INF/spring/webmvc-config.xml];嵌套异常是   java.io.FileNotFoundException:无法打开ServletContext资源   [/WEB-INF/spring/webmvc-config.xml]

所以我尝试直接在Tomcat 7上部署它,结果是

  

使用名称创建bean时出错   'org.springframework.security.filterChainProxy':   调用init方法失败;嵌套异常是   java.lang.IllegalArgumentException:通用匹配模式('/ **')   在过滤器链中的其他模式之前定义,导致它们   被忽略了。请检查您的订购   namespace或FilterChainProxy bean配置

使用eclipse WTP来部署代码根本不起作用,因为某种方式覆盖的工件被忽略了。这种情况下的错误消息是

  

org.springframework.beans.factory.NoSuchBeanDefinitionException:没有   名为'springSecurityFilterChain'的bean已定义   我手动复制必要的代码,我得到了同样的异常,就像我直接在Tomcat 7上部署它一样。

Commons项目可以部署并正常运行。 任何对Maven和Spring有更多经验的人的想法都绝对值得赞赏!