0

I feel a bit silly for making this question, but I'm out of options.

Basically, I have this on a "let's learn groovy" project:

package com.mypackage.markupexercise

import groovy.xml.MarkupBuilder

println "Hello MarkupBuilder"

def writer = new StringWriter()
def builder = new MarkupBuilder(writer)

builder.records() {
    example(id:1)
}

And I'm getting the following compilation error:

<path>\markupbuilderexercise\Main.groovy: 3: unable to resolve class groovy.xml.MarkupBuilder
 @ line 3, column 1.
   import groovy.xml.MarkupBuilder
   ^

1 error

Now, I've seen [this question][1], and I have imported the groovy-xml-3.09.jar file (Through right click on project -> Properties -> Java Build Path -> Add Jars...)

There is nothing on the "Problems" tab, and if I try to run through the groovy console, I get the following error:

groovy.lang.MissingMethodException: No signature of method: groovy.xml.MarkupBuilder.records() is applicable for argument types: (com.neuro.groovyhelloworld.markupbuilderexercise.Main$_run_closure1) values: [com.neuro.groovyhelloworld.markupbuilderexercise.Main$_run_closure1@2e757861]

    at com.neuro.groovyhelloworld.markupbuilderexercise.Main.run(Main.groovy:10)

    at com.neuro.groovyhelloworld.markupbuilderexercise.Main.main(Main.groovy)

My take here is that I'm not adding the dependency in the right way, but I'm a Java developer and dependencies were either what I've done or just relying on Maven/Gradle, so I'm kind of out of ideas here.

What am I missing?

  [1]: https://stackoverflow.com/questions/59132101/eclipse-groovy-unable-to-resolve-class
Neuromante
  • 531
  • 2
  • 12
  • 29

1 Answers1

1

I see the same issue when choosing Run As > Groovy Console and then running the script. It does work fine for Run As > Groovy Script on the following source -- I used grab so I didn't need to add groovy-xml to my project's classpath:

@Grab('org.codehaus.groovy:groovy-xml:3.0.9')
import groovy.xml.MarkupBuilder

def writer = new StringWriter()
new MarkupBuilder(writer).tap {
  records {
    example(id:1)
  }
}

print writer

I get the following output:

<records>
  <example id='1' />
</records>

You can also build the project and run the script using Run As > Java Application.

emilles
  • 1,104
  • 7
  • 14
  • Well, that's the answer, but now I got more questions than before, lol. It works with only putting the "grab" annotation as you wrote it (the official documentation had some bits on 20 different ways to write it, but wasn't really clear on it), and it seems it ignores eclipse configuration? Is that the only way to add dependencies (by putting a @grab in each file that needs it?) What a mess... – Neuromante Sep 27 '21 at 14:24
  • There are many ways to add dependencies to a project in eclipse. If you are just writing scripts to try out features, then Grab is the most lightweight. You can put the jars in the ~/.groovy/lib directory and restart eclipse. You can add the jar if you have it downloaded with Properties -> Java Build Path -> Add Jars..., just as you described. Or you can use Maven, Gradle or Ivy to manage dependencies for your project. – emilles Sep 27 '21 at 15:14
  • I looked into why Groovy Console is not working. It seems that way down in MetaClassImpl#invokeMissingMethod it is checking if MarkupBuilder implements invokeMethod(String,Object) which it does. However this check is failing to resolve the method, which is the key handling "records" and "example". It is possible to rewrite the method call "records(){...}" to "delegate.invokeMethod('records',[{...}].toArray())" to prove that it can work in Groovy Console if that resolution succeeded. – emilles Sep 27 '21 at 15:17
  • "it seems it ignores eclipse configuration" -- not the case. The eclipse project classpath is encoded into the Groovy Console or Script command line, which you can have a look at under Run > Run Configurations... > Groovy Console > your_script > Arguments. – emilles Sep 27 '21 at 15:20
  • As I wrote in the original post, I tried the Properties -> Java Build Path -> Add Jars and it didn't work. That's why I wrote about ignoring eclipse config. The same with Maven. I mean, that's the reason I wrote the post to begin with, lol. I assumed there was some problem with the integration or something. – Neuromante Sep 29 '21 at 10:16