kdnakt blog

hello there.

gradleでKotlin/Nativeのテストをスキップしてビルドする

ググって2, 3分で解決したけどメモしておく。

 

 

[build.gradle.ktsの設定]

今回利用しているKotlin/Nativeプロジェクトの設定ファイルは次のようになっている。

kmonkey/build.gradle.kts at master · kdnakt/kmonkey · GitHub

plugins {
    kotlin("multiplatform") version "1.4.21"
}

group = "me.akito"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

kotlin {
    val hostOs = System.getProperty("os.name")
    val isMingwX64 = hostOs.startsWith("Windows")
    val nativeTarget = when {
        hostOs == "Mac OS X" -> macosX64("native")
        hostOs == "Linux" -> linuxX64("native")
        isMingwX64 -> mingwX64("native")
        else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
    }

    nativeTarget.apply {
        // For kotlinx-cli
        compilations["main"].enableEndorsedLibs = true
        binaries {
            executable {
                entryPoint = "main"
            }
        }
    }
    sourceSets {
        val nativeMain by getting
        val nativeTest by getting
    }
}

 

kotlinx-cliというライブラリを利用するための設定を追加している箇所を除いて、基本的にはKotlin/Nativeのチュートリアルで書かれているようにIntelliJ IDEAで作成した設定ファイルをそのまま利用している。

 

利用しているバージョンや環境情報は以下の通り。

$ gradle --version

------------------------------------------------------------
Gradle 6.7.1
------------------------------------------------------------

Build time:   2020-11-16 17:09:24 UTC
Revision:     2972ff02f3210d2ceed2f1ea880f026acfbab5c0

Kotlin:       1.3.72
Groovy:       2.5.12
Ant:          Apache Ant(TM) version 1.10.8 compiled on May 10 2020
JVM:          15.0.1 (Oracle Corporation 15.0.1+9)
OS:           Mac OS X 10.15.7 x86_64

 

[タスクをスキップする]

実装の途中で、とあるメソッドの引数の型をList<E>からMutableList<E>に変更した。実装の途中だったので、テストが失敗する状態になっている。そのため、gradle buildコマンドを実行すると、テストが失敗してビルドも失敗してしまう。

 

順番的にはコンパイルしてからテストが実行されるので、テストが実行されているということは、メソッドの型を変更したことによってコンパイルエラーは起きていないはずである。

ただ、なんとなくビルドが失敗しているのが気持ち悪いので、テストをスキップしてビルドが成功するところが見たい。

 

普段はmavenを利用している。mavenではmvn build -DskipTestsのようにしてビルド時のテストをスキップできる。きっとgradleでも同じことができるはず、と思い検索してみるとStackOverflowにたどり着いた。

stackoverflow.com

 

回答によれば、gradle build -x testのようなコマンドを実行することで、指定されたテストを除外(exclude)できるらしい。

 

早速実行してみると以下のようにエラーが出た。

$ gradle build -x test
(略)

FAILURE: Build failed with an exception.

* What went wrong:
Task 'test' not found in root project 'kmonkey'.

* Try:
Run gradle tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.7.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 960ms

 

testというタスクがこのプロジェクトにはない、ということらしい。

もう一度gradle buildコマンドを実行して確認してみると、以下の出力が得られた。

$ gradle build
(略)

> Task :nativeTest

ast.ModifyTest.testModify FAILED
    kotlin.AssertionError at /Users/teamcity/buildAgent/work/f01984a9f5203417/runtime/src/main/kotlin/kotlin/Throwable.kt:23

evaluator.QuoteUnquoteTest.testQuoteUnquote FAILED
    kotlin.AssertionError at /Users/teamcity/buildAgent/work/f01984a9f5203417/runtime/src/main/kotlin/kotlin/Throwable.kt:23

42 tests completed, 2 failed
There were failing tests

> Task :allTests FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':allTests'.
> There were failing tests. See the report at: file:///Users/akito/Develop/kdnakt/kmonkey/build/reports/tests/allTests/index.html

(略)

BUILD FAILED in 1s
13 actionable tasks: 2 executed, 11 up-to-date

 

