gpt4 book ai didi

java - Maven 多模块 : error assembling WAR: webxml attribute is required

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:23:22 26 4
gpt4 key购买 nike

我正在将单个 Eclipse Maven 管理的 webapp 项目转换为多模块 Maven 项目(这是一个测试 Maven 的测试项目,请随时提供任何类型的建议)。

单个项目 webapp 没有任何错误,编译成功并且在部署时行为正确,所以我从一个工作应用程序开始。

该应用程序有一个 Web 部分和一个控制台部分,这意味着有一些类具有 main() 方法,当从 Eclipse 中运行时(使用 Run as -> Java Application)作为预期的。两个部分都显示来自数据库的数据,直接通过 JDBC 或通过 jOOQ 查询。 .

所以,这就是我拆分项目的方式:

  • core(包含其他两个部分共有的所有内容);
  • runnable(包含具有 main() 方法的类);
  • webapp(网络应用程序部分)。

在 Eclipse 中,我现在有 4 个独立的项目:

  • shaker-multi 包含聚合器(和父级)POM,以及子目录中的每个模块;
  • 振动器多核;
  • shaker-multi-runnable;
  • shaker-multi-webapp

在 Eclipse 中,corewebapp 编译,后者可以部署到 Tomcat 实例,我可以在浏览器中看到它。

问题出现在 runnable 上。该项目依赖于 jOOQ 类,因此必须生成相关的源代码。 jOOQ 依赖项和配置位于 core/pom.xml 中(因为它们也可能在那里使用)。

当我在 shaker-multi-core 上执行 Project -> Run As -> Maven build... -> clean generate-sources 时,我得到:

Non-resolvable parent POM: Failure to find sunshine.web:shaker-multi:pom:0.0.1

which sounds reasonable, since I didn't install any of those artifacts, even in my local repository.

But when I call Maven build... -> 'clean install' on shaker-multi, it breaks because it can't find the web.xml file for shaker-multi-webapp (although it correctly resides in shaker-multi-webapp/src/main/webapp/WEB-INF/web.xml).

What should I do?Is my project configuration / splitting totally wrong?Should I add another module with the parent POM? This sounds wrong, since the POM Reference states:

Inheritance and aggregation create a nice dynamic to control builds through a single, high-level POM. You will often see projects that are both parents and aggregators.

I'm totally lost here.

My expectations:

  • run Maven package on shaker-multi-webapp and obtain a deployable war;
  • run Maven package on shaker-multi-runnable and obtain a command line runnable jar (I still need to configure its POM to generate a jar-with-dependencies, though, I know);
  • run Maven package on shaker-multi and obtain some kind of bundle that I can move around and that will contain the war or the jar of each module.

EDIT

I added

        <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
</configuration>
</plugin>

shaker-multi-webapp POM,如 this answer 中所示, 但没有区别。


EDIT-2

我清除了我的整个本地存储库(按照建议 here ),当我重新打开 Eclipse 时,我在 Maven 控制台中看到了

[...]05/09/14 07:58:19 CEST: [INFO] Adding source folder /shaker-multi-webapp/src/main/java05/09/14 07:58:19 CEST: [INFO] Adding source folder /shaker-multi-webapp/src/test/java**05/09/14 07:58:19 CEST: [ERROR] Could not read web.xml**[...]

Any hint? From where does it come from? I can't reproduce it though (without removing again my whole local repo).


This is shaker-multi POM:

<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>sunshine.web</groupId>
<artifactId>shaker-multi</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>

<modules>
<module>shaker-multi-core</module>
<module>shaker-multi-runnable</module>
<module>shaker-multi-webapp</module>
</modules>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>

</project>

这是 shaker-multi-core POM:

<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>sunshine.web</groupId>
<artifactId>shaker-multi</artifactId>
<version>0.0.1</version>
</parent>

<artifactId>shaker-multi-core</artifactId>
<packaging>jar</packaging>

<build>
<plugins>
<plugin><!-- jOOQ plugin--></plugin>
</plugins>
</build>

<dependencies>
<dependency><!-- jOOQ dependency --></dependency>
</dependencies>

</project>

这是 shaker-multi-webapp POM:

<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>sunshine.web</groupId>
<artifactId>shaker-multi</artifactId>
<version>0.0.1</version>
</parent>

<artifactId>shaker-multi-webapp</artifactId>
<packaging>war</packaging>

<build>
<plugins>
<plugin><!-- Tomcat local -->
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>sunshine.web</groupId>
<artifactId>shaker-multi-core</artifactId>
<version>0.0.1</version>
</dependency>


<dependency>
<!-- JSP & Servlet dependencies -->
</dependency>

</dependencies>
</project>

最佳答案

我假设你的文件夹结构是这样的:

 +-- shaker-multi
+--- pom.xml
+--- shaker-multi-core
+-- pom.xml
+--- shaker-multi-runnable
+-- pom.xml
+--- shaker-multi-webapp
+-- pom.xml
  1. 此外,您应该检查您的项目是否在命令和在 Eclipse 中。所以你应该去你的项目的根目录(摇床多文件夹)和

    mvn 清理包

    这应该不会产生错误等。

  2. 我想到的一件事是为什么您使用发布版本而不是项目的 SNAPSHOT 版本。所以 0.0.1 而不是 0.0.1-SNAPSHOT

  3. 你应该改进的是maven-compiler插件的定义在你的 parent 中:

    <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>

我建议这样做:

   <build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
  1. 编码可以用更好的方式解决just define the following in your pom :
<project>
...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
...
</project>

上面将定义许多插件的默认值,如 maven-compiler-plugin, maven-resources-plugin 等

  1. 除此之外你的结构看起来不错......我会做一个小的改进建议是如果您定义模块之间的依赖关系:
  <dependencies>
<dependency>
<groupId>sunshine.web</groupId>
<artifactId>shaker-multi-core</artifactId>
<version>0.0.1</version>
</dependency>


<dependency>
<!-- JSP & Servlet dependencies -->
</dependency>

</dependencies>

我建议像这样定义模块间依赖关系:

  <dependencies>
<dependency>
<groupId>sunshine.web</groupId>
<artifactId>shaker-multi-core</artifactId>
<version>${project.version}</version>
</dependency>


<dependency>
<!-- JSP & Servlet dependencies -->
</dependency>

</dependencies>

关于java - Maven 多模块 : error assembling WAR: webxml attribute is required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25674839/

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