4

The following groovy scripts fail using command line

@Grab("org.apache.poi:poi:3.9")
println "test"

Error:

unexpected token: println @ line 2, column 1.
  println "test"
  ^
1 error

Removing the Grab, it works! Anything I missed?

$>groovy -v
Groovy Version: 2.1.7 JVM: 1.7.0_25 Vendor: Oracle Corporation OS: Linux
M. Justin
  • 14,487
  • 7
  • 91
  • 130
fixitagain
  • 6,543
  • 3
  • 20
  • 24

2 Answers2

9

Annotations can only be applied to certain targets. See SO: Why can't I do a method call after a @Grab declaration in a Groovy script?

@Grab("org.apache.poi:poi:3.9")
dummy = null
println "test"

Alternatively you can use grab as a method call:

import static groovy.grape.Grape.grab
grab(group: "org.apache.poi", module: "poi", version: "3.9")
println "test"

For more information refer to Groovy Language Documentation > Dependency management with Grape.

Community
  • 1
  • 1
James Allman
  • 40,573
  • 11
  • 57
  • 70
0

File 'Grabber.groovy'

package org.taste

import groovy.grape.Grape

//List<List[]> artifacts => [[<group>,<module>,<version>,[<Maven-URL>]],..]
static def grab (List<List[]> artifacts) {
    ClassLoader classLoader = new groovy.lang.GroovyClassLoader()
    def eal = Grape.getEnableAutoDownload()
    artifacts.each { artifact -> {
            Map param = [
                classLoader: classLoader, 
                group : artifact.get(0),
                module : artifact.get(1),
                version : artifact.get(2),
                classifier : (artifact.size() < 4) ? null : artifact.get(3)
            ]
            println param
            Grape.grab(param) 
        }
    }
    Grape.setEnableAutoDownload(eal)
}

Usage :

package org.taste
import org.taste.Grabber

Grabber.grab([
    [ "org.codehaus.groovy.modules.http-builder", "http-builder", '0.7.1'],
    [ "org.postgresql", "postgresql", '42.3.1', null ],
    [ "com.oracle.database.jdbc", "ojdbc8", '12.2.0.1', null]
])
user16547619
  • 199
  • 1
  • 4