Skip to content

Commit

Permalink
Add Gradle toolchain setup. (#1294)
Browse files Browse the repository at this point in the history
* Add JVM toolchain setup.

Configure project to use JDK 1.8 toolchain to compile itself and run the tests.

* Add separate task to run tests on JDK 11.

This task utilizes toolchain feature. CI was updated to run only on agents with JDK 1.8 preinstalled. JDK 11 will be installed by Gradle.

* Update Dokka to 1.6.0 release.
  • Loading branch information
Tapchicoma authored Dec 1, 2021
1 parent 161925b commit 1e7cf75
Show file tree
Hide file tree
Showing 23 changed files with 103 additions and 73 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/gradle-pr-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
java: [ 8, 11 ]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- name: Set up Java ${{ matrix.java }}
- name: Set up Java 8
uses: actions/setup-java@v2
with:
distribution: 'zulu'
java-version: ${{ matrix.java }}
java-version: 8
- name: Restore Gradle caches
uses: actions/cache@v2
with:
path: |
~/.gradle/caches/modules-2
~/.gradle/caches/files-2.1
~/.gradle/caches/metadata-2.96
~/.gradle/jdks
key: ${{ runner.os }}-gradle-cache-${{ hashFiles('**/*.gradle.kts') }}-${{ hashFiles('**/*.gradle') }}-${{ hashFiles('**/gradle.properties') }}
restore-keys: |
${{ runner.os }}-gradle-cache-
Expand Down
8 changes: 3 additions & 5 deletions .github/workflows/gradle-snapshot-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,21 @@ on:
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
java: [ 8 ]
steps:
- uses: actions/checkout@v2
- name: Set up Java ${{ matrix.java }}
- name: Set up Java 8
uses: actions/setup-java@v2
with:
distribution: 'zulu'
java-version: ${{ matrix.java }}
java-version: 8
- name: Restore Gradle caches
uses: actions/cache@v2
with:
path: |
~/.gradle/caches/modules-2
~/.gradle/caches/files-2.1
~/.gradle/caches/metadata-2.96
~/.gradle/jdks
key: ${{ runner.os }}-gradle-cache-${{ hashFiles('**/*.gradle.kts') }}-${{ hashFiles('**/*.gradle') }}-${{ hashFiles('**/gradle.properties') }}
restore-keys: |
${{ runner.os }}-gradle-cache-
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ This project adheres to [Semantic Versioning](https://semver.org/).
## Unreleased

### Added
- Use Gradle JVM toolchain with language version 8 to compile the project

### Fixed
- KtLint CLI 0.43 doesn't work with JDK 1.8 ([#1271](https://github.com/pinterest/ktlint/issues/1271))
- Fix false positive in rule spacing-between-declarations-with-annotations ([#1281] (https://github.com/pinterest/ktlint/issues/1281))
-
### Changed
- Update Kotlin version to `1.6.0` release
- Add separate tasks to run tests on JDK 11 - "testOnJdk11"
- Update Dokka to `1.6.0` release

### Removed

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ You may also pass a list of disabled rules via the `--disabled_rules` command li
```sh
git clone https://github.com/pinterest/ktlint && cd ktlint
./mvnw # shows how to build, test, run, etc. project
./gradlew tasks # shows how to build, test, run, etc. project
```

> To open ktlint in Intellij IDEA:
Expand Down
44 changes: 6 additions & 38 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ plugins {
}

ext.versions = [
'kotlin': gradle.ext.isKotlinDev ? "1.6.0" : "1.6.0",
'gradle': '7.2',
'gradle-sha256': 'f581709a9c35e9cb92e16f585d2c4bc99b2b1a5f85d2badbd3dc6bff59e1e6dd'
]

ext.deps = [
'kotlin' : [
'compiler': "org.jetbrains.kotlin:kotlin-compiler-embeddable:${versions.kotlin}"
'compiler': "org.jetbrains.kotlin:kotlin-compiler-embeddable"
],
'klob' : 'com.github.shyiko.klob:klob:0.2.1',
ec4j : 'org.ec4j.core:ec4j-core:0.3.0',
Expand All @@ -23,7 +22,11 @@ ext.deps = [
'jimfs' : 'com.google.jimfs:jimfs:1.1'
]

if (gradle.ext.isKotlinDev) {
repositories {
mavenCentral()
}

if (project.hasProperty('isKotlinDev')) {
allprojects { p ->
String definedVersion = p.ext."VERSION_NAME".minus("-SNAPSHOT")
p.ext."VERSION_NAME" = "$definedVersion-kotlin-dev-SNAPSHOT".toString()
Expand All @@ -45,41 +48,6 @@ task ktlint(type: JavaExec, group: LifecycleBasePlugin.VERIFICATION_GROUP) {
args '**/src/**/*.kt', '--baseline=ktlint-baseline.xml', '--verbose'
}

allprojects {
repositories {
mavenCentral()
gradlePluginPortal()
}

tasks.withType(JavaCompile).configureEach {
it.sourceCompatibility = JavaVersion.VERSION_1_8
it.targetCompatibility = JavaVersion.VERSION_1_8
}

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
it.sourceCompatibility = JavaVersion.VERSION_1_8
it.targetCompatibility = JavaVersion.VERSION_1_8

it.kotlinOptions {
jvmTarget = '1.8'
apiVersion = '1.4'

freeCompilerArgs += ["-Xopt-in=kotlin.RequiresOptIn"]
}
}
}

subprojects { subProject ->
// Do not enabling explicit api for cli project
if (subProject.name != "ktlint") {
subProject.plugins.withId("org.jetbrains.kotlin.jvm") {
subProject.extensions.configure("kotlin") { ext ->
ext.explicitApi = 'warning'
}
}
}
}

/**
* Configures "wrapper" task to use specific Gradle version and distribution type.
*/
Expand Down
18 changes: 16 additions & 2 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,25 @@ plugins {
`kotlin-dsl`
}

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(8))
}
}

repositories {
mavenCentral()
gradlePluginPortal()
}

// Pass '-PkotlinDev' to command line to enable kotlin-in-development version
val kotlinVersion = if (project.hasProperty("kotlinDev")) {
logger.warn("Enabling kotlin dev version!")
"1.6.0"
} else {
"1.6.0"
}

dependencies {
implementation("org.jetbrains.dokka:dokka-gradle-plugin:1.4.32")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
implementation("org.jetbrains.dokka:dokka-gradle-plugin:1.6.0")
}
21 changes: 21 additions & 0 deletions buildSrc/src/main/kotlin/ToolchainForTests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import gradle.kotlin.dsl.accessors._80e422bfb44acd38519c1b2ed4303c90.javaToolchains
import org.gradle.api.Project
import org.gradle.api.tasks.testing.Test
import org.gradle.jvm.toolchain.JavaLanguageVersion
import org.gradle.kotlin.dsl.register

const val testsOnJDK11TaskName: String = "testOnJdk11"

fun Project.addJdk11Tests() {
val testTask = tasks.register<Test>(testsOnJDK11TaskName) {
javaLauncher.set(
javaToolchains.launcherFor {
languageVersion.set(JavaLanguageVersion.of(11))
}
)
}

tasks.named("check") {
dependsOn(testTask)
}
}
32 changes: 32 additions & 0 deletions buildSrc/src/main/kotlin/ktlint-kotlin-common.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
id("org.jetbrains.kotlin.jvm")
}

repositories {
mavenCentral()
}

kotlin {
jvmToolchain {
(this as JavaToolchainSpec).languageVersion.set(JavaLanguageVersion.of(8))
}

// Do not enable explicit api for cli project
if (project.name != "ktlint") {
explicitApiWarning()
}
}

tasks
.withType<KotlinCompile>()
.configureEach {
kotlinOptions {
apiVersion = "1.4"
@Suppress("SuspiciousCollectionReassignment")
freeCompilerArgs += listOf("-Xopt-in=kotlin.RequiresOptIn")
}
}

addJdk11Tests()
8 changes: 5 additions & 3 deletions gradle/verification-metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<trusting group="org.jetbrains.kotlinx"/>
<trusting group="org.jetbrains.spek"/>
</trusted-key>
<trusted-key id="8a10792983023d5d14c93b488d7f1bec1e2ecae7" group="^com[.]fasterxml($|([.].*))" regex="true"/>
<trusted-key id="8e3a02905a1ae67e7b0f9acd3967d4eda591b991">
<trusting group="org.jetbrains.kotlinx"/>
<trusting group="org.jetbrains.kotlinx" name="kotlinx-html-jvm" version="0.7.3"/>
Expand Down Expand Up @@ -141,6 +142,7 @@
</component>
<component group="com.fasterxml" name="oss-parent" version="38">
<artifact name="oss-parent-38.pom">
<pgp value="6214760097dc5cfad0175ac2c9fbaa83a8753994"/>
<sha256 value="c83f8f45dfdca8d0b6b3661c60b3f84780f671b12e06f91ad5d1c1a1d1f966e8" origin="Generated by Gradle"/>
</artifact>
</component>
Expand Down Expand Up @@ -1040,9 +1042,9 @@
<sha256 value="965aeb2bedff369819bdde1bf7a0b3b89b8247dd69c88b86375d76163bb8c397" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="org.jetbrains" name="markdown" version="0.2.1">
<artifact name="markdown-0.2.1.module">
<sha256 value="df73ca2f4f87e0f28892155c6ad4ab757246dd63880b9a18ea0e52f99b23813f" origin="Generated by Gradle"/>
<component group="org.jetbrains" name="markdown" version="0.2.4">
<artifact name="markdown-metadata-0.2.4.jar">
<sha256 value="23955bb4bbc929f7a98d79eb63ae91ae430abf80123315310f7eb433cf7b90d4" origin="Generated by Gradle because artifact wasn't signed"/>
</artifact>
</component>
<component group="org.jetbrains" name="markdown-jvm" version="0.2.1">
Expand Down
2 changes: 1 addition & 1 deletion ktlint-core/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id 'org.jetbrains.kotlin.jvm'
id 'ktlint-publication'
id 'ktlint-kotlin-common'
}

dependencies {
Expand Down
2 changes: 1 addition & 1 deletion ktlint-reporter-baseline/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id 'org.jetbrains.kotlin.jvm'
id 'ktlint-publication'
id 'ktlint-kotlin-common'
}

dependencies {
Expand Down
2 changes: 1 addition & 1 deletion ktlint-reporter-checkstyle/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'org.jetbrains.kotlin.jvm'
id 'ktlint-kotlin-common'
id 'ktlint-publication'
}

Expand Down
2 changes: 1 addition & 1 deletion ktlint-reporter-html/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'org.jetbrains.kotlin.jvm'
id 'ktlint-kotlin-common'
id 'ktlint-publication'
}

Expand Down
2 changes: 1 addition & 1 deletion ktlint-reporter-json/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'org.jetbrains.kotlin.jvm'
id 'ktlint-kotlin-common'
id 'ktlint-publication'
}

