从多项目 Maven 配置生成 WAR 文件
我有一个 Maven 项目,有 4 个组件 Web、Persistence、Common 和其他。
我的 POM 文件中的相关内容:
Parent POM:
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
<modules>
<module>components/TestWeb</module>
<module>components/TestOther</module>
<module>components/TestPersistence</module>
<module>components/TestCommon</module>
</modules>
<build>
<defaultGoal>package</defaultGoal>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1</version>
<configuration>
<dependentWarExcludes>
**/hibernate.cfg.xml,**/sql-map-config.xml,**/web.xml,WEB-INF/classes/META-INF/**
</dependentWarExcludes>
</configuration>
</plugin>
</plugins>
</build>
common pom:
<modelVersion>4.0.0</modelVersion>
<artifactId>test-common</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<relativePath>../../pom.xml</relativePath>
<version>0.0.1-SNAPSHOT</version>
</parent>
persistence pom:
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>test-persistence</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<relativePath>../../pom.xml</relativePath>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.test</groupId>
<artifactId>test-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
web pom:
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>test-web</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>TestWeb</name>
<parent>
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<relativePath>../../pom.xml</relativePath>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependency>
<groupId>com.test</groupId>
<artifactId>test-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.test</groupId>
<artifactId>test-persistence</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.test</groupId>
<artifactId>test-other</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
Maybe我在复制和粘贴过程中破坏了某些内容,但 XML 是有效的。
- 当我运行 mvn package 时,未创建项目 WAR 文件,但所有组件包已创建且格式良好。
- 然后,如果我运行
mvn war:war
,则 WAR 文件将生成为空。
如何解决这个问题?
I have a Maven project, with 4 components Web, Persistence, Common and Other.
The relevant stuff off my POM files:
Parent POM:
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
<modules>
<module>components/TestWeb</module>
<module>components/TestOther</module>
<module>components/TestPersistence</module>
<module>components/TestCommon</module>
</modules>
<build>
<defaultGoal>package</defaultGoal>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1</version>
<configuration>
<dependentWarExcludes>
**/hibernate.cfg.xml,**/sql-map-config.xml,**/web.xml,WEB-INF/classes/META-INF/**
</dependentWarExcludes>
</configuration>
</plugin>
</plugins>
</build>
common pom:
<modelVersion>4.0.0</modelVersion>
<artifactId>test-common</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<relativePath>../../pom.xml</relativePath>
<version>0.0.1-SNAPSHOT</version>
</parent>
persistence pom:
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>test-persistence</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<relativePath>../../pom.xml</relativePath>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.test</groupId>
<artifactId>test-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
web pom:
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>test-web</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>TestWeb</name>
<parent>
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<relativePath>../../pom.xml</relativePath>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependency>
<groupId>com.test</groupId>
<artifactId>test-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.test</groupId>
<artifactId>test-persistence</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.test</groupId>
<artifactId>test-other</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
Maybe I broke something in the copy&paste, but the XML is valid.
- when I run mvn package, the project WAR file isn't created, but all components packages are created and well formed.
- if, then, I run
mvn war:war
, the WAR file is generated empty.
How can fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在根项目中安装插件不起作用。您可以在此处配置插件,如下所示
,但仍然必须在子项目中引用它们才能激活(默认进程中的插件除外,例如 maven-compiler-plugin 和 maven-resource-plugin)。
因此,您可以将 war 插件配置移至根项目中的
pluginManagement
并包含在您的 war 项目中,或者将整个配置移至 war 中。
附加说明:
确保根 pom 中模块的顺序与项目之间的依赖关系对齐(在您的情况下只需颠倒顺序)。
Having plugins in the root project does not work. You can configure the plugins here, like this
but they still have to be referenced in the subprojects to be active (except for plugins that are part of the default process, like maven-compiler-plugin and maven-resource-plugin).
So either you move your war-plugin configuration to
pluginManagement
in the root project and includein your war project or you move the entire configuration into the war.
Additional note:
Make sure the order of the modules in the root pom is aligned to the dependency relations between the projects (In your case just reverse the order).
我创建了一个类似的项目结构并粘贴了您提供的 POM,但无法重现该问题。从聚合 pom 运行
mvn package
会按预期工作:并产生以下结果:
所以你的问题一定与你的 WAR 项目本身、它的结构或类似的东西有关。请显示它的结构以及 Maven 在其上运行
war:war
时的输出。顺便说一句,这是 POM 在多模块构建中通常的样子:
另一件事,来自父项目的 war 插件配置是继承的(您可以通过运行
help: effective-pom
在 Web 模块中),但在 Web 模块本身中配置它是有意义的。I created a similar project structure and pasted the POMs you provided but couldn't reproduce the problem. Running
mvn package
from the aggregating pom just works as expected:And produces the following result:
So your problem must have something to do with your WAR project itself, its structure or something like this. Please show its structure and the output of Maven when running
war:war
on it.By the way, here is how a POM should typically look like in a multi-modules build:
Another thing, the war plugin configuration from the parent project is inherited (you can check that by running
help:effective-pom
in the web module) but it would make sense to configure it in the web module itself.