Dependent project not reading properties from parent POM - maven

I have the following project structure:
Parent
-- Module A
-- Module B
-- Module C
-- Module D
Parent has the following properties:
<properties>
<ModuleA.version>1.8</ModuleA.version>
</properties>
In module C, I am specifying dependency of A, using ${ModuleA.version}.
Module D depends on Module C, which in turn depends on Module A. Now, when I am running mvn clean install on Module D, considering that they share the same parent, I am expecting that the properties defined in parent would be available and hence ModuleA.version would be resolved to 1.8 and used.
However, the command is failing with the error:
The following artifacts could not be resolved: ModuleA:jar:${ModuleA.version}
Can someone help me with what I am missing here.
In all the modules I am specifying parents correctly.
That is: Module C and Module D mentions Module B as parent. Module B and Module A mentions Parent as parent.
Actual POM's below:
Parent:
<groupId>com.dummy</groupId>
<artifactId>parent</artifactId>
<version>0.15-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Parent</name>
<properties> <store-version>0.19-SNAPSHOT</store-version>
<data-version>0.16-SNAPSHOT</data-version>
</properties>
Data POM
<parent>
<groupId>com.dummy</groupId>
<artifactId>parent</artifactId>
<version>0.15-SNAPSHOT</version>
<relativePath>../../parent</relativePath>
Store POM:
<parent>
<groupId>com.dummy</groupId>
<artifactId>parent</artifactId>
<version>0.15-SNAPSHOT</version>
<relativePath>../../parent</relativePath>
</parent>
<dependency>
<groupId>com.dummy</groupId>
<artifactId>data</artifactId>
<version>${data-version}</version>
<scope>compile</scope>
</dependency>
When I use mvn clean install for Store project, it finds Data version and gives no errors.
Source POM
<parent>
<groupId>com.dummy</groupId>
<artifactId>parent</artifactId>
<version>0.15-SNAPSHOT</version>
<relativePath>../parent</relativePath>
</parent>
<artifactId>source</artifactId>
<packaging>pom</packaging>
<version>0.6-SNAPSHOT</version>
Source API POM
<parent>
<groupId>com.dummy</groupId>
<artifactId>source</artifactId>
<version>0.6-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependency>
<groupId>com.dummy</groupId>
<artifactId>store</artifactId>
<version>${store.version}</version>
<scope>compile</scope>
</dependency>
Source API builds fine, finds proper versions.
Source WS
<parent>
<groupId>com.dummy</groupId>
<artifactId>source</artifactId>
<version>0.6-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependency>
<groupId>com.dummy</groupId>
<artifactId>source-api</artifactId>
<version>${project.version}</version>
</dependency>
When I try to build source-ws, the build fails with error, ${data-version} not found.

I created the following projects according to your description:
+- P
+- A -> parent P
+- B -> parent P
+- C -> parent B, depends on A
+- D -> parent B, depends on C
P's POM
<groupId>so.55374493</groupId>
<artifactId>P</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<ModuleA.version>1.8</ModuleA.version>
<revision>1.8</revision>
</properties>
<modules>
<module>../A</module>
<module>../B</module>
</modules>
A's POM
<parent>
<groupId>so.55374493</groupId>
<artifactId>P</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../P</relativePath>
</parent>
<artifactId>A</artifactId>
<version>${ModuleA.version}</version>
<!-- use the following instead to omit:
"[WARNING] 'version' contains an expression but should be a constant." -->
<!-- <version>${revision}</version> -->
B's POM
<parent>
<groupId>so.55374493</groupId>
<artifactId>P</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../P</relativePath>
</parent>
<artifactId>B</artifactId>
<packaging>pom</packaging>
<modules>
<module>../C</module>
<module>../D</module>
</modules>
C's POM
<parent>
<groupId>so.55374493</groupId>
<artifactId>B</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../B</relativePath>
</parent>
<artifactId>C</artifactId>
<dependencies>
<dependency>
<groupId>so.55374493</groupId>
<artifactId>A</artifactId>
<version>${ModuleA.version}</version>
</dependency>
</dependencies>
D's POM
<parent>
<groupId>so.55374493</groupId>
<artifactId>B</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../B</relativePath>
</parent>
<artifactId>D</artifactId>
<dependencies>
<dependency>
<groupId>so.55374493</groupId>
<artifactId>C</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
mvn install on P and D both succeed (where A at the former prints the well-known [WARNING] 'version' contains an expression but should be a constant., which can be resolved by ${revision} since Maven 3.5).
Can you confirm that your POMs look equally in terms of <parent>s, <properties> (except ${revision}), <version>s and <dependencies>?