Expand Down
2 changes: 1 addition & 1 deletion ktlint-reporter-plain/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'org.jetbrains.kotlin.jvm'
id 'ktlint-kotlin-common'
id 'ktlint-publication'
}

Expand Down
2 changes: 1 addition & 1 deletion ktlint-reporter-sarif/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'org.jetbrains.kotlin.jvm'
id 'ktlint-kotlin-common'
id 'ktlint-publication'
}

Expand Down
2 changes: 1 addition & 1 deletion ktlint-ruleset-experimental/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'org.jetbrains.kotlin.jvm'
id 'ktlint-kotlin-common'
id 'ktlint-publication'
}

Expand Down
2 changes: 1 addition & 1 deletion ktlint-ruleset-standard/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'org.jetbrains.kotlin.jvm'
id 'ktlint-kotlin-common'
id 'ktlint-publication'
}

Expand Down
2 changes: 1 addition & 1 deletion ktlint-ruleset-template/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'org.jetbrains.kotlin.jvm'
id 'ktlint-kotlin-common' // replace it with 'org.jetbrains.kotlin.jvm'
id 'java-library'
id 'maven-publish'
}
Expand Down
2 changes: 1 addition & 1 deletion ktlint-ruleset-test/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'org.jetbrains.kotlin.jvm'
id 'ktlint-kotlin-common'
id 'ktlint-publication'
}

