Groovy 2: Cant find JSonSlurper and XmlSlurper anymore - groovy

I upgraded to groovy 2 release and now my build is broken.
It fails when importing classes: groovy.json.JsonSlurper and XmlSlurper.
I have checked http://groovy.codehaus.org/gapi/ and cannot find these classes anymore. Do they still exist in groovy 2? Or have they moved somewhere?

The groovy.jar distributed with groovy 2 has been split out to contain just the bare minimum, with all the additional modules (XML, SQL, JSON, etc.) in separate jars. However, in the embeddable directory, you'll find a jar file groovy-all-2.0.0.jar which contains groovy and all the modules together, like previous versions. The easiest way to migrate is to use this jar file.
If you're using Maven Central, you can use an artifactId of groovy-all to get everything, or groovy (plus modules) to have finer grained control over your dependencies. Here's a list of the modules available on Maven Central: http://search.maven.org/#search|ga|1|g%3A%22org.codehaus.groovy%22

Never mind. Need to include groovy-xml and groovy-json jars.
These were split from groovy's jar. See: http://www.infoq.com/articles/new-groovy-20

When I import, groovy-json-2.4.3 and groovy-xml-2.4.3 , JsonSlurper is recognized.
Also refer for new code refactor after 1.8.0 version: Parsing array of JSON arrays in Groovy

I faced a similar issue in Gradle build of a Java project (Gradle uses Groovy).
Gradle stopped including the local Groovy libs in classpath automatically. So, I had to include the following lines (marked with +) in my build.gradle file:
buildscript {
ext {
springbootVersion = '2.x.x'
awsVersion = '1.x.x'
...
}
+ dependencies {
+ classpath localGroovy()
+ }
repositories ...
}

Instead of using "groovy.json.jsonSlurper", use "net.sf.json.groovy.JsonSlurper".
Your script must be running.

Related

How can I use Groovy classes in other packages without JAR?

I'm pretty new to Groovy (coming from Java), so this may be a stupid question :-)
Nonetheless: I'd like to structure a couple of Groovy scripts using packages. And I'd like to import some general Groovy classes from some other package.
How can I make sure that my Groovy scripts finds the other classes in the other packages? The only classpath related files I can remember are JARs.
If you from java:
Groovy loads classes as java and also includes non-compiled with extension .groovy.
So, you have to place your classes relative to classpath according to their package name.
The command line should be something like this:
groovy -cp "path_to_classes_root" "path_and_name_of_main_groovy_script.groovy"

How to convert picocli groovy-grape script to native standalone app?

