1

I try to define some groovy module that has 2 different scripts near from apply plugin: 'android' in Android Studio, but I receive an Error. As I found, apply plugin: 'android' and apply plugin: 'groovy' are incompatible, for this reason I starting my scripts like:

def gse = new GroovyScriptEngine([pathToFolderOfScript] as String[])
gse.run('LoadJsonFromServer.groovy', new Binding())

Is there any another more elegant way? Such as this, for exmaple: Running Groovy scripts from Gradle

Community
  • 1
  • 1
AntonD
  • 108
  • 12

2 Answers2

3

Assuming that every user has groovy installed the following script will work:

build.gradle

task runScript (type: Exec) {
   commandLine  'groovy', '<PATH_TO_SCRIPTS>/myscript.groovy'// it's good idea to take the script path from env variable
}

myscript.groovy

@Grab(group='org.springframework', module='spring', version='2.5.6')
import org.springframework.jdbc.core.JdbcTemplate

def tmpl = new JdbcTemplate()

println tmpl.hashCode()
Opal
  • 81,889
  • 28
  • 189
  • 210
  • You should just use the Groovy which Gradle itself uses, not expect users to install Groovy in their machines. – Renato Sep 30 '14 at 15:14
1
Eval.me(new File("path/to/file").text)
Renato
  • 12,940
  • 3
  • 54
  • 85
  • For more advanced use cases, see http://groovy.codehaus.org/Embedding+Groovy – Renato Sep 29 '14 at 21:51
  • I suppose it will not work with `Grapes` dependency resolution. – Opal Sep 30 '14 at 06:19
  • did you try? I don't see why that wouldn't work. – Renato Sep 30 '14 at 09:00
  • Yes, I've already tried without success. I'm getting `java.lang.NoClassDefFoundError: org/apache/ivy/core/report/ResolveReport`, so it seems CP problem only. Nevertheless it requires additional work. – Opal Sep 30 '14 at 09:08
  • This works just fine from the groovy console. I think you might need to update your version of Gradle? – Renato Sep 30 '14 at 15:17
  • Ok, what is the content of the file You're evaluating? If You put `Grapes` code, copy my sample script, it won't work. I've gradle 2.1. – Opal Sep 30 '14 at 15:21
  • I found that Gradle has a limited version of Groovy: http://gradle.1045684.n5.nabble.com/problem-with-gradle-and-groovy-scripts-with-Grapes-td3315546.html So if you want to make this work, you should explicitly set your version of Groovy for Gradle to use. Hope you can figure out how to do that. – Renato Sep 30 '14 at 15:28
  • My bet is: add a "buildScript" dependency on groovy-all. – Renato Sep 30 '14 at 15:29
  • The problem is that the code is not working without using gradle. Nevermind. Forget about it. – Opal Sep 30 '14 at 16:05