0
0

More than 1 year has passed since last update.

gradle init で作ったプロジェクトをEclipseにインポートするとテストクラスがバージョン不一致エラー

Posted at

事象

gradle init でJavaプロジェクトを作成しEclipseにインポートすると、spockのテストクラスでエラーが出たのでこの対応記録です。
依存関係のgroovyバージョンとEclipseのgroovyバージョンを合わせることで解消できました。

環境

  • 環境変数のgradleバージョン
$ gradle -v

------------------------------------------------------------
Gradle 7.5.1
------------------------------------------------------------

Build time:   2022-08-05 21:17:56 UTC
Revision:     d1daa0cbf1a0103000b71484e1dbfe096e095918

Kotlin:       1.6.21
Groovy:       3.0.10
Ant:          Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM:          18.0.2 (Amazon.com Inc. 18.0.2+9-FR)
OS:           Windows 11 10.0 amd64
  • Eclipse のGroovyバージョン
    image.png

作成したプロジェクト

gradle initで作ったJava + spock のプロジェクトです。

gradle init
Select type of project to generate:
  1: basic
  2: application
  3: library
  4: Gradle plugin
Enter selection (default: basic) [1..4] 2

Select implementation language:
  1: C++
  2: Groovy
  3: Java
  4: Kotlin
  5: Scala
  6: Swift
Enter selection (default: Java) [1..6] 3

Split functionality across multiple subprojects?:
  1: no - only one application project
  2: yes - application and library projects
Enter selection (default: no - only one application project) [1..2] 1

Select build script DSL:
  1: Groovy
  2: Kotlin
Enter selection (default: Groovy) [1..2] 1

Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no]
Select test framework:
  1: JUnit 4
  2: TestNG
  3: Spock
  4: JUnit Jupiter
Enter selection (default: JUnit Jupiter) [1..4] 3

Project name (default: sandbox):
Source package (default: sandbox):

> Task :init
Get more help with your project: https://docs.gradle.org/7.5.1/samples/sample_building_java_applications.html

BUILD SUCCESSFUL in 20s
2 actionable tasks: 2 executed

build.gralde にeclipseプラグインだけ追加してeclipseプロジェクト化します。
image.png

$ gradle clean eclipse

エラー内容

Eclipseにプロジェクトをインポートすると問題タブに以下が出ました。
テストクラスがコンパイルされずに実行できない状態です。

Groovy:Unexpected problem with AST transform: The Spock compiler plugin cannot execute because Spock 2.1.0-groovy-3.0 is not compatible with Groovy 4.0.5. For more information (including enforce mode), see https://docs.spockframework.org (section 'Known Issues').

原因

自動生成されたビルドスクリプトを確認すると、groovyは3.0.10ですがEclipse上は4.0.5のためバージョンが合わないようです。

build.gradle
dependencies {
    // Use the latest Groovy version for Spock testing
    testImplementation 'org.codehaus.groovy:groovy:3.0.10'

    // Use the awesome Spock testing and specification framework even with Java
    testImplementation 'org.spockframework:spock-core:2.1-groovy-3.0'
}

とりあえず4系に合わせて現在の最新バージョンに修正します。
groovyは4.0からグループIDがorg.apache.groovyに変わってるので要注意。

以下のように修正します。

build.gradle
dependencies {
    // Use the latest Groovy version for Spock testing
    testImplementation 'org.apache.groovy:groovy:4.0.6'

    // Use the awesome Spock testing and specification framework even with Java
    testImplementation 'org.spockframework:spock-core:2.3-groovy-4.0'
}

これでもう一度clean するとエラーが解消されました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0