Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of parent tag in Maven

Tags:

java

maven

I created my project with structure same as in next snippet of code.

|-- modP

|   |-- pom.xml

|   |-- src

|   |   |-- main

|   |     `-- java

|   |         `-- com

|   |             `-- myorg

|   |                 `-- myapp

|   |                     `-- modP

|   |                         `-- AppP.java

|-- modC1

|   |-- pom.xml

|   |-- src

|   |   |-- main

|   |     `-- java

|   |         `-- com

|   |             `-- myorg

|   |                 `-- myapp

|   |                     `-- modC

|   |                         `-- AppM.java

|-- modC2

|   |-- pom.xml

|   |-- src

        |-- main

          `-- java

              `-- com

                  `-- myorg

                      `-- myapp

                          `-- modC2

                              `-- AppN.java

My pom.xml for modP is:

<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>com.myorg.myapp</groupId>

  <artifactId>modP</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <packaging>pom</packaging>



  <name>modP</name>

  <url>http://maven.apache.org</url>



  <properties>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  </properties>



  <dependencies>

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>3.8.1</version>

          <scope>test</scope>

    </dependency>

  </dependencies>

</project>

My pom.xml for C1 is :

<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">

<parent>

    <artifactId>modP</artifactId>

    <groupId>com.myorg.myapp</groupId>

    <version>0.0.1-SNAPSHOT</version>

    <relativePath>.../modP/pom.xml</relativePath>

</parent>

<modelVersion>4.0.0</modelVersion>



  <groupId>com.myorg.myapp</groupId>

  <artifactId>modC1</artifactId>

  <packaging>jar</packaging>



  <name>modC1</name>

  <url>http://maven.apache.org</url>

</project>

And pom.xml for C2 is:

<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">

<parent>

    <artifactId>modP</artifactId>

    <groupId>com.myorg.myapp</groupId>

    <version>0.0.1-SNAPSHOT</version>

    <relativePath>.../modP/pom.xml</relativePath>

</parent>

<modelVersion>4.0.0</modelVersion>



  <groupId>com.myorg.myapp</groupId>

  <artifactId>modC2</artifactId>

  <packaging>jar</packaging>



  <name>modC2</name>

  <url>http://maven.apache.org</url>

</project>

My question is can I using such configuration reference AppP.java from ModP in AppC1.java and AppC2.java in ModC1 and ModC2.

I tried this and it looks like this doesn't works. Did I misunderstood meaning of parent tag in pom.xml? What I need to do in maven for such function?

I read a lot of documentation, but now I'm even more confused than before reading. :(

Every answer will be highly appreciated.

Thanks

like image 244
Tiho Avatar asked Dec 07 '11 18:12

Tiho


People also ask

What is parent tag?

Written by Elisha Zhang. Nested tags are labels associated by hierarchy. The 'sub-tag' or 'child tag' is a tag that is more specific and can be categorized under a 'parent tag'. In this example, you will see that we have 3 'child-tags', “Intercom”, “Salesforce”, and “Slack” nested under the parent tag of “Integrations” ...

What is the use of parent in Maven?

A parent pom. xml file (or super POM) in Maven is used to structure the project in order to avoid redundancies and duplicate configurations by using an inheritance between different pom. xml files. If any dependency or properties are configured in both - parent and child - pom.

What is the difference between parent and dependency in Maven?

A dependency is libraries you need to get your code to compile. This can be your own code, or libraries such as Apache Commons. A parent contains information, but nothing to actually build, that is shared between a number of your projects.

What tag is required in parent dependencies?

Each dependency is supposed to have at least two main tags, which are groupId and artifactId.


2 Answers

Typically maven projects hierarchy is stored in hierarchical file system. Child projects are stored under parent, i.e. in your terms:

modP
    pom.xml
    modC1
        pom.xml
    modC2
        pom.xml

The sub projects may have their own children etc.

Each project except the higher-level one should contain definition like

<parent>
    <groupId>com.company</groupId>
    <artifactId>parent-artifact-id</artifactId>
    <version>1.0</version>
</parent>

Each parent module should hold list of modules:

<modules>
    <module>child1</module>     
    <module>child2</module>     
    <module>child3</module>     
</modules>

As far as I understand in your case you hold all 3 projects in the same directory but one of them is parent. It is possible but I think that your reference to parent's pom is wrong. Number of dots is 3 while should be 1:

<relativePath>.../modP/pom.xml</relativePath>

try this one:

<relativePath>../modP/pom.xml</relativePath>

like image 185
AlexR Avatar answered Sep 30 '22 20:09

AlexR


Since ModP is a pom (packaging) project, it should not contain any Java code.

like image 30
Puce Avatar answered Sep 30 '22 20:09

Puce