Parent Tag Value in Maven

I created my project with the same structure as in the following code fragment.

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

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

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

 <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 use the configuration link AppP.java from ModP in AppC1.java and AppC2.java in ModC1 and ModC2.

I tried this and it looks like this is not working. I misunderstood the meaning of the parent tag in pom.xml? What do I need to do in maven for such a function?

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

Each answer will be highly appreciated.

thanks

+6
source share
2 answers

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

+4
source

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

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

Subprojects may have their own children, etc.

Each project, except the top level, must contain a type definition

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

Each parent module must contain a list of modules:

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

As far as I understand, in your case you keep all 3 projects in one directory, but one of them is the parent. It is possible, but I think your link to the parent error is wrong. The number of points is 3, and should be 1:

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

try the following:

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

+12
source

Source: https://habr.com/ru/post/903319/


All Articles