Maven war 插件,未从父 pom 获取依赖项

Ana*_*hah 5 java spring maven tomcat7 sts-springsourcetoolsuite

我的目标是将一场战争纳入另一场战争。查看我的项目结构:

+test-parent (这里是 spring 和 hibernate 框架的所有常见依赖项) -- test-core (所有常见类类型的集合:jar) -- test-web (应该作为单独的 web 应用程序运行的 web 应用程序,依赖于 test -core, type:war) -- test-web-2 (依赖于 test-web,test-core, type:war 的 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>
Run Code Online (Sandbox Code Playgroud)

测试核心/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>
Run Code Online (Sandbox Code Playgroud)

测试-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>
Run Code Online (Sandbox Code Playgroud)

测试-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>
Run Code Online (Sandbox Code Playgroud)

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

我在两个项目中给出了不同的包和 jsp 文件名。我什至能够根据需要看到正确的合并构建。

如何在 test-web 和 test-web-2 项目中生成此 lib 文件夹以及来自父级的所有依赖 jar?

Mar*_*szS 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)
Run Code Online (Sandbox Code Playgroud)

test-web.war有依赖关系:

  • 测试-web-core.jar
  • 测试核心.jar

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

  • 测试-web-2-core.jar
  • 测试核心.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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

源代码