nativeTestタスクが失敗して、結果としてallTestsタスクも失敗している、ということらしい。

gradle build -x allTestsでテストをスキップできるかな、と思ってやってみたが、結局nativeTestタスクが実行され、ビルドが失敗してしまった。

というわけで、nativeTestタスクをスキップしてビルドを実行してみると、以下のようにビルドが成功した。

$ gradle build -x nativeTest

> Configure project :
Kotlin Multiplatform Projects are an Alpha feature. See: https://kotlinlang.org/docs/reference/evolution/components-stability.html. To hide this message, add 'kotlin.mpp.stability.nowarn=true' to the Gradle properties.


Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.7.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 784ms
10 actionable tasks: 1 executed, 9 up-to-date

 

[gradleタスクの一覧を確認する]

gradle build -x testコマンドを実行してビルドに失敗したときに、タスクが存在しないため、gradle tasksコマンドを実行してタスクの一覧を確認しろとエラーメッセージが出ていた。

今回のプロジェクトで実行すると、以下の結果が得られた。思っていたより多くのタスクが定義されていることが分かった。

$ gradle tasks
(略)

> Task :tasks

------------------------------------------------------------
Tasks runnable from root project
------------------------------------------------------------

Build tasks
-----------
allMetadataJar - Assembles a jar archive containing the metadata for all Kotlin source sets.
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
clean - Deletes the build directory.
compileKotlinNative - Compiles a klibrary from the 'main' compilation for target 'native'.
compileTestKotlinNative - Compiles a klibrary from the 'test' compilation for target 'native'.
linkDebugExecutableNative - Links an executable 'debugExecutable' for a target 'native'.
linkDebugTestNative - Links a test executable 'debugTest' for a target 'native'.
linkReleaseExecutableNative - Links an executable 'releaseExecutable' for a target 'native'.
metadataCommonMainClasses - Assembles outputs for compilation 'commonMain' of target 'metadata'
metadataMainClasses - Assembles outputs for compilation 'main' of target 'metadata'
nativeMainKlibrary - Assembles outputs for compilation 'main' of target 'native'
nativeTestKlibrary - Assembles outputs for compilation 'test' of target 'native'

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'kmonkey'.
components - Displays the components produced by root project 'kmonkey'. [incubating]
dependencies - Displays all dependencies declared in root project 'kmonkey'.
dependencyInsight - Displays the insight into a specific dependency in root project 'kmonkey'.
dependentComponents - Displays the dependent components of components in root project 'kmonkey'. [incubating]
help - Displays a help message.
kotlinDslAccessorsReport - Prints the Kotlin code for accessing the currently available project extensions and conventions.
model - Displays the configuration model of root project 'kmonkey'. [incubating]
outgoingVariants - Displays the outgoing variants of root project 'kmonkey'.
projects - Displays the sub-projects of root project 'kmonkey'.
properties - Displays the properties of root project 'kmonkey'.
tasks - Displays the tasks runnable from root project 'kmonkey'.

Run tasks
---------
runDebugExecutableNative - Executes Kotlin/Native executable debugExecutable for target native
runReleaseExecutableNative - Executes Kotlin/Native executable releaseExecutable for target native

Verification tasks
------------------
allTests - Runs the tests for all targets and create aggregated report
check - Runs all checks.
nativeTest - Executes Kotlin/Native unit tests for target native.

Rules
-----
Pattern: clean<TaskName>: Cleans the output files of a task.
Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.
Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration.

To see all tasks and more detail, run gradle tasks --all

To see more detail about a task, run gradle help --task <task>

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.7.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 743ms
1 actionable task: 1 executed

 

実際にはこのようにカラフルで見やすい出力が得られる。

f:id:kidani_a:20210516171214p:plain

 

[まとめ]

  • gradleで特定のタスクをスキップしてビルドを実行するときはgradle build -x <タスク名>
  • Kotlin/Nativeでネイティブのテストをスキップする場合はgradle build -x nativeTest
  • gradle tasksで定義されているタスク一覧を取得できる