Problem with @Slf4j transform and Groovy 2.0


(Sebastian YEPES FERNANDEZ) #1

New post as suggested by Peter Niederwieser : http://forums.gradle.org/gradle/topics/unable_to_force_gradle_to_use_groovy_2_0_0_for_the_project

I am trying to build a Groovy 2.0 app (jar) and it fails with the following error:

  Execution failed for task ‘:compileGroovy’.  > BUG! exception in phase ‘class generation’ in source unit ‘/test/src/main/groovy/HelloWorld.groovy’ ClassNode#getTypeClass for org.slf4j.Logger is called before the type class is set

The strange thing is that it work with Groovy 1.8.6

Here is an example:

_____build.gradle
|____src
| |____main
| | |____groovy
| | | |____HelloWorld.groovy
| | |____java
| | |____resources
| |____test
| | |____groovy
| | |____java
| | |____resources

HelloWorld.groovy:

import org.codehaus.groovy.transform.*
import org.slf4j.*
import groovy.util.logging.*
import ch.qos.logback.classic.*
import static ch.qos.logback.classic.Level.*
  @Slf4j
class HelloWorld {
  HelloWorld() { log.info "Initialization ${this.class.name}" }
    static main(args) { def main = new HelloWorld() }
}

build.gradle

apply plugin:'groovy'
group = 'HelloWorld'
archivesBaseName = 'HelloWorld'
  repositories {
  mavenLocal()
  mavenCentral()
}
dependencies {
    groovy 'org.codehaus.groovy:groovy:2.0.0',
           'org.codehaus.groovy:groovy-test:2.0.0'
           //'org.codehaus.groovy:groovy-all:2.0.0' also tried this an still get the same error
  //groovy 'org.codehaus.groovy:groovy:1.8.6'
  compile "org.slf4j:slf4j-api:1.6.6",
          "ch.qos.logback:logback-core:1.0.6",
          "ch.qos.logback:logback-classic:1.0.6"
    testCompile "junit:junit:4.10"
}
jar {
  println "+Building JAR"
  from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
  manifest {
    attributes 'Implementation-Title': 'HelloWorld'
  }
}

Update: ============= Adding this option make the build work :slight_smile: Do you know way we have to use this setting with Groovy 2.0?

build.gradle

apply plugin:'groovy'
group = 'HelloWorld'
archivesBaseName = 'HelloWorld'
  repositories {
  mavenLocal()
  mavenCentral()
}
dependencies {
    groovy 'org.codehaus.groovy:groovy:2.0.0',
           'org.codehaus.groovy:groovy-ant:2.0.0',
           'org.codehaus.groovy:groovy-test:2.0.0'
  compile "org.slf4j:slf4j-api:1.6.6",
           "ch.qos.logback:logback-core:1.0.6",
           "ch.qos.logback:logback-classic:1.0.6"
    testCompile "junit:junit:4.10"
}
compileGroovy.groovyOptions.useAnt = true
jar {
  println "+Building JAR"
  from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
  manifest {
    attributes 'Implementation-Title': 'HelloWorld'
  }
}
# gradle jar
+Building JAR
:compileJava UP-TO-DATE
:compileGroovy
When groovyOptions.useAnt is enabled, options.useAnt must also be enabled. Ignoring options.useAnt = false.
:processResources UP-TO-DATE
:classes
:jar
  BUILD SUCCESSFUL
  Total time: 14.135 secs

(Peter Niederwieser) #2

Looks like a bug in the @Slf4J transform that does not surface with the groovyc Ant task but does surface in other compiler environments (which is common for such bugs). groovy-all-2.1.0-SNAPSHOT seems to have solved the problem. Might be related to https://github.com/groovy/groovy-core/commit/2a1026bab9bd55e681cdf1d71dd6de48257edb84 which has been applied to the master (i.e. 2.1) branch but not the 2.0.x branch. If you want to dig deeper, I suggest to ask on the Groovy list.