Maven多模块:错误组装WAR:需要webxml属性 [英] Maven multi module: error assembling WAR: webxml attribute is required

查看:75
本文介绍了Maven多模块:错误组装WAR:需要webxml属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在多模块Maven项目中打开一个由Eclipse Maven管理的webapp项目(这是一个用于测试Maven的测试项目,请随时提供任何建议).

单个项目webapp没有任何错误,可以成功地编译并在部署时正常运行,所以我从一个正常运行的应用程序开始.

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

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

  • 核心(包含其他两个部分共有的所有内容);
  • 可运行(包含具有main()方法的类);
  • 网络应用程序(网络应用程序部分).

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

  • shaker-multi保存聚合器(和父级)POM,以及子目录中的每个模块;
  • shaker-multi-core;
  • shaker-multi-runnable;
  • shaker-multi-webapp.

在Eclipse内部, core webapp 可以进行编译,并且可以将后者部署到Tomcat实例,并且可以在浏览器中看到它.

问题是 runnable 引起的.该项目依赖于jOOQ类,因此必须生成相关的源代码. jOOQ依赖关系和配置在core/pom.xml中(因为它们也可以在其中使用).

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

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

这听起来很合理,因为我什至没有在本地存储库中安装任何这些工件.

但是当我在shaker-multi上调用Maven build ...->'clean install'时,它中断了,因为它找不到shaker-multi-webappweb.xml文件(尽管它正确地驻留在shaker-multi-webapp/src/main/webapp/WEB-INF/web.xml中)

我该怎么办? 我的项目配置/拆分是否完全错误? 我是否应该使用父POM添加另一个模块?这听起来是错误的,因为POM参考指出:

继承和聚合创建了一个不错的动态特性,可以通过单个高级POM控制构建. 您经常会看到既是父母又是聚合者的项目.

我在这里完全迷路了.

我的期望:

  • shaker-multi-webapp上运行Maven package并获得可部署的 war ;
  • shaker-multi-runnable上运行Maven package并获取命令行可运行的 jar (尽管我仍然需要配置其POM以生成具有依赖关系的jar);
  • shaker-multi上运行Maven package并获得一种我可以移动的捆绑,其中包含 war jar .

编辑

我添加了

         <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,如此答案所示,但没有区别.


EDIT-2

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

[...]
05/09/14 07:58:19 CEST: [INFO] Adding source folder /shaker-multi-webapp/src/main/java
05/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**
[...]

有任何提示吗?它来自哪里?但是,我无法复制它(无需再次删除我的整个本地存储库).


这是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>
 

解决方案

这里的整个问题真的很简单,对此我感到有些as愧:但是作为我第一次进行多模块项目,我想可能会发生.

当然,当您独自使用所有项目和模块源时,这里的细节将保留:如果您是团队成员,仅处理项目的一部分和/或使用集中式私有存储库,则使用YMMV

首先,在将单个项目拆分为几个模块之后,这就是我的Eclipse Project Explorer中的情况:

+-- shaker-multi
        ^--- pom.xml
        ^--- shaker-multi-core
                    ^-- pom.xml
                    ^-- (other content)
        ^--- shaker-multi-runnable
                    ^-- pom.xml
                    ^-- (other content)
        ^--- shaker-multi-webapp
                    ^-- pom.xml
                    ^-- (other content)

+-- shaker-multi-core
            ^-- pom.xml

+-- shaker-multi-runnable
            ^-- pom.xml

+-- shaker-multi-webapp
            ^-- pom.xml

每个+--是一个单独的Eclipse项目.他们每个人都已经从SVN中单独签出(因此,他们实际上是彼此分离的).
然后,我正在+-- shaker-multi-runnable中编辑某些内容,并希望当我在+-- shaker-multi上运行Maven时能够正常工作,而无需svn提交前者和svn升级后者.

这就是为什么我一直在这个问题中得到错误的原因!

处理此类项目的正确方法(如果它们来自原始的整体项目)是:

  • 在子文件夹中拆分代码,资源等;
  • 将所有更改提交到存储库;
  • 擦除Eclipse中所有涉及的项目.

接下来,您进入IDE的SVN Repositories选项卡,使用现在已拆分的项目扩展存储库,并执行以Maven项目检出父项目(具有模块的项目).作为子文件夹).
如果您像我一样,正在使用Subclipse的最新版本,则需要 Maven Eclipse(m2e )用于subclipse 1.10(svn 1.8)的SCM连接器-更新站点(感谢 buluschek开发,请参阅该帖子的最后评论),以便您可以从以Maven项目签出对话框中选择存储库路径.

使用它,您告诉Eclipse从父文件夹中检出整个项目,并且Eclipse自动执行:

  1. 获取所有Maven项目模块;
  2. 创建一个与父项目相对应的项目,其中模块是子文件夹;
  3. 为每个模块创建一个单独的项目
  4. 将每个模块项目与父项目链接在一起,这样,模块源代码,资源,POM等中的每个编辑都会立即反映在父项目的文件夹中.

要点(4)是这里的关键:尽管有多个项目,但它们已经链接在一起(我猜想,手动签出每个模块文件夹时,可以完成相同的操作,尽管我不知道如何做.) >

这样做之后,我所有的Maven问题都消失了.

I'm turning a single Eclipse Maven-managed webapp project in a multi-module Maven project (this is a test project to experiment with Maven, so feel free to provide any kind of suggestion).

The single project webapp doean't have any error, succesfully compiles and behaves correctly when deployed, so I'm starting with a working application.

The application has a web part and a console part, meaning that there are some classes with a main() method that when run from within Eclipse (with Run as -> Java Application) work as expected. Both parts show data from a database, queried either directly through JDBC or through jOOQ.

So, this is how I split the project:

  • core (holds everything common to the other two parts);
  • runnable (contains the classes that have a main() method);
  • webapp (the web application part).

Inside Eclipse, I have now 4 separate projects:

  • shaker-multi holds the aggregator (and parent) POM, plus each module in a subdirectory;
  • shaker-multi-core;
  • shaker-multi-runnable;
  • shaker-multi-webapp.

Inside Eclipse, core and webapp compile, and the latter can be deployed to a Tomcat instance and I can see it in the browser.

The problem arises with runnable. That project relies on jOOQ classes, so the relevant source code must be generated. The jOOQ dependencies and configuration are in core/pom.xml (since they may be used there too).

When I do Project -> Run As -> Maven build... -> clean generate-sources, on shaker-multi-core I get:

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>

to shaker-multi-webapp POM, as seen in this answer, but with no difference.


EDIT-2

I cleared my whole local repository (as suggested here), and when I reopened Eclipse, in the Maven console I saw

[...]
05/09/14 07:58:19 CEST: [INFO] Adding source folder /shaker-multi-webapp/src/main/java
05/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>

This is 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>

This is 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>

解决方案

The whole problem here was really simple, and I feel a bit ashamed about it: but being the first time I did a multi–module project, I guess that could happen.

Of course, the details here hold when you work with all the project and modules sources by yourself: if you're in a team, working on only a part of the project and / or with a centralized private repository, then YMMV.

First of all, after I splitted the single–project into several modules, this was the situation in my Eclipse Project Explorer:

+-- shaker-multi
        ^--- pom.xml
        ^--- shaker-multi-core
                    ^-- pom.xml
                    ^-- (other content)
        ^--- shaker-multi-runnable
                    ^-- pom.xml
                    ^-- (other content)
        ^--- shaker-multi-webapp
                    ^-- pom.xml
                    ^-- (other content)

+-- shaker-multi-core
            ^-- pom.xml

+-- shaker-multi-runnable
            ^-- pom.xml

+-- shaker-multi-webapp
            ^-- pom.xml

Each +-- is a single, separated, Eclipse project. Each one of them has been singularly checked out from SVN (so they were, in fact, detached from one another).
I was, then, editing something in +-- shaker-multi-runnable and expecting that to work when I was running Maven on +-- shaker-multi, without svn–committing the former and svn–updating the latter.

That's why I kept getting the error in this question!

The proper way to handle such projects, if they come from an originally monolithic project is:

  • to split the code, resources, etc in subfolders;
  • to commit every change to the repository;
  • to erase every involved project in Eclipse.

Next, you go in the SVN Repositories tab of the IDE, expand the repository with the now splitted project and do Check out as Maven project of the parent–project (the one that has modules as subfolders).
If you, like me, are working with a recent version of Subclipse, you'll need the Maven Eclipse (m2e) SCM connector for subclipse 1.10 (svn 1.8) - update site (thanks go to buluschek development, see the last comments on that post), so that you can choose the repository path from the Check out as Maven Project dialog.

With it, you tell Eclipse to checkout the whole project from the parent folder, and the Eclipse automatically:

  1. fetches all the Maven project modules;
  2. creates a project corresponding to the parent–project, where modules are subfolders;
  3. creates a separate project for every single module
  4. links together each module project with the parent project, so that each edit in a module source code, resource, POM, etc is instantly reflected inside the folders of the parent-project.

That point (4) is the key here: although there are several projects, they have been linked together (I guess the same can be done when manually checking out each module folder, although I do not know how).

After having done this, all the Maven problems I had disappeared.

这篇关于Maven多模块:错误组装WAR:需要webxml属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