4

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

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

我已经使用 Maven 战争插件来实现这一点。

测试父/ 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>

测试网络/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>

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

我在两个项目中都给出了不同的包和 jsp 文件名。我什至可以根据需要看到正确的合并版本。

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

4

2 回答 2

3

简化项目配置。将 java 源代码从WARJAR. 在此之后,您可以简单地将其添加JAR到另一个WAR作为依赖项。在这种情况下合并java 代码不是一个好主意。

用于合并来自WAR使用 maven覆盖的资源。

样品溶液

* 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
  • 测试核心.jar

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

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

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>

源代码

于 2013-11-23T17:21:20.453 回答
2

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

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

查看 maven 打包插件的“ear”打包选项。

于 2013-11-23T17:13:15.217 回答