简体   繁体   English

如何在 java gradle 项目中包含对库的引用

[英]How to include reference to library in java gradle project

I am coming from a C# background.我来自 C# 背景。 I am used to NuGet and Visual Studio project references so the Java ecosystem has confused me quite a bit.我习惯了 NuGet 和 Visual Studio 项目参考,所以 Java 生态系统让我很困惑。

I have a gradle library project.我有一个 gradle 库项目。 I want to import org.apache.commons.codec.binary.Base64;我要import org.apache.commons.codec.binary.Base64;

However I keep getting cannot resolve errors.但是我不断收到无法解决的错误。 I am using VSCode as my IDE and I would like to include the codec dependancy.我使用 VSCode 作为我的 IDE 并且我想包括编解码器的依赖关系。 How would I achieve this in VSCode/gradle.我将如何在 VSCode/gradle 中实现这一点。

I have downloaded the commons-codec-1.14.jar file, but don't know where to put it in the project.我已经下载了commons-codec-1.14.jar文件,但是不知道放在项目的哪个位置。

Gradle is a tool that, among other things, manages your dependencies. Gradle 是一个工具,除其他外,它可以管理您的依赖关系。 This means that, you do not need to manually download and add dependencies to your project.这意味着,您无需手动下载依赖项并将其添加到项目中。 Gradle solves this for you. Gradle 为您解决这个问题。

See the official documenation on how to handle dependencies with Gradle.请参阅有关如何使用 Gradle 处理依赖关系的官方文档。

You probably have a build.gradle file, in which you need to include your dependency.您可能有一个build.gradle文件,您需要在其中包含您的依赖项。 It would look something like:它看起来像:

dependencies {
  implementation 'commons-codec:commons-codec:1.14'
}

This lets Gradle know that you have a dependency to version 1.14 of commons-codec which your codes need to build and run.这让 Gradle 知道您依赖于1.14版的commons-codec ,您的代码需要构建和运行。

This will automatically be downloaded from a remote repository, which you also can specify in your build.gradle file:这将自动从远程存储库下载,您也可以在build.gradle文件中指定:

repositories {
  mavenCentral()
}

This tells gradle to download the dependencies from Maven Central, which probably is the most typical Maven/Gradle repository and most likely hosts most dependencies you would need.这告诉 gradle 从 Maven Central 下载依赖项,这可能是最典型的 Maven/Gradle 存储库,并且很可能托管您需要的大多数依赖项。

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

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