IntelliJ not loading transitive dependency in maven project - java

I've made a small library, lets call it lib. It dependends on another library, sublib which is available in Maven central:
lib/pom.xml:
<dependencies>
<dependency>
<groupId>3rdparty</groupId>
<artifactId>sublib</artifactId>
<version>x</version>
</dependency>
</dependencies>
Now I'm trying to use lib in my project proj. I've set it as a dependency:
proj/pom.xml:
<dependencies>
<dependency>
<groupId>mynamespace</groupId>
<artifactId>lib</artifactId>
<version>y</version>
</dependency>
</dependencies>
When I run mvn exec:java -D exec.mainClass=mynamespace.proj.Main the program runs fine.
However if I run it from IntelliJ, I get the following error:
java.lang.NoClassDefFoundError: 3rdparty/SomeSubLibClass
at mynamespace.SomeLibClass.method(SomeLibClass.java:100)
This seems to indicate that IntelliJ does not load the transitive sublib dependency. How can I fix this?

You can manually right click on the pom.xml file in the file tree and select maven > reimport.
Sometimes you'll see a popup saying "Maven projects need to be imported"; you should select Enable Auto-Import.
This option can be found in Preferences > Maven > Importing > [x] Import Maven projects automatically (and is unchecked by default):

What worked for me was changing from using maven (Intellij) version and using my latest version that was installed on my machine previously.

I had a similar problem. The below command resolved the problem. It downloaded all the dependency jars into my IDEA project.
mvn -U idea:idea

Related

No java classes in downloaded Selenium jar

