Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ Mono repo for all public development

* [Java](./java): Sandbox for all Java public development.
* [Python](./python): Sandbox for all Python public development.
* [Kotlin](./kotlin): Sandbox for all Kotlin public development.
3 changes: 3 additions & 0 deletions kotlin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Kotlin Sandbox

Somewhere to put any Kotlin code I'm playing with.
12 changes: 12 additions & 0 deletions kotlin/gradle-tutorial/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#
# https://help.github.com/articles/dealing-with-line-endings/
#
# Linux start script should use lf
/gradlew text eol=lf

# These are Windows script files and should use crlf
*.bat text eol=crlf

# Binary files should be left untouched
*.jar binary

8 changes: 8 additions & 0 deletions kotlin/gradle-tutorial/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Ignore Gradle project-specific cache directory
.gradle

# Ignore Gradle build output directory
build

# Ignore Kotlin plugin data
.kotlin
121 changes: 121 additions & 0 deletions kotlin/gradle-tutorial/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# Gradle Tutorial

## Links

* [Beginner Tutorial](https://docs.gradle.org/current/userguide/part1_gradle_init.html)

## Notes

* Where does gradle store it's local deps:

```bash
ls -l ~/.gradle/caches/modules-2/files-2.1/
```

### [Beginner Tutorial](https://docs.gradle.org/current/userguide/part1_gradle_init.html)

#### Part 1: Initializing the Project

* Help for init command:

```bash
macbookpro:gradle-tutorial mkwyche$ gradle help --task init
Starting a Gradle Daemon (subsequent builds will be faster)
```

* Commands to generate:

```bash
# Generates the Kotlin Template Project which makes the wrapper etc...
macbookpro:gradle-tutorial mkwyche$ gradle init --type kotlin-application --dsl kotlin
# Post outcome is:
macbookpro:gradle-tutorial mkwyche$ ls -l
total 56
-rw-r--r-- 1 mkwyche staff 287 Mar 24 21:43 _README.md
drwxr-xr-x 4 mkwyche staff 128 Mar 24 21:48 app
drwxr-xr-x 4 mkwyche staff 128 Mar 24 21:48 gradle
-rw-r--r-- 1 mkwyche staff 194 Mar 24 21:48 gradle.properties
-rwxr-xr-x 1 mkwyche staff 8654 Mar 24 21:48 gradlew
-rw-r--r-- 1 mkwyche staff 2896 Mar 24 21:48 gradlew.bat
-rw-r--r-- 1 mkwyche staff 531 Mar 24 21:48 settings.gradle.kts
```

* Gain a better understanding of:
* [settings.gradle.kts](settings.gradle.kts)
* [build.gradle.kts](app/build.gradle.kts)
* [libs.versions.toml](gradle/libs.versions.toml)

#### Part 2: Running Tasks

* View available tasks:

```bash
./gradlew tasks
```

* TODO: Would be cool to see the tasks that are dependent on each task. Look
more into TaskInfo plugin.

#### Part 3: Understanding Dependencies

* Dependencies live in [build.gradle.kts](app/build.gradle.kts) .
* Scopes:
* implementation
* runtimeOnly
* testImplementation
* Dependency Versions live in [libs.versions.toml](gradle/libs.versions.toml)
* By defining them there you can clean up the [build.gradle.kts](app/build.gradle.kts) to not need versions. This is
basically the same concept as parent poms.
* ./gradlew app:dependencies == mvn dependency:tree

#### Part 4: Applying Plugins

* Adding in plugins. For example add this:

```kotlin
// Apply the maven publish plugin
id("maven-publish")
```

To the plugins section. This will add Publishing Tasks to your list of tasks:

```bash
Publishing tasks
----------------
publish - Publishes all publications produced by this project.
publishToMavenLocal - Publishes all Maven publications produced by this project to the local Maven cache.
```

* Publish to maven locally:

```bash
./gradlew :app:publishToMavenLocal
```

* Plugins extend Gradles capabilities by adding tasks, configurations and behavior to your build. They are the
main way to organize and reuse build logic.
* Plugin Types:
* Core plugins: Built into Gradle (e.g. java, application) . See full list of core plugins [here](https://docs.gradle.org/current/userguide/plugin_reference.html)
* Community Plugins: Published by others to the [Gradle Plugin Portal](https://plugins.gradle.org/)
* Custom Plugins: Created by you or your team for internal use.

#### Part 5: Exploring Incremental Builds

* Label Outcomes:
* UP-TO-DATE : Task that has been already executed and hasn't changed.
* SKIPPED : Task was explicitly prevented from running
* FROM-CACHE : Task output has been copied to local directory from previous builds in the build cache (caching feature)
* NO-SOURCE : Task was not executed because it's required inputs we not available.

#### Part 6: Enabling the Gradle Build Cache

* The build cache is here

```bash
ls -l ~/.gradle/caches/build-cache-1/
```
* Remote build cache can likely be used to speed up build.
* Develocity is a tool for that https://gradle.com/develocity/product/build-cache/



110 changes: 110 additions & 0 deletions kotlin/gradle-tutorial/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Kotlin application project to get you started.
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/9.4.1/userguide/building_java_projects.html in the Gradle documentation.
*/

plugins {
// Apply the org.jetbrains.kotlin.jvm Plugin to add support for Kotlin.
alias(libs.plugins.kotlin.jvm)

// Task info dep
// id("org.barfuin.gradle.taskinfo") version "1.2.5" // Use the latest version

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

// Apply the maven publish plugin
id("maven-publish")
}

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

dependencies {
// Use JUnit Jupiter for testing.
testImplementation(libs.junit.jupiter)

testRuntimeOnly("org.junit.platform:junit-platform-launcher")

// This dependency is used by the application.
implementation(libs.guava)
implementation(libs.mapstruct)
}

// Apply a specific Java toolchain to ease working on different environments.
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}

application {
// Define the main class for the application.
mainClass = "org.example.AppKt"
}

tasks.named<Test>("test") {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
}

// Example of registering a custom task...
/*
macbookpro:gradle-tutorial mkwyche$ ./gradlew :app:copyTask
Calculating task graph as no cached configuration is available for tasks: :app:copyTask

BUILD SUCCESSFUL in 1s
Configuration cache entry stored.
*/
tasks.register<Copy>("copyTask") {
from("source")
into("target")
include("*.war")
}

// Understanding Dependencies Between Tasks
/*
macbookpro:gradle-tutorial mkwyche$ ./gradlew :app:greet
Calculating task graph as no cached configuration is available for tasks: :app:greet

> Task :app:hello
Hello!

> Task :app:greet
How are you?

BUILD SUCCESSFUL in 1s
2 actionable tasks: 2 executed
*/
tasks.register("hello") {
doLast {
println("Hello!")
}
}

tasks.register("greet") {
doLast {
println("How are you?")
}
dependsOn("hello")
}

/**
* Publishing configuration
*/
publishing {
publications {
// Adds the information needed by the plugin to actually publish.
create<MavenPublication>("maven") {
groupId = "datadidit.gradle.tutorial"
artifactId = "beginner-tutorial"
version = "1.0-SNAPSHOT"

from(components["java"])
}
}
}
15 changes: 15 additions & 0 deletions kotlin/gradle-tutorial/app/src/main/kotlin/org/example/App.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* This source file was generated by the Gradle 'init' task
*/
package org.example

class App {
val greeting: String
get() {
return "Hello World!"
}
}

fun main() {
println(App().greeting)
}
15 changes: 15 additions & 0 deletions kotlin/gradle-tutorial/app/src/test/kotlin/org/example/AppTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* This source file was generated by the Gradle 'init' task
*/
package org.example

import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.assertNotNull

class AppTest {
@Test
fun appHasAGreeting() {
val classUnderTest = App()
assertNotNull(classUnderTest.greeting, "app should have a greeting")
}
}
6 changes: 6 additions & 0 deletions kotlin/gradle-tutorial/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This file was generated by the Gradle 'init' task.
# https://docs.gradle.org/current/userguide/build_environment.html#sec:gradle_configuration_properties
org.gradle.console=verbose
org.gradle.caching=true
org.gradle.configuration-cache=true

15 changes: 15 additions & 0 deletions kotlin/gradle-tutorial/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# This file was generated by the Gradle 'init' task.
# https://docs.gradle.org/current/userguide/version_catalogs.html#sec::toml-dependencies-format

[versions]
guava = "33.5.0-jre"
junit-jupiter = "6.0.1"
mapstruct = "1.6.3"

[libraries]
guava = { module = "com.google.guava:guava", version.ref = "guava" }
mapstruct = { module = "org.mapstruct:mapstruct", version.ref = "mapstruct" }
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }

[plugins]
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version = "2.3.0" }
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-9.4.1-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading
Loading