How to avoid telling the version in dependency with maven? - java

I have a project that has 2 modules, the first two work without version, but when I added a new one is asking me for the version.
Why is letting me use the first two without version and is asking for a version in the third one?
Maven project to create ear
....
<parent>
<groupId>es.imas.gestresi</groupId>
<artifactId>gestresi</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
...
<dependencies>
<dependency>
<groupId>es.imas.gestresi</groupId>
<artifactId>gestresi-web</artifactId>
<type>war</type>
</dependency>
<dependency>
<groupId>es.imas.gestresi</groupId>
<artifactId>gestresi-ejb</artifactId>
<type>ejb</type>
</dependency>
<dependency>
<groupId>es.imas.gestresi</groupId>
<artifactId>test-dialog</artifactId>
<version>0.0.1-SNAPSHOT</version> <!--Ask for version-->
<type>war</type>
</dependency>
</dependencies>

Related

maven dependency management mechanism, the priority about dependency management which using import scope

There are two examples.
example 1.
pom of project A is a simple pom:
<groupId>org.demo</groupId>
<artifactId>kafka-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<name>kafka-demo</name>
pom of project B:
<parent>
<artifactId>kafka-demo</artifactId>
<groupId>org.demo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>child-one</artifactId>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
In this example, spring-boot version of project B is 2.0.3.RELEASE.
example 2.
pom of project A.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/>
</parent>
<groupId>org.demo</groupId>
<artifactId>kafka-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<name>kafka-demo</name>
pom of project B is the same as the previous example.
<parent>
<artifactId>kafka-demo</artifactId>
<groupId>org.demo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>child-one</artifactId>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
In the example two, spring-boot version of project B is 2.3.1.RELEASE.
My question is in example 2, why the version of spring-boot is 2.3.1, rather than 2.0.3.
This scope is only supported on a dependency of type pom in the section. It indicates the dependency to be replaced with the effective list of dependencies in the specified POM's section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.
AFAIK, dependencyManagement that is directly declared in the POM is always stronger than imported dependencyManagement.
It is wrong to assume that an import is the same as declaring the list from the BOM at the same place.
So in your second example, one of the BOMs is used as parent, so it is directly used in the resulting effective POM, while the other is an import, which is less strong.

Setting maven to have a framework and other projects that use it

I created a Maven project that i should use as a framework. This framework has some dependencies:
<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>SeleniumJavaFramework</groupId>
<artifactId>SeleniumJavaFramework</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>7</maven.compiler.source>
<maven.compiler.target>7</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0-alpha-6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.1.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.aventstack/extentreports -->
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>4.1.6</version>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
</dependencies>
</project>
I'd like to create other Maven projects, separated from the framework project, that will have their own pom.xml with the framework dependency.
If possible, they should inherit dependencies from the framework project.
<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>RicercaGoogle</groupId>
<artifactId>RicercaGoogle</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>TestOne</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>7</maven.compiler.source>
<maven.compiler.target>7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>SeleniumJavaFramework</groupId>
<artifactId>SeleniumJavaFramework</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>system</scope>
<systemPath>patofmyjar.jar</systemPath>
</dependency>
</dependencies>
</project>
Unfortunately I don't know where to start with the maven settings. I'm not even sure if the pom files are correct. I only know that i can't simply put the jar dependency into the test project and run it. Can you help me out?
Thank you
If you are working at single project, you can create a parent pom that include your TestOne app and Framework as two modules, so you'll have 3 poms (search for maven multimodule project). If your framework is a library for different projects, you need a repository (ie nexus), where your Framework can be deployed to. Then you can use it as a dependency in other projects (dependencies of Framework will be included automatically)
I suggest to follow the maven official guide for learning how inheritance works in maven and adapt this to your logic:
http://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Project_Inheritance
Problem was that my classes where in the wrong folders.
They were in src/test/java while they had to be in src/main/java.
Also in the test project pom i removed scope and systemPath as suggested by Andrew Fomin
<dependency>
<groupId>SeleniumJavaFramework</groupId>
<artifactId>SeleniumJavaFramework</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
I had an error
Missing artifact SeleniumJavaFramework:SeleniumJavaFramework:jar:0.1.1-SNAPSHOT
because, as you can see, version was wrong.
Thank you all

maven dependency hell: java.lang.NoSuchMethodError

