3

How do I compile my groovy source code into java 1.8 bytecode?

I'm using Grails 2.5.4, Java 1.8.0_74

I might be missing something obvious here, but the below settings in Grails BuildConfig.groovy don't seem to affect the Java bytecode being generated for Groovy source files.

Major version 49=Java 1.5, and 52=Java 1.8 (cite: how to check the jdk version used to compile a .class file)

grails.project.target.level = 1.8
grails.project.source.level = 1.8

Then to check bytecode version:

$ javap -verbose target/classes/MyGroovy.class | grep major
(standard input):7:  major version: 49

$ javap -verbose target/classes/MyJava.class | grep major
(standard input):7:  major version: 52

$ javap -verbose target/classes/UrlMappings.class | grep major
(standard input):7:  major version: 49

$ javap -verbose target/classes/BootStrap.class | grep major
(standard input):7:  major version: 49

So based on the above results, it seems that the target.level=bla configuration does affect Java source code, but not Groovy?

My test setup:

  • Vanilla grails 2.5.4 app: 'grails create-app foo'
  • Added one java source file, and one groovy. Placed in foo/src/*
  • Modified BuildConfig.groovy and set target/source levels to '1.8' instead of the default '1.6'
  • Those are all the steps. Totally simple.

My source code:

class MyJava {                                         
        String name;                                   
        int age;                                       

        public static String foo() { return "java!"; } 
}                                                      

And...

class MyGroovy {                         
        def name                         
        def age                          

        static foo() { 'groovy!' }       
}                                        
Community
  • 1
  • 1
Vahid Pazirandeh
  • 1,552
  • 3
  • 13
  • 29

0 Answers0