Logan Phillips
Logan Phillips

Reputation: 710

Can't use external dependency (installed by Maven) in IntelliJ Ultimate

I created a pom.xml that includes the XML to import JUnit 4. I opened up project in IntelliJ. It seems to have downloaded the external dependency, but when I try to use the dependency in an import statement, IntelliJ can't recognize it.

Here are the steps I took from the beginning:

  1. I created a pom.xml.
  2. mkdir MAVEN
  3. mv pom.xml MAVEN
  4. Open IntelliJ
  5. File > open > pom.xml
  6. cd ~/MAVEN
  7. mkdir -p src/main/java
  8. Go back to IntelliJ
  9. Right click on src -> mark directory as root

I believe that Maven imported everything correctly because I can see JUnit as a dependency in the project directory: https://imgur.com/TdJq7PK

Here is my 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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.company.org</groupId>
    <artifactId>mvn-prac</atifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
</project>

When I attempt to use a common class from JUnit, such as import org.junit.Test;, IntelliJ can't recognize the symbol.

I have attempted both mvn clean and mvn install in the root of the project, and neither seemed to help.

Thanks for any help.

Upvotes: 1

Views: 286

Answers (1)

CrazyCoder
CrazyCoder

Reputation: 401897

If you are using the test scope, JUnit dependency will be available only for the tests located in src/test/java, but not to the classes in src/main/java.

Either remove the <scope>test</scope> line in pom.xml or move your tests to the proper Test Sources root.

Upvotes: 1

Related Questions