简体   繁体   English

使用相关项目进行Maven构建

[英]Maven build with dependent projects

I am working on Java projects using maven and eclipse IDE. 我正在使用Maven和Eclipse IDE进行Java项目。

I have 3 projects namely P1, P2 and Main Project 我有3个项目,分别是P1,P2和Main Project

I have individual poms for P1, P2, Main Project, I have included P1, P2 as modules for Main Project and Main project is accepting packaging as pom only 我为P1,P2,主项目设置了单独的pom,我将P1,P2作为主项目的模块包含在内,并且主项目仅接受pom包装

If I change the packaging to war I get below error 如果我把包装换成战争,我会得到以下错误

Project build error: 'packaging' with value 'war' is invalid. 项目构建错误:值为“ war”的“包装”无效。 Aggregator projects require 'pom' as packaging. 聚合器项目需要“ pom”作为包装。

I wanted to build a war consisting of P1, P2 and Main Project class files in the war generated. 我想在产生的战争中建立一场由P1,P2和Main Project类文件组成的战争。

Can anyone give any pointers to achieve the same 任何人都可以提供任何指针以实现相同的目标

Thanks in Advance 提前致谢

You can't mix packaging types ( pom and war ). 您不能混合包装类型( pomwar )。 You need to introduce a fourth POM: 您需要引入第四个POM:

<project>
  <groupId>org.example</groupId>
  <artifactId>modules</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <modules>
   <module>P1</module>
   <module>P2</module>
   <module>Main</module> <!-- This contains your WAR -->
  </modules>
  ...
</project>

Your project Main depends on P1 and P2 . 您的项目Main取决于P1P2 Maven/Eclipse will figure out the build order for you. Maven / Eclipse将为您确定构建顺序。 You don't even need to import the module artifact in Eclipse if you don't want to, but you probably need to install the top POM at least once. 如果不想,甚至不需要在Eclipse中导入module构件,但是您可能至少需要安装一次顶级POM。

You can also put common configurations in the above POM at let the sub-modules inherit from it if you like. 您还可以在上面的POM中放入通用配置,如果愿意,可以让子模块从其继承。

Packaging POM is used for aggregation of the modules, their common dependencies (via Dependency Management section) or plugins (via Plugin Management section) etc. 打包POM用于模块,其公共依赖项(通过“依赖关系管理”部分)或插件(通过“插件管理”部分)的聚合。

In your case, you have to move the sources from the "Main Project" with packaging POM to another module, ex.: P0 . 在您的情况下,必须将源从打包了POM的“主项目”移到另一个模块,例如: P0 Then you should have a packaging WAR on this P0 module. 然后,您应该在此P0模块上有包装的WAR。 Additionally, the P0 module can be dependent on P1 and P2 module. 另外,P0模块可以依赖于P1P2模块。

You may have project structure like this: 您可能具有以下项目结构:

  • Main Project - packaging POM with modules: P0 , P1 , P2 Main Project -带模块的包装POM: P0P1P2
    • P0 - packaging WAR with the sources from Main Project, dependent on: P1 , P2 P0将WAR与Main Project一起打包,取决于: P1P2
    • P1 - packaging JAR P1包装JAR
    • P2 - packaging JAR P2包装JAR

When you build the Main Project with mvn package , you will have a WAR file in /target directory of P0 module. 使用mvn package构建Main Project时,您将在P0模块的/target目录中拥有一个WAR文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Netbeans和Maven,清洁和构建依赖项目 - Netbeans and Maven, Clean and Build with dependent projects 如果在Maven中构建父项目时未构建依赖项目,则自动构建 - Build dependent projects automatically if they are not build when bulding parent project in Maven 是否可以与相关项目共享构建Maven配置文件属性 - Is it possible to share build maven profile properties with dependent projects Maven-Nexus:查找相关项目 - Maven - Nexus: find dependent projects 在依赖的Maven项目上测试失败 - Test failures on dependent maven projects Android应用程序项目中的依赖库项目的NoClassDefFoundError(使用maven作为构建工具) - NoClassDefFoundError for dependent library projects in Android app project (using maven as build tool) 如何使用Maven使用对多个相关Java项目的Java类引用来构建Java EE项目? - How to use Maven to build a Java EE project with Java class references to several dependent Java projects? 在 Maven 中,在构建主项目时,如何构建作为依赖项添加到主项目中的依赖项目? - In Maven How to build the dependent projects that are added as the dependencies in the main project when building the main project? 依赖 maven 项目 heroku 或 google cloud 部署 - Dependent maven projects heroku or google cloud deployment 带有相关Maven项目的最佳Eclipse(Luna)设置 - Best Eclipse (Luna) setup with dependent Maven projects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM