首页 > 解决方案 > 添加 gradle 依赖项

问题描述

我有一个简单的 gradle 7.2 项目,带有一个简单的 kotlin 文件,运行 java 11,在 ubuntu 20.04 上的 vs 代码

对于我的项目,我需要添加一些简单的依赖项,java.security以便能够加密和散列一些东西。所以我需要将它添加为依赖项。

该项目是通过运行gradle init并选择所有默认选项来创建的。

然后我希望能够进行如下导入:import java.security.MessageDigest并使用该java.security包。

我想我必须在构建文件中添加依赖项,目前看起来像这样:

plugins {
    // Apply the org.jetbrains.kotlin.jvm Plugin to add support for Kotlin.
    id("org.jetbrains.kotlin.jvm") version "1.5.0"

    // Apply the application plugin to add support for building a CLI application in Java.
    application
}

repositories {
    // Use Maven Central for resolving dependencies.
    mavenCentral()
}

dependencies {
    // Align versions of all Kotlin components
    implementation(platform("org.jetbrains.kotlin:kotlin-bom"))

    // Use the Kotlin JDK 8 standard library.
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

    // This dependency is used by the application.
    implementation("com.google.guava:guava:30.1.1-jre")

    // Use the Kotlin test library.
    testImplementation("org.jetbrains.kotlin:kotlin-test")

    // Use the Kotlin JUnit integration.
    testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
}

application {
    // Define the main class for the application.
    mainClass.set("com.what.isthis.AppKt")
}

我现在在谷歌上下搜索如何在 gradle 中添加对 java.security 包的引用,但在任何地方都找不到。

按照这样的指南,看起来我可以以这种方式添加

implementation 'org.springframework.boot:spring-boot-starter-validation:2.4.0'

如果我想要的是对这个验证库的引用。但我永远无法测试它,因为我找不到任何关于如何在java.security任何地方定位的信息。

查看 文档,我试图抓住我可以在这里找到的名字,但这没有编译:

implementation 'java.security Package'

所以是的,我如何获得依赖。一般来说,如何找到获取依赖项所需的名称?

标签: gradle

解决方案


您没有java.security在 Gradle 中找到声明包的示例,因为您不需要声明它们;它们包含在 JDK 中,因此您可以在任何类中本地导入它们,而无需在 gradle 中声明它们。尝试在项目中的任何给定包中创建此类并运行它。它应该成功。

import java.security.MessageDigest;

public class Test {
    public static void main(String[] args) throws Exception {
        MessageDigest test = MessageDigest.getInstance("SHA-256");
        System.out.println("Test Succeeded");
    }
}

推荐阅读