1

有人可以帮我配置maven-war-plugin吗?如何使用maven-war-plugin在项目之间共享类,配置文件和Web工件...?

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

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

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

如果我只用战我就不能扩展任何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的:运行)做不能开始,因为它不能正确地找到位于lib文件夹中的jar。
我也尝试使用第二个共享项目,其中只包含Java类和Spring配置文件以及所有依赖项,然后打包为jar。在Tomcat中运行主Web应用程序仍导致出现异常:
org.springframework.beans.factory.BeanDefinitionStoreException:IOException从ServletContext资源解析XML文档[/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 ... 

我怎样才能得到这个运行?只有一个共享项目 - 一个罐子还是战争?是否有可能实现这一目标?

+0

2我看到这里的东西:1)做一个多模块maven项目,然后在你的项目之间使用普通的东西。 2)使用maven覆盖来继承和扩展你的spring配置文件http://maven.apache.org/plugins/maven-war-plugin/overlays.html – mfirry 2012-08-07 21:56:59

+0

那么,使用多模块项目是很好的,因为它使建设所有项目更容易,以及使用继承进行中央管理的依赖:[链接](http://maven.apache.org/guides/introduction/introduction-to-the-pom.html)不幸的是,这并没有解决我的任何问题。 – Ninca 2012-08-27 15:45:00

回答

2

我整合了Project Inheritance和Aggregation,因为两者都有意义。

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

我尝试了一些行家的选择,但仍然没有找到一个令人满意的解决方案:

我PizzaShopCommons_WEB的pom.xml中增加的配置元素与attachClassesMaven的战争插件到直接从公共代码创建一个jar和一个战争覆盖:

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

与弹簧资源的问题似乎是,他们现在被加载两次(一次从jar和一次战争)。这对于弹簧安全尤其不适用。

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

我已经试过,什么不工作是:
添加包装不包括在上面的配置有问题的Spring配置文件。但是这导致了战争的构建没有applicationContext文件,但包括它们的罐子。现在,公共项目不能被部署为独立的任何更多,所以我没有尝试运行此:

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

2.定制的maven-JAR-插件,只包括类文件,如果一个罐子其中工程是建立起来的,但如果战争是用上面所示的另外一个罐子来建造的话,那就不是。

<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> 

撇除最后的战争生成的JAR和使用类文件从覆盖。但我还是

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

MVN tomcat的:运行

总是导致相同的异常:

DispatcherServlet - 上下文初始化失败IOException解析来自ServletContext资源的XML文档 [/WEB-INF/spring/webmvc-config.xml];嵌套的例外是 java.io.FileNotFoundException:无法打开ServletContext的资源 [/WEB-INF/spring/webmvc-config.xml]

所以,我试图将它部署到直接在Tomcat 7导致

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

使用Eclipse WTP部署代码根本不会因为莫名其妙的重叠文物被那种忽略工作。在这种情况下,该错误消息

org.springframework.beans.factory.NoSuchBeanDefinitionException:没有 豆名为“springSecurityFilterChain”被定义 II手动复制必要的代码在那里,我得到了同样的异常就像当我直接在Tomcat 7上部署它。

Commons项目的所有时间都可以部署并正常工作。 来自Maven和Spring有更多经验的人的想法绝对值得赞赏!

相关问题