I have a made my first groovy CLI app with picocli. Now, I want it to be available for use without any JVM installed on the client machine, maybe with the use of GraalVM.
This is for an opensource project:
https://github.com/kchaitanya863/db2csv
Another easy option is to dockerize your script (read this blog about how to do it https://groovy-lang.gitlab.io/101-scripts/docker/basico-en.html)
If you want to build a linux executable you need to change your project:
convert to a gradle project (maven is also an option but gradle has a lot of plugins)
change your script to a class with a tipical main (and move it to the standard directory src/main/groovy/mypackage)
add some tasks into you build.gradle similar to these https://gitlab.com/snippets/1797638
You will need to:
statically compile your groovy script
make the args variable available after static compilation with
final String[] args = getProperty("args") as String[]
specify a reflection configuration file for the classes dynamically loaded/invoked using reflection by Groovy (this may be useful)
specify a reflection configuration file for the classes loaded/invoked using reflection by picocli. The picocli-codegen module provides a picocli.codegen.aot.graalvm.ReflectionConfigGenerator tool to generate the configuration file.
If your script has any #Grape dependencies, you may need to turn off the Grape dependency manager with -Dgroovy.grape.enabled=false and add all dependencies to the classpath manually instead
Credit: I got most of these tips from this article by Szymon Stepniak
If you want to use Graal with Groovy, check out this article:
https://e.printstacktrace.blog/graalvm-and-groovy-how-to-start/

Groovy-Eclipse 2.5.2: java.lang.ClassNotFoundException: picocli.CommandLine$ParameterException

I'm using Eclipse 4.5 with the Groovy-Eclipse 2.9.2/4.5 plugin which I thought was supposed to have the Groovy 2.5 compiler. However, it didn't have any picocli support so I added the groovy-cli-picocli-2.5.2-indy.jar to my classpath and was able to compile. However #2, when trying to run the script via Eclipse I get:
java.lang.ClassNotFoundException: picocli.CommandLine$ParameterException
It looks like groovy-cli-picocli-2.5.2-indy.jar does not have CommandLine class at all.
I would just throw jars at this from the fullblown picocli distribution but I'm under the impression they all have to somehow wrap nicely into Eclipse Groovy library via groovy.cli.picocli.CliBuilder.
Is my Groovy 2.5.2 missing this or am I somehow missing the boat on how it's supposed to work because picocli is not working for me in this configuration. Thanks!
You are correct: groovy-cli-picocli-2.5.2.jar (and groovy-cli-picocli-2.5.2-indy.jar) do not contain the picocli classes.
You need to add the picocli jar to the classpath.
If you use Maven, the groovy-all POM should include all dependencies.
(My original answer mentioned picocli classes that are shaded into the groovy-2.5.x.jar under the groovyjarjarpicocli package but these are intended for use internally by Groovy and not meant to be used by applications.)

How to make a GDSL file in a jar get picked up in another project in Intellij?

I have a project with a GDSL file that describes a DSL delegate like:
def ctx = context pathRegexp: ".*installer\\.groovy", scope: scriptScope()
contributor(ctx) {
delegatesTo(findClass("com.whatever.InstallerBase"))
}
I package this file up in the jar (just in the root of the jar) using maven.
In a separate project I have a maven dependency on my jar artifact containing the gdsl. However, my autocompletion doesn't work. It works fine with the sample scripts in the first project (with the GDSL).
Is there a step that I'm missing in order for the GDSL to be picked up? Do I need to place it in a special folder in the jar?
The problem was indeed what #PeterGromov indicated in the comment on the question:
ensure that the library jar is only attached as classes and not library source as well
both the source and library were configured and thus IDEA doesn't include it. I have opened a youtrack issue to fix this here:
https://youtrack.jetbrains.com/issue/IDEA-137411

Cucumber/gradle example not generating report?

I'm investigating using gradle and cucumber together, and found this lovely example in cucumber's github.
So, I cloned the repository and ran it myself. It failed, as it's configured to do, but I couldn't find the HTML or JSON report that it appears to be configured to output. I say appear because I'm brand new to cucumber, but this class would seem to indicate where it'll put it:
#RunWith(Cucumber.class)
#Cucumber.Options(format = {"pretty", "html:build/cucumber-html-report", "json-pretty:build/cucumber-report.json"})
public class RunCukesTest {
}
However, it's not appearing in the build directory after running gradle cucumber.There's no cucumber-html-report directory, not is there a cucumber-report.json file. I'm running it with Java 7 and Gradle 1.6, if it matters.
Ideas? Is this a known issue with the Cucumber/Gradle integration?
The class name changed depending on the version of Cucumber you are using. It changed from json-pretty to json.
When running the 'cucumber' task on this example the generated cucumber report is located at 'build/cucumber-html-report/index.html'. Running the 'test' task fails as it seems that gradle has problems to create the test report for the cucumber created tests (file name contains spaces) I need to dig a bit into this to see how this can be fixed in gradle.
cheers,
René
The cucumber-jvm-example doesn't do reporting using gradle cucumber, but does do it with gradle test. However, gradle test will have a couple issues, namely showing a "null" test of sorts.
A workaround to this, if need be, is to add the formats to the args of the javaexec that runs cucumber. For example, in build.gradle:
javaexec {
main = "cucumber.api.cli.Main"
classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
args = ['--format', 'html:cucumber-html-report', '-f', 'pretty', '--glue', 'gradle.cucumber', 'src/test/resources']
}
I had an error with that very same line (taken from this tutorial).
In order to resolve, had to change the third parameter from "json-pretty" to just "pretty"
So this is my final code line:
#CucumberOptions(format = {"pretty", "html:target/cucumber-html-report", "pretty:target/cucumber-report.json"})
BTW,
#Cucumber.Options is deprecated, we should use CucumberOptions

Resources