Invalid duplicate class definition error (Groovy)

By Steve Claridge on 2014-03-15.

Invalid duplicate class definition of class steve.Test : The source /steve/Test.groovy contains at least two definitions of the class steve.Test

Groovy treats your .groovy file as either a script file or as a class definition file. A script file is a file that contains code that is not inside a class definition, when Groovy compiles a script file it implicitly creates a class to hold your code and the implicit class is given the name of the .groovy file. So if you have this:

package steve class Test {
  public dostuff() {
    def str = "inside a class"
  }
} println(["some","code","outside","of","a","class"].join(" "))

Then the compiler will try and create a class called Test (same as the file name) to hold the println line and the error then occurs because there are now two Test class definitions.