Related

I found mvn dependency transitive not work, why it happened?

my project A import a dependency jar B, and B import dependency jar C.they all use compile scope.
when I use "mvn dependency:tree" command to show all the dependencies, I could not found
the jar C in my project. And i also could not saw any jar C in my IDE External Libraries.
Is there some special mechanism in mvn?
I display the code below.
1、project A dependency gfintertrade-common-service-nearby
<parent>
<groupId>com.alipay.findataprod.service</groupId>
<artifactId>findataprod-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.alipay.findataprod.service</groupId>
<artifactId>findataprod-biz-service-impl</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Alipay findataprod-biz-service-impl</name>
<description>Alipay findataprod</description>
<url>http://home.alipay.com</url>
<packaging>jar</packaging>
<dependencies>
<!-- project depends -->
<dependency>
<groupId>com.alipay.gfintertrade</groupId>
<artifactId>gfintertrade-common-service-nearby</artifactId>
<version>1.0.0.20200930</version>
</dependency>
</dependencies>
2、gfintertrade-common-service-nearby dependency cryptprod-common-service-crypto
<parent>
<groupId>com.alipay.gfintertrade</groupId>
<artifactId>gfintertrade-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.alipay.gfintertrade</groupId>
<artifactId>gfintertrade-common-service-nearby</artifactId>
<version>1.0.0.20200930</version>
<name>Alipay gfintertrade-common-service-nearby</name>
<description>Alipay gfintertrade-common-service-nearby</description>
<url>http://home.alipay.net</url>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.alipay.cryptprod</groupId>
<artifactId>cryptprod-common-service-crypto</artifactId>
<version>1.2.2.20180516</version>
</dependency>
</dependencies>
3、I found project A not dependency cryptprod-common-service-crypto
gfintertrade-common-service-nearby is from remote repository

Maven pom version inheritance

I have 3 projects Parent,Child,SubChild.
Project Parent pom is as follows:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
</parent>
<groupId>mu.parent</groupId>
<artifactId>parent-system</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
....
Project Child pom is defined below and its Parent is defined follows:
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>mu.parent</groupId>
<artifactId>parent-system</artifactId>
<version>1.0</version>
</parent>
<groupId>mu.dummy.child</groupId>
<artifactId>child-backend</artifactId>
<name>child-backend</name>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>subChild-app</module>
</modules>
...
Now subChild pom is as follows and the child is defined as parent for subChild :
<parent>
<groupId>mu.dummy.child</groupId>
<artifactId>child-backend</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>mu.dummy.subchild</groupId>
<artifactId>subchild-backend</artifactId>
<name>subchild-backend</name>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>org.project.test</groupId>
<artifactId>project</artifactId>
<version></version> --version of parent-system???
</dependency>
</dependencies>
Is it possible to get version of parent-system(1.0) in subchild-backend please without hardcoding it?
You can use CI-friendly versions and write ${revision} for the versions, setting this property in the main pom (from which you build everything).
You need to use the flatten Maven plugin, though, to get proper POMs in the repository.
I asked a similar question a while back and determined that tthe groovy-maven-plugin works for this. See this answer.

Maven - Add a maven module as dependency to other maven module

I have 2 Spring boot maven projects(module A, Module B). And they both are added as modules to a parent project. Both Modules have some common dependencies and java classes(Domain objects), so I created third module Module C, and placed all the common java files and dependencies there.
I added the Module C to parent pom as one of the module. And added Module C as dependency to Module A and Module B. In Module A, B wherever the classes of Module C is referred there it was resolved and is pointing to Module C Classes (on ctrl+click).No error was shown in eclipse and maven dependencies are updated. But when I build the projects either from Parent pom or build Module A alone (after building the module c), I get cannot find symbol error in the places where module C classes were referenced.
Below are my pom.xml's
Parent pom.xml
<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.2.5.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.services</groupId>
<artifactId>cloud-services</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>ModuleC</module> <!-- Project where common dependencies and Common java classes are placed -->
<module>ModuleA</module>
<module>ModuleB</module>
</modules>
</project>
Module C Pom.xml - Common Project
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example.services</groupId>
<artifactId>cloud-services</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>ModuleC</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ModuleC</name>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencyManagement>
</project>
Module A Pom.xml - Dependent Project on Module C
<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.services</groupId>
<artifactId>cloud-services</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>ModuleA</artifactId>
<dependencies>
<!-- Project where common dependencies and Common java classes are placed -->
<dependency>
<groupId>com.example.services</groupId>
<artifactId>ModuleC</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
Module B Pom.xml
Same as ModuleA.
Whats wrong with my configuration. Any help is much appreciated. Thanks.
If its a spring project the root folder of the jar would be BOOT-INF.
If you create a normal project jar it wouldn't be having BOOT-INF.
The way it works is if you have a Project A and it has dependency on x.jar
Say Project A needs com.x.y.ClassA.class from x.jar it will search in root folder of x.jar with this path x/y/ClassA.class.
In your case as it was a spring project the resolution didnt work and it was complaining about that.
I guess that the spring-boot-starter-parent is the culprit.
By using it as a parent POM to your parent POM, you are building all your modules with it. AFAIK, jars build with spring-boot-starter-parent cannot be used as dependencies because the classes are moved to a different directory inside the jar.
As I am not Spring expert, I cannot really offer you a good solution for this.

