Maven: test dependencies - java

Problem: Maven can't find some dependencies inside my tests
import org.hamcrest.core.StringStartsWith; // HIGHLIGHTED AS RED IN INTELLIJ
It's imported like this in my pom.xml:
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
I already tried:
mvn test-compile
Invalidate Intellij caches
Restart Intellij
Clear .m2 and rebuild project

The way to figure this out is to run your tests from the command line, via mvn verify, and ensure that things compile and run correctly. This will allow you to determine whether the problem is in your POM configuration, or something unique to Intellij.
If it's a problem in Intellij, it is very likely to be something around folder configuration: generally only folders marked as "Test Source Root" will have access to things in the test scope.

Ensure that the dependency is at least specified under <project><dependencies> on not only under <project><dependencyManagement><dependencies>.

Related

JUnit dependency not available in Maven project

I have already rebuilt the Maven repository, but my Maven still does not see JUnit:
I use Eclipse IDE,
Tried reinstalling the library also manually installed maven,
Replaced part of the code in pom, does anyone know how to solve the problem
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
The log says Compiling 2 source files to C:\workspace\Reverse2\target\classes instead of ...target\test-classes which indicates that src/test is handled as main code, not as test code. In main code, dependencies with the scope test are not available, hence the compile error.
In Maven test code is by default in src/test/java, not in src/test.

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.

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.

IntelliJ not loading transitive dependency in maven project

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

Categories