Transitive dependency jars are not resolved from maven nexus repository - maven

I created my own maven project (project 1) where I have added dependencies like junit, spring, etc. and deployed the jar (name it as jar1 ) in my nexus maven repository.
Now when I add jar1 as dependency to my new project 2, only jar1 is getting downloaded from the maven repository and the transitive dependencies such as junit ,spring are not downloaded.
pom - project1
<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>Project1</groupId>
<artifactId>Project1</artifactId>
<version>Dev.0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
pom - project2
<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>Project2</groupId>
<artifactId>Project2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>Project1</groupId>
<artifactId>Project1</artifactId>
<version>Dev.0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
As per above pom configuration, project2 should automatically resolve junit jar right?.
But project2 only resolves Project1.jar and not the junit jar. Please let know what am I missing.

Not all scopes are transitive, especially test and provided dependencies are not.
Look at the table in https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

Related

How to find the source for an artifact version in Maven?

Assuming I have Maven pom (let's say Maven 3.8) like this:
<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>some.group</groupId>
<artifactId>my-parent</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>some.group</groupId>
<artifactId>my-artifact</artifactId>
</dependency>
</dependencies>
</project>
...where my-parent has a dependency management section, maybe bom imports and further parents up the chain with it's own dependency management sections, how can I find the source of the version of some.group:my-artifact that actually used in the child pom?
Let's say, I build this and see that the effective pom contains version 1.2.3 for this artifact, how can I find out the pom that actually contains that version?

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 dependencyMangement doesn't inherit the virtue of dependencies in the parent pom

All our external dependencies are defined in enterprise pom. We do not want to define scope of the dependencies in the enterprise pom cause we have applications that are deployed both to Tomcat and JBoss. As we all know couple of dependencies come bundled out of the box in JBoss but may not exist in Tomcat default installation.
bom-parent is the parent of all the application specific enterprise poms. We then created a second level enterprise pom for each app, in the example below its bom-app1. All the projects inherit the app specific enterprise poms.
Sample project with detail instructions and comments available on GitHub
GitHub : https://github.com/samirshaik/mvn-dep-issue.git
Problem:
In the app specific enterprise pom we would just like to define scope of the dependency declared in dependencyManagement section in the first level enterprise pom. But this is not possible as Maven forces us to declare the version of the dependencies re-declared in the app specific enterprise pom. We went ahead and used the same version property as is declared in first level enterprise pom and every thing worked, except that we lost all the properties of the parent dependency like the exclusion of all the transitive dependencies.
To us this is very genuine requirement but looks like dependencyManagement the way its designed today, doesn't consider the virtues of inheritance.
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.samir.enterprise</groupId>
<artifactId>bom-parent</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<name>Enterprise BOM</name>
<properties>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
Below is sample snippet of the child BOM, which should let me define the scope of the dependency and inherit all the features of the parent.
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.samir.enterprise</groupId>
<artifactId>bom-parent</artifactId>
<version>1.0.0</version>
</parent>
<groupId>com.samir.enterprise</groupId>
<artifactId>bom-app1</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<name>Enterprise Child BOM</name>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

Compiling classes with dependent jar files in maven

I want to compile some java files which have multiple dependent jars and make a jar file. I have kept all dependent jars under src/main/lib. after running mvn clean install, i get compilation failure of the classes. Dependent jars are not being copied to class path it seems. Anyone can tell whats going wrong her.
<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>test</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<repositories>
<repository>
<id>external-jars</id>
<name>external-jars</name>
<url>file://${project.basedir}\src\main\lib\</url>
</repository>
</repositories>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>jars</groupId>
<artifactId>jars</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib/*</systemPath>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>finaljar</finalName>
<sourceDirectory></sourceDirectory>
</build>
</project>
You construction
<dependency>
<groupId>jars</groupId>
<artifactId>jars</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib/*</systemPath>
</dependency>
does not work. In Maven, each artifact is addressed through groupId, artifactId and version. You cannot add a directory, but only separate jars.
While it is possible to add jars through system paths, it is much better to use a repository. Look at the possibilities in
How to add local jar files to a Maven project?

Maven and dependencies

I have a simply POM like this :
<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.sim</groupId>
<artifactId>log4j</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>com.sim</groupId>
<artifactId>sim-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../sim-java/pom.xml</relativePath>
</parent>
<name>log4j</name>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<scope>compile</scope>
</dependency>
</dependencies>
I run mvn clean package on this project and a JAR is created as expected.
When I navigate into this JAR, I thought that I would see a JAR named log4j-1.2.17.jar inside it but it's not the case.
Using dependency with compile scope does not include JAR into packaging version of project ?
Thank you for clarification
The jar:jar plugin of maven just compiles your source and bundles it into a jar. Just like building a jar out of ant or bare hands, no dependency jars will be bundled in the jar. Jars cannot have dependent jars bundled inside them and even if they did, they cannot be loaded by the default class loader.
If you are looking to build a ejb-jar, then you might want to consider a EJB plugin
Check this link for various plugins that you can exploit.

Resources