tags:

views:

30

answers:

1

Hi all, I have parent web module called "PARENT". And I have another web module called "CHILD". PARENT module's packaging is war. How can I add a dependency in CHILD module. I have tried the following. But it doesn't works.

Root pom.xml

<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"&gt;
 <modelVersion>4.0.0</modelVersion>
 <groupId>MY_App</groupId>
 <artifactId>MY_App</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>pom</packaging>
 <name>MY Application</name>
 <url>http://maven.apache.org&lt;/url&gt;
 <repositories>
  <repository>
   <id>ibiblio</id>
   <name>iBiblio Maven2 Repository</name>
   <url>http://www.ibiblio.org/maven2&lt;/url&gt;
  </repository>
  <repository>
   <id>apache-repo</id>
   <name>Apache Repository</name>
   <url>http://people.apache.org/repo/m2-snapshot-repository&lt;/url&gt;
  </repository>
 </repositories>
 <modules>
  <module>MY_PARENT</module>
  <module>MY_CHILD</module>
 </modules>
</project>

PARENT web module pom.xml

<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 ">
 <modelVersion>4.0.0</modelVersion>
 <parent>
  <artifactId>MY_App</artifactId>
  <groupId>MY_App</groupId>
  <version>1.0-SNAPSHOT</version>
 </parent>
 <groupId>MY_App</groupId>
 <artifactId>MY_PARENT</artifactId>
 <packaging>war</packaging>
 <version>1.0-SNAPSHOT</version>
 <name>MY_PARENT Webapp</name>
 <url>http://maven.apache.org&lt;/url&gt;

</project>

CHILD web module pom.xml

<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 ">
 <modelVersion>4.0.0</modelVersion>
 <parent>
  <artifactId>MY_App</artifactId>
  <groupId>MY_App</groupId>
  <version>1.0-SNAPSHOT</version>
 </parent>
 <groupId>MY_App</groupId>
 <artifactId>MY_CHILD</artifactId>
 <packaging>war</packaging>
 <version>1.0-SNAPSHOT</version>
 <name>MY_CHILD Webapp</name>
 <url>http://maven.apache.org&lt;/url&gt;
 <dependencies>
  <dependency>
   <groupId>MY_App</groupId>
   <artifactId>MY_PARENT</artifactId>
   <version>1.0-SNAPSHOT</version>
   <type>war</type>
  </dependency>
 </dependencies>
</project>

While packaging the application I am getting compilation error. ie, CHILD module doesn't refers the classes of PARENT module.

Note: I have removed "http://maven.apache.org/maven-v4_0_0.xsd" this schema location because of the limitation.

+1  A: 

You can't use the classes from a parent project. Content is not inherited, only configuration (the pom).

In a parent / child relationship, the parent project usually has packaging pom, as it specifies configuration, but has no content.

What you can do is add a dependency in the child project

<dependency>
  <artifactId>MY_App</artifactId>
  <groupId>MY_PARENT</groupId>
  <version>1.0-SNAPSHOT</version>
  <type>war</type>
</dependency>

but packaging a war inside a war is not really elegant.

The proper thing to do this in maven would be to have three modules

<modules>
  <module>common</module>
  <module>war1</module>
  <module>war2</module>
</modules>

where common has packaging jar and includes all java classes used by both other projects and both war1 and war2 reference common as dependency.

seanizer
I second that (the only "valid" use case for war dependencies would be overlays but it doesn't make things more elegant). +1 by the way (casted earlier).
Pascal Thivent