Home > Enterprise >  Adding gradle dependencies
Adding gradle dependencies

Time:11-07

I have a simple gradle 7.2 project, with a simple kotlin file, running java 11.

For my project, I need to add some simple dependencies to java.security such that I'll be able to encrypt and hash some things. So I need to add it as a dependency.

The project is created by running gradle init and picking all the default options.

I then want to be able to do an import like: import java.security.MessageDigest and use the java.security package.

I guess I'll have to add the dependency in the build file, which currently looks like this:

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")
}

I now search google high and low for how a reference to the java.security package can be added in gradle, but find absolutely nothing anywhere.

Following a guide like this it looks like I could just add a in this manner:

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

If what I wanted was a reference to this validation library. But I can never get to test it, because I can't find any info on how I would target java.security anywhere.

Looking at the docsI tried to just grab the names I could find here, but this did not compile:

implementation 'java.security Package'

So yes, how do I get thjis dependency. And in general how do find the names that I need for getting dependencies in general?

CodePudding user response:

You're not finding examples of declaring java.security packages in Gradle because you don't need to declare them; they're included in the JDK so you can import them natively in any class without declaring them in gradle. Try creating this class in any given package within your project and running it. It should succeed.

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");
    }
}

CodePudding user response:

java.security package is part of the Java language itself as you can see from the documentation, for this reason you don't need to include it explicitly it should already be available to you.

Please make sure you have proper Java SDK set up in IDE. Try to configure different distribution/type than you use currently.

Even if you have logic in Kotlin class it should properly resolve an import and compile.

import java.security.MessageDigest

fun main()  {
  val test = MessageDigest.getInstance("SHA-256")
  println("Test Succeeded")
}
  • Related