Maven war插件,没有从父pom获得依赖

时间:2013-11-03 11:30:30

标签: java spring maven tomcat7 sts-springsourcetoolsuite

我的目标是将战争纳入另一场战争。 请参阅我的项目结构:

+ test-parent(这里是spring和amp; hibernate框架的所有常见依赖项) - test-core(所有常见类的集合类型:jar) - test-web(应该作为单独的webapp运行的webapp,取决于test-core,类型:war) - test-web-2(webapp取决于测试网,测试核心,类型:战争)

我使用了maven war插件来实现这一目标。

测试父/ pom.xml的

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>test-parent</groupId>
    <artifactId>test-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>test-core</module>
        <module>test-web</module>
        <module>test-web-2</module>
    </modules>
    <dependencies>
        <!-- All common spring & hibernate dependencies -->
    </dependencies>
</project>

测试核/ pom.xml的

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>test-parent</groupId>
    <artifactId>test-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>test-core</artifactId>
</project>

测试的web / pom.xml中

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>test-parent</groupId>
        <artifactId>test-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>test-parent</groupId>
    <artifactId>test-web</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>test-web Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>test-parent</groupId>
            <artifactId>test-core</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>test-web</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <packagingIncludes>**/*.xml,**/*.properties,**/*.class,**/*.png,**/*.css,**/*.js,**/*.jsp</packagingIncludes>
                    <packagingExcludes>WEB-INF/web.xml</packagingExcludes>
                    <attachClasses>true</attachClasses>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

测试的web-2 / pom.xml的

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>test-parent</groupId>
        <artifactId>test-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>test-parent</groupId>
    <artifactId>test-web-2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>test-web-2 Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>test-parent</groupId>
            <artifactId>test-web</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <type>war</type>
        </dependency>
        <dependency>
            <groupId>test-parent</groupId>
            <artifactId>test-web</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <classifier>classes</classifier>
        </dependency>
    </dependencies>
    <build>
        <finalName>test-web-2</finalName>
    </build>
</project>

由于我的test-web-2依赖于test-web,我想合并test-web-2中的所有类和资源。当我运行mvn install时,我的所有构建都成功了,但是当我在服务器上运行test-web或test-web-2时,它在WEB-INF下没有lib文件夹,所有必需的spring jar都来自于父。

我给了包&amp;两个项目中的jsp文件名都不同。我甚至能够根据需要看到正确的合并构建。

如何在test-web和amp;中生成此lib文件夹? test-web-2项目包含来自父母的所有依赖罐子?

2 个答案:

答案 0 :(得分:3)

简化项目配置。将java源代码从WAR分隔为JAR。在此之后,您只需将此JAR添加到另一个WAR作为依赖项。在这种情况下,合并 java代码并不是一个好主意。

用于合并来自WAR的资源使用maven overlays

样品溶液

* test-parent.pom
  * test-core (test-core.jar)         - shared code
  * test-web-parent (test-web-parent.pom)   - first project
    * core (test-web-core.jar)
    * web (test-web.war)
  * test-web-2-parent (test-web-2-parent.pom) - second project
    * core (test-web-2-core.jar)
    * web (test-web-2.war)

test-web.war有依赖关系:

  • 测试的web-core.jar添加
  • 测试core.jar添加

test-web-2.war有依赖关系:

  • 测试的web -2- core.jar添加
  • 测试core.jar添加

配置

test-parent.pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>test-parent</groupId>
    <artifactId>test-parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <modules>
        <module>test-core</module>
        <module>test-web-parent</module>
        <module>test-web-2-parent</module>
    </modules>

</project>

test-core.jar

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>test-parent</groupId>
        <artifactId>test-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>test-core</artifactId>

</project>

test-web-parent.pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>test-parent</groupId>
        <artifactId>test-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>test-web-parent</artifactId>
    <packaging>pom</packaging>
    <modules>
        <module>core</module>
        <module>web</module>
    </modules>

</project>

test-web-core.jar

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>test-parent</groupId>
        <artifactId>test-web-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>test-web-core</artifactId>

    <dependencies>
        <dependency>
            <groupId>test-parent</groupId>
            <artifactId>test-core</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

test-web.war

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>test-parent</groupId>
        <artifactId>test-web-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>test-web</artifactId>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>test-parent</groupId>
            <artifactId>test-web-core</artifactId>
            <version>1.0-SNAPSHOT</version>
            <type>jar</type>
        </dependency>
    </dependencies>

</project>

test-web-2-parent.pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>test-parent</groupId>
        <artifactId>test-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>test-web-2-parent</artifactId>
    <packaging>pom</packaging>

    <modules>
        <module>core</module>
        <module>web</module>
    </modules>

</project>

test-web-2-core.jar

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>test-parent</groupId>
        <artifactId>test-web-2-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>test-web-2-core</artifactId>

    <dependencies>
        <dependency>
            <groupId>test-parent</groupId>
            <artifactId>test-core</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

test-web-2.war

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>test-parent</groupId>
        <artifactId>test-web-2-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>test-web-2</artifactId>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>test-parent</groupId>
            <artifactId>test-web-2-core</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--first war-->
        <dependency>
            <groupId>test-parent</groupId>
            <artifactId>test-web</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>runtime</scope>
            <type>war</type>
        </dependency>
        <dependency>
            <groupId>test-parent</groupId>
            <artifactId>test-web-core</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

源代码

答案 1 :(得分:2)

我认为问题在于你正在尝试做一些不符合J2EE标准的事情。将war文件包含在另一个war文件中是没有意义的。

处理此类依赖关系的“标​​准”方法是将您的部署打包为.ear文件,将共享资源打包为耳内的jar文件,以及使用这些共享依赖项的war文件也在ear文件中。

看看maven包装插件的“耳朵”包装选项。