Set up a Gradle project that uses the Amazon SDK for Java 2.x
You can use Gradle
The initial steps in the following example come from Gradle's Getting Started guide
To create a Java application with Gradle (command line)
-
Create a directory to hold your project. In this example,
demois the directory name. -
Inside the
demodirectory, execute thegradle initcommand and supply the values highlighted in red as shown in the following command line output. For the walk through, we choose Kotlin as the build script DSL language, but a complete example for Groovy is also shown at the end of this topic.> gradle init Starting a Gradle Daemon (subsequent builds will be faster) Select type of project to generate: 1: basic 2: application 3: library 4: Gradle plugin Enter selection (default: basic) [1..4]2Select implementation language: 1: C++ 2: Groovy 3: Java 4: Kotlin 5: Scala 6: Swift Enter selection (default: Java) [1..6]3Generate multiple subprojects for application? (default: no) [yes, no]noSelect build script DSL: 1: Kotlin 2: Groovy Enter selection (default: Kotlin) [1..2]<Enter>Select test framework: 1: JUnit 4 2: TestNG 3: Spock 4: JUnit Jupiter Enter selection (default: JUnit Jupiter) [1..4]4Project name (default: demo):<Enter>Source package (default: demo):<Enter>Enter target version of Java (min. 7) (default: 11):<Enter>Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no]<Enter>> Task :init To learn more about Gradle by exploring our Samples at https://docs.gradle.org/8.4/samples/sample_building_java_applications.html BUILD SUCCESSFUL in 3m 43s 2 actionable tasks: 2 executed -
After the
inittask completes, thedemodirectory contains the following tree structure. We take a closer look at the main build file,build.gradle.kts(highlighted in red), in the next section.├── app │ ├──build.gradle.kts│ └── src │ ├── main │ │ ├── java │ │ │ └── demo │ │ │ └── App.java │ │ └── resources │ └── test │ ├── java │ │ └── demo │ │ └── AppTest.java │ └── resources ├── gradle │ └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle.ktsThe
build.gradle.ktsfile contains the following scaffolded content./* * This file was generated by the Gradle 'init' task. * * This generated file contains a sample Java application project to get you started. * For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.4/userguide/building_java_projects.html in the Gradle documentation. */ plugins { // Apply the application plugin to add support for building a CLI application in Java. application } repositories { // Use Maven Central for resolving dependencies. mavenCentral() } dependencies { // Use JUnit Jupiter for testing. testImplementation("org.junit.jupiter:junit-jupiter:5.9.3") testRuntimeOnly("org.junit.platform:junit-platform-launcher") // This dependency is used by the application. implementation("com.google.guava:guava:33.3.0-jre") } // Apply a specific Java toolchain to ease working on different environments. java { toolchain { languageVersion.set(JavaLanguageVersion.of(11)) } } application { // Define the main class for the application. mainClass.set("demo.App") } tasks.named<Test>("test") { // Use JUnit Platform for unit tests. useJUnitPlatform() } -
Use the scaffolded Gradle build file as the basis for your Amazon project.
-
To manage SDK dependencies for your Gradle project, add the Maven bill of materials (BOM) for the Amazon SDK for Java 2.x to the
dependenciessection of thebuild.gradle.ktsfile.... dependencies { implementation(platform("software.amazon.awssdk:bom:2.27.21")) // With the bom declared, you specify individual SDK dependencies without a version. ... } ...Note
In this example build file, replace 2.27.21 with the latest version of the SDK for Java 2.x. Find the latest version available in Maven central repository
. -
Specify the SDK modules your application needs in the
dependenciessection. As an example, the following adds a dependency on Amazon Simple Storage Service.... dependencies { implementation(platform("software.amazon.awssdk:bom:2.27.21")) implementation("software.amazon.awssdk:s3") ... } ...Gradle automatically resolves the correct version of declared dependencies by using the information from the BOM.
-
The following examples show complete Gradle build files in both the Kotlin and Groovy DSLs. The build file contains dependencies for Amazon S3, authentication, logging, and testing. The source and target version of Java is version 11.
For next steps, see the Getting Started guide on the Gradle website for instructions on
how to build and run a Gradle application