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.
Related
Below is the pom.xml content. I generated the project with maven archetype quickstart with junit. I encounter two problems:
When trying to run tests from Intellij, I get "no Groovy library defined", although I have nothing to do with Groovy in this project.
When doing mvn test from command line, no tests are identified.
To mention, the default java version is 8 on my mac, so when running mvn i export JAVA_HOME to 12, maybe it's related to the problem nr.1, not sure.
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.pcollections</groupId>
<artifactId>pcollections</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
When trying to run tests from Intellij, I get "no Groovy library defined", although I have nothing to do with Groovy in this project.
This sound like a bug, since both pcollections and junit are not related to groovy:
Try to re-import your project (maven tab + reimport). If this doesn't help, close the project, and open up again the pom.xml it will offer to re-create the project and discard old data, agree to this and it will rebuild everything.
When doing mvn test from command line, no tests are identified.
Probably the tests that you've developed do not follow the surefire convention for classes resolution (It should end with *Test although if I remember correctly it has changed slightly in the latest versions of surefire, so now, also *Tests is ok). Please add the maven command line output when it runs the "test" phase as well as one of your test definitions (even with a trivial test code).
Similar to some other Questions, I find IntelliJ mysteriously refuses to recognize AssertJ library. I am asking again as (a) I have tried the various suggestions, and (b) I have a very simple example anyone can try themselves.
In IntelliJ 2018 and IntelliJ 2019 pre-release, I create a new project using the Maven archetype maven-archetype-quickstart version 1.4.
AssertJ 3 requires Java 8. So I changed these two lines in the POM for 1.7 to 11.
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
I add this to the POM:
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.11.1</version>
<scope>test</scope>
</dependency>
Using the Maven panel in IntelliJ, I executed a clean and install.
Seems good. I verify the org.assertj:assertj-core:3.11.1 library appears in the Project panel of IntelliJ. The app runs, with Hello World appearing on the console in IntelliJ.
In the App.java file, I add this import statement.
import static org.assertj.core.api.Assertions.* ;
Error reported in the IDE editor:
Cannot resolve symbol 'Assertions'
Some people suggest a corrupted Maven cache. So I quit IntelliJ, and I delete the .m2 folder in my home folder. I re-open my project in IntelliJ, and re-execute the Maven clean & install. Many things are downloading, so I know the Maven cache is indeed being recreated.
Yet, still the error in my editor, Cannot resolve symbol 'Assertions'.
No Java Modules involved, as the quickstart archetype has not yet been updated for that.
Delete <scope>test</scope>
This topic was addressed in a closed ticket # 520 on the AssertJ issue tracker.
When a Maven dependency carries a scope element with a value of test, that means you cannot use that library outside of your test-specific source package/folder.
If you are trying to call AssertJ from code in your example project’s src/main/java/… folder hierarchy, you will see that error. If you call AssertJ from src/test/java…, you will see success.
To enable AssertJ in the src/main/java/… folder hierarchy, delete the scope element in your POM dependency. So this:
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.11.1</version>
<scope>test</scope>
</dependency>
…becomes this:
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.11.1</version>
</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.
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>.
I'm working on a project that builds and deploys fine. I'm trying to add some code that uses JWebUnit, and use the following Maven code to bring it in:
<dependency>
<groupId>net.sourceforge.jwebunit</groupId>
<artifactId>jwebunit-htmlunit-plugin</artifactId>
<version>3.2</version>
<scope>test</scope>
</dependency>
Maven seems to resolve this fine and it's bringing everything in (I'm using Intellij, and it now appears under 'Dependencies' in the 'Maven Projects' tab, and also under 'External Libraries' in the Project tab).
However, when I bring this dependency in, the IDE is not able to find it (e.g. if I use import net.sourceforge.jwebunit.junit.WebTester, it can't find it).
But an even bigger issue is it actually breaks some existing code -- I have some JUnit tests that use org.apache.commons.httpclient.HttpClient, and now on Maven's install goal I get a
NoClassDefFoundError - Could not initialize class for that class.
If I remove the JWebUnit dependency, the Maven install goal exits successfully.
I'm used to seeing errors about dependency version convergence when bringing new dependencies, and I feel like chasing this 'no class def found' error could be a red herring, but I'm not sure of the general types of issues in Maven that could be causing it.
EDIT: the dependency code for pulling in HttpClient is:
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
jwebunit-htmlunit-plugin includes transitive dependencies which seem like they're conflicting with some of your dependencies (likely because they are different versions).
Maven puts classpath priority on artifacts declared earlier. Try moving jwebunit to the end of your dependencies section, or at least after where you pull in the httpclient classes. Alternatively, you can manually exclude certain transitive dependencies from being pulled in, but this can be tedious.
As for your IDE not allowing imports on the library, remember that you have this declared in the test scope. Production classes cannot see test dependencies.