gpt4 book ai didi

java - Maven war 插件,没有从父 pom 获取依赖项

转载 作者:搜寻专家 更新时间:2023-11-01 02:27:02 26 4
gpt4 key购买 nike

我的目标是将一场 war 包含到另一场 war 中。查看我的项目结构:

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

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

test-parent/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>

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

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 下没有包含父级所需的所有 spring jar 的 lib 文件夹.

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

我怎样才能在 test-web 和 test-web-2 项目中生成这个 lib 文件夹以及来自父级的所有依赖 jar?

最佳答案

简化项目配置。将 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 有依赖:

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

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

源代码

关于java - Maven war 插件,没有从父 pom 获取依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19752449/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com