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

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

Related

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.

Dependent project not reading properties from parent POM

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>?

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.

spring boot multi modules package

I am trying to use Maven to package Spring Boot with multi modules,Here my main module pom.xml :
<modules>
<module>my-data-services</module>
<module>my-message-services</module>
<module>my-common</module>
</modules>
<groupId>com.my</groupId>
<artifactId>my-multi-services</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>....</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
my-common pom.xml:
<parent>
<artifactId>my-multi-services</artifactId>
<groupId>com.my</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>my-common</artifactId>
<packaging>jar</packaging>
<dependencies>....</dependencies>
and my-data-services pom.xml:
<parent>
<artifactId>my-multi-services</artifactId>
<groupId>com.my</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>my-data-services</artifactId>
<dependencies>
<dependency>
<groupId>com.my</groupId>
<artifactId>my-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
my-common module is just a common utils lib not a runnable module,but when i trying to mvn clean package,exception throws below:
Execution default of goal org.springframework.boot:spring-boot-maven-p
lugin:1.4.2.RELEASE:repackage failed: Unable to find main class
then i add a main class,this module can package,but its not a lib jar, its like a runnable spring boot jar
-BOOT-INF
|
-META-INF
|
-org
and the exception throws
Failed to execute goal org.apache.maven.plugins:maven-compiler- plugin:3.1:compile (default-compile) on project my-data-services: Compilation failure: Compilation failure: package com.my.common.utils does not exist;
com.my.common.utils is in module my-common
How do i fix this problem, and in spring boot multi modules project,how to package a common utils lib without BOOT-INF
This happens because your modules will have the 'spring-boot-maven-plugin' added to them because it's defined in their parent.
What you need to do is move everything into submodules, even your application starter class. How I do this usually:
my-parent-module
my-service-module
my-common-module
my-web-module (or my-runtime-module in a non-web app)
The my-parent-module will have 'spring-boot-starter-parent' as it's parent but it not going to have an src folder because everything is moved into submodules.
The my-web-module will depend on the other modules and will have the 'spring-boot-maven-plugin'. You will be able to run the app with 'mvn spring-boot:run' in the my-web-module folder.

How does Maven handle transitive dependencies inherited from parent?

Given the parent and child pom below and lib1 and lib2 both include the class foo.bar.Test.
parent pom
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>foo</groupId>
<artifactId>foo-parent</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>my.transitive</groupId>
<artifactId>lib1</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
child pom
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>foo</groupId>
<artifactId>foo-parent</artifactId>
<version>1.0</version>
</parent>
<artifactId>foo-child</artifactId>
<dependencies>
<dependency>
<groupId>my.transitive</groupId>
<artifactId>lib2</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
If I include foo-child as a dependency in myApp and instantiate foo.bar.Test, which version of the class would Maven resolve to? And why?
my app pom
<project>
<modelVersion>4.0.0</modelVersion>
<artifactId>myApp</artifactId>
<groupId>myApp</groupId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>foo</groupId>
<artifactId>foo-child</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
The short answer would be: it depends on which one is found on the classpath first.
Having 2 of the same classes packaged with an application is not ideal as it can lead to many difficult to debug errors... If your myApp project is just going to be a jar then it would be best to compile it using the same library as what is going to be available to it at runtime.
I believe maven uses the order it is written to the pom to build. One way of looking at this would be to run the following command for myApp:
mvn dependency:tree -Dverbose
This will print the dependencies in the order that they should appear on the classpath per spec. You can always use exclusions to exclude any inherited library you might not want. Hope this helps.

Resources