Maven 3 regression: Profile dependencies lost? - maven

I observed a change behavior between Maven 2 and Maven 3. Is this a bug or a feature? I couldn't find anything in the Maven 3.x Compatibility Notes
I use two different profiles to add dependencies for different environment in a multi-module project.
parent-pom:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>group</groupId>
<artifactId>main</artifactId>
<packaging>pom</packaging>
<version>1.0.0-SNAPSHOT</version>
<modules>
<module>common</module>
<module>portal</module>
</modules>
</project>
The common-pom has the profile in it:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>group</groupId>
<artifactId>common</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<parent>
<groupId>group</groupId>
<artifactId>main</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<profiles>
<profile>
<id>development</id>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>production</id>
<dependencies>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</profile>
</profiles>
</project>
The portal-pom has only the common dependency and should include the transitive dependency from the common module:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>group</groupId>
<artifactId>portal</artifactId>
<packaging>war</packaging>
<version>1.0.0-SNAPSHOT</version>
<parent>
<groupId>group</groupId>
<artifactId>main</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>group</groupId>
<artifactId>common</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
Building this project (from the parent) via Maven2 mvn -P production package results in a WAR which includes the transitive dependency. (I used those simple dependencies for easy reproduction)
Executing via Maven3, I get only the common.jar in the WAR, but not the transitive dependency.
Is this a desired behavior?
To make things even "interesting": If one of the profiles is activated by default (explicit in the common-pom as the activation is not derived from the parent to the child modules) these transitive dependencies are always add to the portal.
But filtering or property-setting via profiles works as expected.

This comment solves it the one way:
the situation described above does work when profile activation using properties is used
With property activation (and not via -P) it works in Maven2 and 3
But the real solution was "caused" by the release plugin:
The profile must be submitted to release-plugin via
-Darguments=-Pproduction

Not a solution, but your use case looks similar to this open maven bug. Interestingly, according to the bug details, it happens on earlier maven versions as well.

Related

How to make Quarkus use local library classes

I'm starting with Quarkus using Maven and can't seem to find a solution to this:
I have a Quarkus app with dependencies on the libraries A and B. Both are imported as "Modules" (not Maven modules!) in the IntelliJ IDEA project for my app.
When starting Quarkus in dev mode, it ignores the classes in target/ of A and B and instead loads them from the Maven repository. Therefore with every change in either A or B, I have to mvn install the respective library, so my Quarkus app uses the correct code.
Coming from Thorntail, this was not necessary. Is there a solution that doesn't require auto-installing A and B on every build and also makes HotSwap work for those libs?
Edit:
As #CrazyCoder requested, here's a minimal example of my 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>test</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<compiler-plugin.version>3.8.1</compiler-plugin.version>
<maven.compiler.parameters>true</maven.compiler.parameters>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<quarkus-plugin.version>1.8.3.Final</quarkus-plugin.version>
<quarkus.platform.version>1.8.3.Final</quarkus.platform.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-universe-bom</artifactId>
<version>${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>A</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>B</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>${quarkus-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
A and B are listed in IntelliJ IDEA under "Project Structure... > Modules > test > Dependencies" as Module Depenencies, not Maven Dependencies. So the code in A and B should be HotSwappable.
I eventually found the solution. As so often: Once you know it, it's trivial.
Open your run configuration, expand the Environment dropdown (only populated when an application module is selected) and check the option Resolve Workspace artifacts:

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

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.

Use property defined in maven dependency artifact

Is it possible, to access the properties defined in a dependent maven artificat without making it the parent artifact of current project?
Example:
My 3rd party dependent artifact has the following pom:
<project>
<groupId>3rd</groupId>
<artifactId>party</artifactId>
<version>1</version>
<properties>
<test>this is a test</test>
</properties>
</project>
My own project depends on this 3rd party artifacts and wants to reuse the property in some way:
<project>
<groupId>my</groupId>
<artifactId>own</artifactId>
<dependencies>
<dependency>
<groupId>3rd</groupId>
<artifactId>party</artifactId>
<version>1</version>
</dependency>
</dependencies>
<properties>
<mytest>${test} - in my artifact</mytest>
</properties>
</project>

Resources