Santhosh_Reddy
Santhosh_Reddy

Reputation: 274

Maven build with dependent projects

I am working on Java projects using maven and eclipse IDE.

I have 3 projects namely P1, P2 and 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

If I change the packaging to war I get below error

Project build error: 'packaging' with value 'war' is invalid. Aggregator projects require 'pom' as packaging.

I wanted to build a war consisting of P1, P2 and Main Project class files in the war generated.

Can anyone give any pointers to achieve the same

Thanks in Advance

Upvotes: 2

Views: 838

Answers (2)

Thomas Weglinski
Thomas Weglinski

Reputation: 1094

Packaging POM is used for aggregation of the modules, their common dependencies (via Dependency Management section) or plugins (via Plugin Management section) etc.

In your case, you have to move the sources from the "Main Project" with packaging POM to another module, ex.: P0. Then you should have a packaging WAR on this P0 module. Additionally, the P0 module can be dependent on P1 and P2 module.

You may have project structure like this:

  • Main Project - packaging POM with modules: P0, P1, P2
    • P0 - packaging WAR with the sources from Main Project, dependent on: P1, P2
    • P1 - packaging JAR
    • P2 - packaging JAR

When you build the Main Project with mvn package, you will have a WAR file in /target directory of P0 module.

Upvotes: 1

Daniel
Daniel

Reputation: 4211

You can't mix packaging types (pom and war). You need to introduce a fourth 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. Maven/Eclipse will figure out the build order for you. 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.

You can also put common configurations in the above POM at let the sub-modules inherit from it if you like.

Upvotes: 3

Related Questions