Re: Using MarkupBuilder as a closure argument

Maarten Boekhold <[email protected]> Wed, 21 May 2025 13:17:18 +0400
Newsgroups gmane.comp.lang.groovy.user
Message-ID <[email protected]>
Hi Per,

Getting back to your original question: have you looked at the closure 
delegation strategy?

https://groovy-lang.org/closures.html#_delegation_strategy

Unless I misunderstood your problem, I think this might help you to 
'delegate' property and method access to a MarkupBuilder instance?

Maarten

On May 21, 2025 11:47:39 Per Nyfelt <[email protected]> wrote:
> Gradle is great and I use it for most of my projects (and I've written some 
> gradle plugins). However, Gradle is slowly moving towards a declarative 
> style and I quite like the imperative style of Ant. It's just that 
> AntBuilder is a bit too imperative as a build system. However, when 
> extending it, adding support for targets like traditional Ant, and marrying 
> it with the maven resolver ant tasks you get a nice, flexible build system. 
> I'm just doing it for fun but I think it looks quite promising so far. The 
> only major remaining thing I haven't quite figured out yet is how to handle 
> multi-module projects in a nice way.
>
> Best regards,
> Per
>
> On Tue, 20 May 2025 at 13:36, <[email protected]> wrote:
> Hi again Per,
>
> Have you seen Gradle ? I believe that Gradle might provide what you are 
> trying to do. Personally I'm not a Gradle fan, but many are.
> Tommy Svensson
> On 20 May 2025 at 10:38 +0200, Per Nyfelt <[email protected]>, wrote:
>
>> Thanks Tommy,
>> My problem was in the context of a custom ant task. I found that if I let 
>> go of the idea of "arbitrary additional content" and defined what the 
>> different additional sections could be, it was quite easy to do. My 
>> solution is here: 
>> https://github.com/Alipsa/uso/blob/main/uso-tasks/src/main/groovy/se/alipsa/uso/tasks/CreatePom.groovy 
>> in case anyone is interested.
>>
>> Best regards,
>> Per
>>
>> On Mon, 19 May 2025 at 14:01, <[email protected]> wrote:
>> Hello Per,
>>
>> I don't know of anything existing that does what you want. But, in Groovy 
>> you can easily define structures like JSON or XML (which both give you a 
>> structure but does so using different formats.
>>
>> Example: Map<String, Object> root = [ "id": "QAZ",  "name" : "Nisse", ... ]
>>
>> This is a java.util.Map structure. Your code can then take this Map<String, 
>> Object> and convert to JSON or XML. But I don't know of anything existing 
>> that takes such a Map structure to XML. I would not be entirely surprised 
>> if such exists.
>>
>> In my current project I'm using the Groovy Map shortcut [ : ] to build 
>> structured information. In my case it will be converted to JSON and 
>> potentially other structured formats later.  But as long as you have the 
>> information stored in some structured way it should be relatively easy to 
>> convert to XML. Reading both JSON and XML is by far more difficult than 
>> producing them.
>>
>> Best Regards,
>> Tommy Svensson
>> On 18 May 2025 at 20:38 +0200, Per Nyfelt <[email protected]>, wrote:
>>
>>> Hi,
>>>
>>> I would like to have a user api that can handle the following:
>>>
>>> createXml(target: xmlFile, name: 'a test') {
>>>
>>> description('test xml')
>>>
>>> licenses {
>>> license('Apache License, Version 2.0',
>>> 'http://www.apache.org/licenses/LICENSE-2.0')
>>> }
>>>
>>> }
>>>
>>> // description and licenses are arbitrary, it can be any structure that
>>> can be converted to XML
>>>
>>> I want the closure to behave as if it would be statements to a
>>> MarkupBuilder but I am unable to figure out how to do it. Can i convert
>>> the closure to a MarkupBuilder or process it with a MarkupBuilder
>>> somehow? Any ideas?
>>>
>>> Best regards
>>>
>>> Per