Expand Down
2 changes: 1 addition & 1 deletion ktlint-test/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'org.jetbrains.kotlin.jvm'
id 'ktlint-kotlin-common'
id 'ktlint-publication'
}

Expand Down
2 changes: 1 addition & 1 deletion ktlint/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import org.gradle.crypto.checksum.Checksum

plugins {
id 'org.jetbrains.kotlin.jvm'
id 'ktlint-publication'
id 'ktlint-kotlin-common'
id 'com.github.johnrengelman.shadow'
id 'org.gradle.crypto.checksum'
id 'signing'
Expand Down
8 changes: 0 additions & 8 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,12 @@ pluginManagement {
}

plugins {
def kotlinVersion = settings.hasProperty("kotlinDev") ? "1.6.0" : "1.6.0"
id 'org.jetbrains.kotlin.jvm' version kotlinVersion
id 'com.github.breadmoirai.github-release' version '2.2.12'
id 'com.github.johnrengelman.shadow' version '7.1.0'
id 'org.gradle.crypto.checksum' version '1.1.0'
}
}

// Pass '-PkotlinDev' to command line to enable kotlin-in-development version
gradle.ext.isKotlinDev = settings.hasProperty("kotlinDev")
if (gradle.ext.isKotlinDev) {
logger.warn("Enabling kotlin dev version")
}

rootProject.name = 'ktlint'

include ':ktlint'
Expand Down

0 comments on commit 1e7cf75

Please sign in to comment.