【问题标题】:"java: package org.junit does not exist" even though it's in Maven pom“java:包 org.junit 不存在”,即使它在 Maven pom 中
【发布时间】:2015-09-25 23:34:09
【问题描述】:

我在我的项目中得到“java:package org.junit 不存在”,这让我发疯。我将 JUnit 作为外部库,但它仍然没有意识到这一点。我正在尝试在 IntelliJ Idea 中设置一个简单的“Hello World”应用程序。

这是我的 pom:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.springapp</groupId>
    <artifactId>untitled</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>untitled</name>

    <properties>
        <spring.version>4.1.1.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
            <scope>test</scope>
        </dependency>

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

    <build>
        <finalName>untitled</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <includes>
                        <include>**/*Tests.java</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

更新:我能够在我的另一台计算机上运行相同的项目。这很奇怪。

【问题讨论】:

  • 删除你的 m2 repo,并发布mvn -U clean install的结果。
  • @Tunaki 我正在使用 intellij idea 的内置 maven。这样做适用于我的另一台 Mac。
  • 我建议您删除本地 m2 存储库,因为它可能已损坏。这意味着删除目录 ~/.m2/repository (这是默认的)

标签: maven junit configuration dependencies


【解决方案1】:

你可以试试这个:

mvn idea:idea

它将生成您的 IntelliJ IDEA 项目所需的文件。

【讨论】:

  • 我这样做了,所有必要的文件都生成了。每当我尝试在我的 Intellij IDEA 项目中编译它时,构建的错误仍然显示 pack org.unit 不存在。该库位于我的外部库文件夹中。
  • 您是否尝试过在命令行中编译您的项目?如果它不起作用,我猜这是由于本地 Maven 存储库中的 jar 损坏。在某些情况下,删除 .m2 中的所有依赖项是不够的。我遇到了一个非常相似的问题。这直接是由于 Maven 版本。升级Maven和mvn clean install -U后就ok了。
  • 只需删除 .m2 目录即可为我修复它。谢谢!
【解决方案2】:

只需删除 .m2 目录即可为我修复它。

klamse 2015 年 9 月 30 日 1:41

我们应该把它放在答案列表中。这真的很有帮助!谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    • 1970-01-01
    • 2015-06-27
    • 2019-02-28
    • 2018-02-15
    • 2017-11-01
    相关资源
    最近更新 更多