maven multiple modules springbootr:boot run error

error message:
The POM for rc:common:jar:1.0 is missing, no dependency information available
parent pom.xml
<modelVersion>4.0.0</modelVersion>
<groupId>rc</groupId>
<artifactId>springboot-multiple-maven-modules</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<modules>
<module>rest</module>
<module>common</module>
</modules>
rest pom.xml
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>rc</groupId>
<artifactId>springboot-multiple-maven-modules</artifactId>
<version>1.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>rest</artifactId>
<name>zeyo rest</name>
<!--<version>1.0</version>-->
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>rc</groupId>
<artifactId>common</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
common pom.xml
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>rc</groupId>
<artifactId>springboot-multiple-maven-modules</artifactId>
<version>1.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>common</artifactId>
<name>common</name>
<version>1.0</version>
<packaging>jar</packaging>
Once you have a dependency called rc -> rest -> common.
Why is this happening?
That simply says that common project not yet built and we started rest.
First run the maven install on parent project so that 'common' project will be built as well.
spring-boot:run : does not build the dependent jars.
Hope this helps.

Maven Multi Module Project looking for modules in repository instead of Building them

I have a multi-module MVN project, which has 1 aggregator pom, another parent pom, and 3 other modules, as follows:
project-all
-project-parent
-project-common
-project-maintainance
-project-webApp
project-WebApp has a dependency on project-common and project-maintenance.
When I run a clean install on project-all, it tries to download the modules from repositories instead of building them.
My understanding of a multi-module project is that it runs an install on all the modules as well, but that is not what is happening.
If I run a clean install on all the modules separately, the project-all clean install works fine. It also works when I run the mvn build in Eclipse with the Resolve Workspace Artifacts option selected.
However, both of these options are not viable options, since the idea of using a multi-module project is to run multiple poms from a single location.
Am I missing something, what would be the process of asking mvn to build those modules instead of looking for them in the repository.
Pom for project-all:
<?xml version="1.0" encoding="UTF-8"?>
<groupId>com.midtier.api</groupId>
<artifactId>project-all</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>project-all</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<modules>
<module>project-parent</module>
<module>project-data</module>
<module>project-maintenance</module>
<module>project-WebApp</module>
</modules>
POM for project-parent:
<?xml version="1.0" encoding="UTF-8"?>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<groupId>com.midtier.api</groupId>
<artifactId>project-parent</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<name>${project.artifactId}</name>
<description>${project.artifactId}</description>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<elasticsearch.version>6.2.4</elasticsearch.version>
<jsonsimple.version>1.1.1</jsonsimple.version>
<slf4j-version>1.7.25</slf4j-version>
</properties>
<dependencyManagement>
<!--Dependencies Here-->
</dependencyManagement>
Pom for project-Webapp:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<parent>
<groupId>com.midtier.api</groupId>
<artifactId>project-parent</artifactId>
<version>1.0</version>
</parent>
<groupId>com.rbc.midtier.api</groupId>
<artifactId>project-webapp</artifactId>
<packaging>jar</packaging>
<version>${project.parent.version}</version>
<name>${project.artifactId}</name>
<description>${project.artifactId}</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.midtier.api</groupId>
<artifactId>project-data</artifactId>
</dependency>
<dependency>
<groupId>com.midtier.api</groupId>
<artifactId>project-maintenance</artifactId>
</dependency>
</dependencies>
</project>
project-data and project-maintenance are similar to project-web app but they don't have any internal dependencies.
Again, what I want to do is, just run the clean install command on all, and not have to worry about building any of the modules individually.
I think the problem is that you define the parent as a module. When you run „clean“ the parent used by the other modules is still not available so Maven tries to find it within the repository. Better define the parent information within the project-all pom because during the build phase the parent has to be available.

Resources