I am trying to convert one of our existing maven projects to spring boot. I dont have any compile errors in my project but when I try to run it I get the following error:
Exception in thread "main" java.lang.NoSuchMethodError: org.springframework.util.MultiValueMap.addAll(Ljava/lang/Object;Ljava/util/List;)V
at org.springframework.core.io.support.SpringFactoriesLoader.loadSpringFactories(SpringFactoriesLoader.java:140)
at org.springframework.core.io.support.SpringFactoriesLoader.loadFactoryNames(SpringFactoriesLoader.java:119)
at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:426)
at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:418)
at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:266)
at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:247)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243)
at com.myorg.MyMainClass ...
My POM:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>couple of our other internal projects</groupId>
**One of these projects is using spring-context and spring-orm** dependencies.
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Since the spring dependencies in my own project are declared before the other projects I would have thought it would override any of those transitive dependencies.
I have wasted an entire day on this. Can some one please help?
Since the spring dependencies in my own project are declared before the other projects I would have thought it would override any of those transitive dependencies.
That is in fact the issue. Your project is dictating what is included, but some of the dependencies require a different version. They are still trying to call the method that exists in their version, but not in yours, and can't find it because you've forcibly excluded their version. That should not be done unless you're confident that the two versions are compatible!
Solutions:
Align on a single version, and use that
Use more compatible versions
Include both versions via shading (see this post) or a similar mechanism

NoSuchMethodError: org.apache.log4j.ConsoleAppender.<init>(Lorg/apache/log4j/Layout;)

I have a maven project with jdk: 1.8, spring-boot: 1.5.4.RELEASE, spring: 4.3.9.RELEASE, and some vendor dependencies. The project builds and runs but when I access resources used by vendor packages, I get following exception:
java.lang.NoSuchMethodError: org.apache.log4j.ConsoleAppender.<init>(Lorg/apache/log4j/Layout;)V
at bkLogPkg.SingletonLog.<init>(SingletonLog.java:19) ~[VendorComLib.jar:na]
at Vendor.ConnectionBasket.<init>(ConnectionBasket.java:31) ~[VendorServices.jar:na]
at Vendor.ConnectionBasketInterface.Loader(ConnectionBasketInterface.java:450) ~[VendorServices.jar:na]
at Vendor.ConnectionBasketInterface.<init>(ConnectionBasketInterface.java:251) ~[VendorServices.jar:na]
...
It's trying to call method from Vendor jar file to log4j 1.2.14.jar file, but it's unable to do it. I have added vendor dependencies in pom.xml file.
pom.xml:
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
<groupId>com.sample</groupId>
<artifactId>angular2-spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>angular2-spring</name>
<description>Angular 2 application with Spring 4</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<org.slf4j-version>1.2.14</org.slf4j-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- Vendor dependencies from Local Repository -->
<dependency>
<groupId>Vendor</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<dependency>
<groupId>Vendor</groupId>
<artifactId>VendorServiceAuth</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>Vendor</groupId>
<artifactId>VendorServices</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>Vendor</groupId>
<artifactId>VendorComLib</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>Vendor</groupId>
<artifactId>VendorHeaderClass</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
I have the same issue today. It cost me several hours to find the reason.
You just have a conflict with your log4j jar.
The quick fix for this is :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
If you really need log4j, then add the dependency:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
You can check log4j-over-slf4j to know the detail.
I have encountered similar issues and all of mine were solved by making sure the version I was using was the most updated and that is is compatible with the rest of the code. With your problem, it looks as if the .jar is specifically built to be compatible so I doubt that's the issue. Only things I can think of given the information at hand is:
1.) Try making sure all .jar's being used are newest version. (Sometimes if your other .jar's are not completely updated, they can throw errors/exceptions when trying to access a method from a .jar that is a version ahead.)
2.) If "1" doesn't do it, try using a dependency import function in an IDE. I have had many occurrences where Maven was just causing odd errors from IDE compatibility. Importing all .jar's from a "Dependency" menu could also help.

Maven module using spring-boot

I like to configure my applications in maven by creating modules like;
<groupId>com.app</groupId>
<artifactId>example-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>app-api</module>
<module>app-impl</module>
<module>app-web</module>
</modules>
The modules then use the 'example-app' as the parent.
Now I want use 'spring-boot' for my web application.
Is there a way to configure maven so that my 'app-web' is a spring-boot application?
The problem I'm facing is that you have to use spring-boot as a parent.
You don't have to use the spring-boot-starter-parent, it's just a way to get started quickly. All it provides are dependency management and plugin management. You can do both yourself, and you can use the spring-boot-dependencies (or equivalently the parent) to manage dependencies if you want a halfway step. To do that, use scope=import like this
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<type>pom</type>
<version>1.0.2.RELEASE</version>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Another alternative, is to include in the parent pom, the parent declaration for spring boot, as shown in this post
example-app pom.xml:
<project>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
// rest of the example-app pom declarations
</project>
After that, in the modules poms (app-web, app-impl, etc.), you declare example-app as parent, but now you can include the starter dependencies as you would normally do in a regular project.
app-web pom.xml:
<project>
<parent>
<groupId>org.demo</groupId>
<artifactId>example-app</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<name>app-web</name>
<artifactId>app-web</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.demo</groupId>
<artifactId>app-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.demo</groupId>
<artifactId>app-impl</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
// rest of the app-web pom declarations
</project>
Regarding version management, what i used in these examples aren't exactly the best practices, but since is out of the scope of the question i skipped dependencyManagement and parent properties usage.
Also, if there is a starter that is used in every module, you can declare the dependency in the parent pom and then all the modules will inherit it (for example spring-boot-starter-test)

Categories