Search code examples
mavenparent-pom

Generate parent pom for existing project modules


I have following project modules. Each of the modules have their own pom.xml . Now I want to generate a parent POM (for existing modules like below) . Is there a way to do that? I know there is a way we can do if we are creating brand new project but my need here is I already have a project structure.

  1. project-module-1
  2. project-module-2
  3. project-module-3
  4. project-module-4

Thank you for your help


Solution

  • You need revising your exising project a little, it is not too hard.

    Your project structure like this

    enter image description here

    parent pom.xml

    <?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.example</groupId>
        <artifactId>sample-parent</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <packaging>pom</packaging>
        <description>sample</description>
        <url>http://example.com</url>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>8</maven.compiler.source>
            <maven.compiler.target>8</maven.compiler.target>
        </properties>
        <modules>
            <module>project-module-1</module>
            <module>project-module-2</module>
            <module>project-module-3</module>
            <module>project-module-4</module>        
        </modules>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
        <build>
            <plugins>
            </plugins>
        </build>
    </project>
    

    Child pom.xml

    <?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>
        <parent>
            <groupId>com.example</groupId>
            <artifactId>sample-parent</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </parent>
        <groupId>com.example</groupId>
        <artifactId>project-module-1</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <packaging>pom</packaging>
        <description>sample</description>
        <url>http://example.com</url>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>8</maven.compiler.source>
            <maven.compiler.target>8</maven.compiler.target>
        </properties>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
        <build>
            <plugins>
            </plugins>
        </build>
    </project>
    

    Sample project source code: https://github.com/donhuvy/maven_parent_childs/archive/master.zip