I am trying to upgrade to Selenum 4.0.0-alpha-6 (same issue happens also with version 3.141.59) which seems to be available in Maven repository. I tried first to add this dependency to my pom.xml:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0-alpha-6</version>
</dependency>
The version appears red in IntelliJ which for some reason sees 3.12.0 as the last available version. I know what version IntelliJ sees because I can hit CTRL-space inside the <version> tag.
Running mvn install also complains that the version is not available. So my first question is why do Intellij and Maven not see the latest versions of Selenium?
I reverted to downloading the jar file directly from mvnrepository (that is from here) and adding it to my local repository as follows:
mvn install:install-file -Dfile=selenium-java-4.0.0-alpha-6.jar
-DgroupId=org.seleniumhq.selenium -DartifactId=selenium-java
-Dversion=4.0.0-alpha-6 -Dpackaging=jar -DgeneratePom=true
The jar appears now in my local repo, the Maven dependency does not appear red anymore, but all Selenium-related references in my project appear red. Examining the downloaded jar file reveals that it contains no Java classes:
C:\Development\Java\Selenium>jar tf selenium-java-4.0.0-alpha-6.jar
META-INF/
META-INF/MANIFEST.MF
META-INF/versions/9/
META-INF/versions/9/module-info.class
Any idea what I am missing?
Update: I have added a small project in GitHub that demonstrates the problem. The pom just has the Selenium dependency.
Just running mvn install results in the error below. So this is not just a problem with IntelliJ:
[ERROR] Failed to execute goal on project MySeleniumProject: Could not resolve dependencies for project com.my:MySeleniumProject:jar:1.0-SNAPSHOT: Failed to collect dependencies for [org.seleniumhq.selenium:selenium-java:jar:3.141.59 (compile)]: Failed to read artifact descriptor for org.seleniumhq.selenium:selenium-ja
va:jar:3.141.59: Could not transfer artifact org.seleniumhq.selenium:selenium-java:pom:3.141.59 from/to central (http://repo.maven.apache.org/maven2): Failed to
transfer file: http://repo.maven.apache.org/maven2/org/seleniumhq/selenium/selenium-java/3.141.59/selenium-java-3.141.59.pom. Return code is: 501 , ReasonPhras
e:HTTPS Required.
Try use stable older version
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
Check also, if you have other errors in pom, and have proper java version
I think your project is correct and problem is with maven configuration. Use commandline to see what maven version you have? maven -v. Try use mvn dependency:resolve on the project - from command line
The problem is resolved by upgrading maven. I had Maven 3.0.5, and uprading to 3.6.3 resolved the dependency successfully.

IntelliJ Idea not resolving Mockito and JUnit dependencies with Maven

I am using IntelliJ idea for writing simple unit test cases using JUnit and Mockito. I am using Maven for dependency management.
IntelliJ idea keeps complaining that following imports cannot be resolved:
import org.junit.Test; //Cannot resolve symbol 'Test'
import static org.mockito.Mockito.*; //Cannot resolve symbol 'mockito'
Following is the dependencies section of my project:
<dependencies>
<!-- Dependency for JUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<!--<scope>test</scope>-->
</dependency>
<!-- Dependency for Mockito -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<!--<scope>test</scope>-->
</dependency>
</dependencies>
Following is my project structure:
Try View -> Tool Windows -> Maven projects, then click the blue icon in the top left of the tool window (Reimport all Maven projects). You should be able to find the dependencies in the project view, under external libraries.
If this does not work, there is probably something wrong with your maven config (pom.xml). Try mvn clean install from the command line see if it gives any errors.
My IDE was not resolving JUnit & Mockito dependencies. Tried reimport in IntelliJ and mvn clean install multiple times which didn't help. Reimport worked for me, but with the following steps.
Please be aware that you will lose any run configurations that you have created.
Close IntelliJ
Go to Project Folder and remove .idea folder (rm -rf ./idea)
Import Maven project again (You will need to add back any run configurations that were deleted)
I solved this problem by adding mockito to module I used. Just go to File -> Project structure and in Modules add mockito to your selected module - the one in which you use mockito (usually Test).
You can also use command
mvn -U idea:idea
This command forces a check for missing dependencies and helps in resolving them.
Restartig intellIJ fixed that for me
For me, I deleted org.mockito package from m2 repository and then did mvn clean install.
this reimport the package.
Try using lower versions of junit and mockito.
The latest versions weren't working in my project but it worked once I used lower versions.
Few other things you can do:
After adding the dependency just make sure to update the maven
project. (right-click on project folder -> Maven -> Reload project)
You can also try clicking on the tiny m symbol on the top right to
load maven changes or select maven button to get clean+install
options and click play button.

eclipse project has jar in referenced library not listed as dependency

I have a java project setup in eclipse to build with maven. Project itself is a multi-module maven project (but I am not using m2e plugin rather maven-eclipse plugin, and eclipse project does not have maven nature)
When running mvn install within eclipse, everything compiles fine, but when I run the same command in command prompt, I get compile errors due to a missing dependency.
I see that the jar it's looking for is neither listed explicitly as dependency in pom.xml or is a transitive dependency. I tried running mvn dependency:tree but also couldn't see this jar.
How can this jar be available in eclipse?
Update: two missing jars are
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-jms</artifactId>
</dependency>
<dependency>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</dependency>
I solved the issue. Problem had to do with my invalid global mirror settings that redirected traffic to wrong remote repository so it could not find and install dependencies, later causing compile error.
In eclipse, I was using embedded maven referencing user settings only with no global settings therefore it worked fine.

Force mvn eclipse:eclipse to pick latest jar

I have a maven dependency which requires org\drools\drools-compiler 4.0.3 (drools-compiler 4.0.3 is not mentioned in my pom.xml but gets downloaded as a dependency). Now I am trying to integrate Drools framework with Spring. This requires me to use drools-spring 5.4.0.Final jar. Hence I have added the below pom entry.
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-spring</artifactId>
<version>5.4.0.Final</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>5.4.0.Final</version>
</dependency>
Now when I run a mvn clean install , I see the dependency drools-spring and corresponding dependencies (drools-compiler 5.4,drools core 5.4) getting downloaded, but however after doing mvn eclipse:eclipse, I still dont find the new dependencies showing up under maven dependencies in Eclipse classpath. In fact I still see the old 4.0.3 version under dependencies for drools-compiler and drools-core. I have tried an eclipse restart. Doesnt help either. How do I show the new 5.4 version jar instead of the 4.0.3 ?
As you mentioned you are using eclipse i will suggest to use maven eclipse plugin from eclipse market place.
When you will add maven plugin from eclipse you will able to see a Maven menu item when you will right click on the maven project then inside that menu you will able to see Update Maven project click on that.
If you do not want to do above two steps run the below command with -U.
Check below
mvn eclipse:eclipse -U
OR
mvn install -U
OR
mvn clean -U
See Maven: The Complete Reference, 6.1.11. Downloading and Verifying Dependencies or mvn --help:
-U,--update-snapshots Forces a check for missing
releases and updated snapshots on
remote repositories
Maven command with
I figured out I was adding the pom entries in the wrong pom.xml. Ideally the pom entries should be present in the service impl pom.xml.

Maven not downloading dependencies in Eclipse

I am setting up a project in eclipse . This projects builds successfully through command line(all mvn commands like mvn package, mvn compile, mvn clean install) work perfectly fine. While setting up this project on STS or Eclipse . I see some of the dependencies are not getting downloaded even though they are present in pom.xml. However, searching them in the maven repository and downloading the jar to my local computer and then adding them to build path makes it work on Eclipse.
Is there anything that we need to do to eclipse to make sure it downloads all the dependencies from the repository.
My POM:
<dependency>
<groupId>org.apache.bookkeeper</groupId>
<artifactId>bookkeeper-server-compat410</artifactId>
<version>4.1.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.apache.bookkeeper</groupId>
<artifactId>bookkeeper-server</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.bookkeeper</groupId>
<artifactId>bookkeeper-server-compat420</artifactId>
<version>4.2.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.apache.bookkeeper</groupId>
<artifactId>bookkeeper-server</artifactId>
</exclusion>
</exclusions>
</dependency>
Both these artifacts were not downloaded for eclipse and there jars found http://mvnrepository.com/artifact/org.apache.bookkeeper/bookkeeper-server-compat410/4.1.0 and http://mvnrepository.com/artifact/org.apache.bookkeeper/bookkeeper-server-compat420/4.2.0 were not present in the folder for MavenDependencies and were subsequently giving errors in Eclipse.
However manually adding them to the build path created a new folder (Reference Library) and resolved the Eclipse Errors . Why did Eclipse not download and import these dependencies by themselves from the maven repository ?? Is it a bug in Eclipse or some problem from my side . Please help.
I got the same problem and this is how i solved. :
Right click your project, choose Run As -> Maven install.
Observe the output console to see the installation progress. After
the installation is finished, you can continue to the next step.
Right click your Spring MVC project, choose Maven -> Update Project.
Choose your project and click OK. Wait until update process is
finished.
The error still yet, then do Project->Clean and then be sure you have selected our project directory and then do the follow Project->Build.
Solution 1:
Set correct proxy:
<proxy>
<id>optional</id>
<active>false</active>
<protocol>http</protocol>
<username></username>
<password></password>
<host>172.27.171.91</host>
<port>8080</port>
</proxy>
Solution2 :
just delete
lastupdated extension files from folder
and try updating maven.
[Most of the times this solution will work]
Sometimes there is an error downloading a dependency - eg. some files are downloaded but the actual JAR is missing from the local Maven repository.
In this case I had to delete the whole folder of the problematic dependency in the local maven repository. Only then did Maven update work (Right-click on the project and select Project > Maven > Update Project.... )
In my case, I had unchecked Build automatically. Checking it again started downloading the jars.
I have come across the same issue recently.
First of all you have to configure proxy settings in settings.xml in your maven repository.
If you are using eclipse/STS then please do verify following.
Window -> Preferences -> Maven -> User Settings -> update user settings by pointing your settings.xml
Now it's set to update the maven project. It worked for me.
I was facing similar sort of issue. I tried deleting folders inside .m2 and again building maven project.
I could download all dependency except one dependency which we have created by ourselves and published on Nexus.
Then I changed by java pointing from JRE to JDK which solved my problem
The following worked for me.
Just right-click on Project -> Maven -> Update Project... such as it is shown here.
I had faced a similar issue and following the below steps helped me fix it.
Delete the last modified jar from respective folders.
Select the project
Right Click -> Maven
Update project..
It will download all the missing Jars.
Try to move your dependencies from "type" tag to "scope" tag like below
or
<dependency>
<groupId>net.xyz.xyz</groupId>
<artifactId>xyz-xyz</artifactId>
<version>x.y.z</version>
<type>pom</type>
</dependency>
or
<dependency>
<groupId>net.xyz.xyz</groupId>
<artifactId>xyz-xyz</artifactId>
<version>x.y.z</version>
<scope>test</scope>
</dependency>
then further Maven > Update Project
For me I changed the packaging from pom to jar, and then the dependency got downloaded.
so I changed from <packaging>pom</packaging> to <packaging>jar</packaging>
Make sure you're defining the dependency as close as possible to the leaf of the project tree where it is needed. Otherwise, Maven might ignore it.
For example, if you have a parent project that references projects A and B and the dependency is with respect to project A, then defining the dependency in the parent's pom.xml might get ignored by Maven. So, define it in project A's pom.xml.
Parent Project's pom.xml
Sub-project A's pom.xml <<< define the dependency where it is needed
Sub-project B's pom.xml
I hope this helps someone as it took me 2 days to realize. I re-imported the project multiple times and followed every possible step I've seen online and in the end I had added a small piece of xml code within the pom.xml. Even though it wasn't erroring or even showing a warning it was preventing maven from reading the lifecycle-mappings.
Click into your pom.xml and go to the dependencies tab on the bottom left, if you see an error there it's likely your pom.xml is corrupted in some way and maven will never attempt to download the dependencies even though you won't get any real error. I had looked back at a previous PR and noticed where/what I added and removed it and was able to get maven to work.

Categories