Home > Software engineering >  How can I make maven subprojects not look for the parent project when they get deployed?
How can I make maven subprojects not look for the parent project when they get deployed?

Time:09-17

In the parent pom, I have the modules declared.

`

<module>module1</module>
<module>module2</module>
<module>module3</module>
`

In each child project pom, I have the parent declared

`

<groupId>com.group</groupId>
<artifactId>parent-module</artifactId>
<version>---</version>
`

In the parent pom, I chose to skip the deployment of the parent pom due to a project-related constraint

parent pom: `

    <plugins>
      <plugin>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>3.0.0-M1</version>
        <configuration>
          <skip>true</skip>
        </configuration>
      </plugin>
    </plugins>
  </build>

`

that means I only have three sets of Jar and Pom when I deploy it.

  • childJar1
  • childPom1
  • childJar2
  • childPom2
  • childJar3
  • childPom3

However, when I use any of the jars above, I encounter errors during their usage because each pom declares the parent module and the parent module is not present (because I chose to skip the deployment of the parent module).

Is there any way I can make each submodule not declare or look for the parent?

CodePudding user response:

Usually, as khmarbaise said, you should just deploy the parent as well and everything is fine. This is the standard way to go.

If this is not possible (and I mean by "not possible" not just something like "I don't like it" or "it takes up space on the disk"), you can use the flatten Maven plugin to remove the parent during the build for the deployed POMs:

https://www.mojohaus.org/flatten-maven-plugin/

CodePudding user response:

The skip build shouldn't prevent them from being parents. But I think you might mix up "parents" and "dependencies". There is often one "master parent" for all modules, then cascade dependencies between modules. Also make sure that what you call parents are not "aggregators": if their only purpose is to list the modules, you don't have to set a build configuration at all, but also you won't make a dependency to them.

Below is an example, it's difficult to use your data as it's not complete

An aggregator (important: packaing:pom prevents it from trying to build):

<?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">
    <packaging>pom</packaging>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.foo</groupId>
    <artifactId>components</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <modules>
        <module>comp1</module>
        <module>comp2</module>
        <module>comp3</module>
    </modules>

</project>

One of the modules declared:

<?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">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.foo</groupId>
    <artifactId>comp1</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.foo</groupId>
            <artifactId>core</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.5.3</version>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Another module using that one, although build was skipped

<?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">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.foo</groupId>
    <artifactId>bar</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.foo</groupId>
            <artifactId>comp1</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.5.3</version>
                <configuration>
                    <skip>false</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>
  • Related