简体   繁体   English

Gradle 将库添加到类路径

[英]Gradle add library to classpath

I am using Gradle for a project.我正在为一个项目使用 Gradle。 My build.gradle has got JUnit as a dependency, but every time I import the project again it does not recognize the JUnit library and asks me to add it to classpath.我的 build.gradle 已将 JUnit 作为依赖项,但每次我再次导入项目时,它都无法识别 JUnit 库并要求我将其添加到类路径中。 This problem does not only apply to JUnit, but also to other dependencies.这个问题不仅适用于 JUnit,也适用于其他依赖项。 Did I add JUnit to the wrong place?我是否将 JUnit 添加到错误的位置?

PS: The project builds just fine, it's just the IDE (IntelliJ) that's marking everything red. PS:该项目构建得很好,只是 IDE (IntelliJ) 将所有内容标记为红色。

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

错误截图

The dependency declaration is correct.依赖声明是正确的。 Probably you're opening the project in IntelliJ IDEA the wrong way.可能您以错误的方式在 IntelliJ IDEA 中打开项目。

I suggest you to open a Gradle project this way:我建议你这样打开一个 Gradle 项目:

  1. Click the File | Open单击File | Open File | Open menu File | Open菜单
  2. Select the build.gradle file选择build.gradle文件

Here is further information about importing Gradle projects (see also the side note in that page).以下是有关导入 Gradle 项目的更多信息(另请参阅该页面中的旁注)。

download the external jar file from mvn repository and add it to libs folder.从 mvn 存储库下载外部 jar 文件并将其添加到 libs 文件夹。

  1. Gradle-project(your project)->libs(inside libs folder paste jar file). Gradle-project(您的项目)-> libs(在 libs 文件夹中粘贴 jar 文件)。 if you don't have libs folder just create it.如果您没有 libs 文件夹,只需创建它。

  2. go to build.gradle dependencies { compile files('libs/your_jar.jar') }转到 build.gradle 依赖项 { compile files('libs/your_jar.jar') }

  3. reload the gradle project.重新加载gradle项目。

it's worked for me.它对我有用。 i hope it's work for you guys also...我希望这对你们也有用......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM