0

I am using Maven for building my code. I created module based maven structure like below

  • Parent-POM
    • Sub-Parent1
      • SP1_Child1
      • SP1_Child2
      • SP1_Child3
    • Sub-Parent2
      • SP2_Child1
      • SP2_Child2
      • SP2_Child3

All my module versions, and external dependency versions are maintained in the Parent POM. Everything works fine when I do a complete mvn install, but when I try to build one sub module like SP1_Child1, then the build fails, because it is not able to identify the version of its dependencies. I checked the maven repository in my local machine, and all my modules were installed, but the .POM files do not have the version numbers. This is probably because the where the mvn install on the Parent POM is not replacing the ${module.version} with the actual version for the child modules.

Parent-POM

<project ..> <groupId>the.company.project</groupId> <artifactId>Parent-POM</artifactId> <version>1.0-SNAPSHOT</version> ... <properties> <module.version>1.0</module.version> </properties> </project> 

SP1_Child1

<project ..> <parent> ... </parent> <groupId>the.company.project</groupId> <artifactId>SP1_Child1</artifactId> <version>${module.version}</version> ... </project> 

How how can my mvn install update the versions in the .POM files in the maven repository? Or how can I run one of my sub-modules without any version errors?

2 Answers 2

2

The default layout of a child pom should look like this.

<project ..> <parent> <groupId>the.company.project</groupId> <artifactId>SP1_Child1</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>the.company.project</groupId> <artifactId>SP1_Child1</artifactId> ... </project> 

But you child should not define version separately only in the parent element without using a property. The version is automatically inherited to the child module from the parent. If you have the same group you also don't need to define the group in child. You can use it like this:

<project ..> <parent> <groupId>the.company.project</groupId> <artifactId>SP1_Child1</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>SP1_Child1</artifactId> ... </project> 
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, What version SP1_Child1 will get if the parent section of SP1_Child1 says <groupid>the.company.project2</groupid> < artifactid>SP1_Parent2</artifactid> <version>1.0-SNAPSHOT</version> </parent> and where SP1_Parent2 of the.company.project2 has the latest version now at 0.0.5 (0.0.6-SNAPSHOT) ? i.e. even though the.company.project lists SP1_Parent1 as one of its module, and SP1_Child1 of the.company.project lists SP1_Child1 as one of its module, but this SP1_Child1 pom file's parent section says, the parent is not the.company.project but it's the.company.project2/SP1_Parent2.
1

Taking the SP1_Child1 version from parent will be very annoying for you because it will force you to install a new version of the parent for any new version of the SP1_Child1 project.

There are 2 different possible situations :

  • You want to be able to manage different project, with different lifecycles. Then you specify a version in the SP1_Child1 project, and you specify the version of SP1_Child1 to be used by other projects in the parent POM (in this case, the 2 values can be different).

  • Your application is monolithic, even if it is organized in different modules for convenience. Then in this case, the best is to do what khmarbaise advises, keep one version for all your projects, and inherit the version from the parent.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.