Inheritance and construction with Maven

I started learning maven, and now I'm trying to make some realworld projects with starting simple. Maybe it looks simple, first of all, here, but I'm really confused about what I should do if I want to pack both parent and child projects at the same time.

(PS: I am using intellij idea btw)

here is my configuration

Project A

  • Suppose it depends on guava and gson

Project B

  • it will depend on project A

Project C

  • it will depend on project B

when I set the packing attribute of Project A to the jar, it gives an error, basically saying that "you have to set the packing element as pom if it is in the parent pump". But I know that if I install the packaging element as pom, it will not create a package.

, , , ! , jar ?

- ,

MODULE POM

<?xml version="1.0" encoding="UTF-8"?>
<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.deneme.module</groupId>
    <artifactId>ModuleGroup</artifactId>
    <packaging>pom</packaging>
    <version>1.0.1</version>
    <modules>
        <module>A</module>
        <module>B</module>
    </modules>


</project>

A

<?xml version="1.0" encoding="UTF-8"?>
<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.mg.A</groupId>
    <artifactId>A</artifactId>
    <version>1.0.2</version>
    <packaging>pom</packaging>

    <dependencies>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.2.3</version>
        </dependency>
    </dependencies>



</project>

B

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <packaging>jar</packaging>

    <parent>
        <artifactId>A</artifactId>
        <groupId>com.mg.A</groupId>
        <version>1.0.2</version>
        <relativePath>../A/pom.xml</relativePath>
    </parent>

    <groupId>com.mg.B</groupId>
    <artifactId>B</artifactId>
    <version>1.0.1</version>

</project>
+3
2

, .

: pom , , groupId, , . ,

: pom jar/pom , /

, :

  • A - , gson guava
  • B - B gson guava
  • C - , B, A, gson guava

:

Workspace
|- ModuleProject
|- ProjectA
|- ProjectB
\- ProjectC

( ):

pom:

<groupId>com.deneme.projectname</groupId>
<artifactId>ModuleGroup</artifactId>
<packaging>pom</packaging>
<version>1.0.1</version>

<modules>
    <module>../ProjectA</module>
    <module>../ProjectB</module>
    <module>../ProjectC</module>
</modules>

A:

<parent>
    <groupId>com.deneme.projectname</groupId>
    <artifactId>ModuleGroup</artifactId>
    <version>1.0.1</version>
    <relativePath>../ModuleProject/</relativePath>
</parent>

<artifactId>A</artifactId>
<version>1.0.2</version>
<packaging>jar</packaging>

<dependencies>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.2.3</version>
    </dependency>
</dependencies>

B:

<parent>
    <groupId>com.deneme.projectname</groupId>
    <artifactId>ModuleGroup</artifactId>
    <version>1.0.1</version>
    <relativePath>../ModuleProject/</relativePath>
</parent>

<artifactId>B</artifactId>
<version>1.0.2</version>
<packaging>jar</packaging>

<dependencies>
    <dependency>
        <groupId>com.deneme.projectname</groupId>
        <artifactId>A</artifactId>
        <version>1.0.2</version>
    </dependency>
</dependencies>

C:

<parent>
    <groupId>com.deneme.projectname</groupId>
    <artifactId>ModuleGroup</artifactId>
    <version>1.0.1</version>
    <relativePath>../ModuleProject/</relativePath>
</parent>

<artifactId>C</artifactId>
<version>1.0.2</version>
<packaging>jar</packaging>

<dependencies>
    <dependency>
        <groupId>com.deneme.projectname</groupId>
        <artifactId>B</artifactId>
        <version>1.0.2</version>
    </dependency>
</dependencies>
+1

A B C , , pom. , . .

maven pom ( ..), .

, ( pom) 3 : A, B C:

parentProject 
 * module A
 * module B
 * module C

mvn install parentProject (, A.jar, B.jar C.jar).

, pom , , . . :

https://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Project_Inheritance_vs_Project_Aggregation

+2

All Articles