Call uploaded groovy script via Task

263 views
Skip to first unread message

Robert Heinzmann

unread,
Jan 11, 2018, 10:51:20 AM1/11/18
to Nexus Users
Hello, 

I want to call an already uploaded groovy script (via https://books.sonatype.com/nexus-book/3.0/reference/scripting.html#scripting-configuration) from a Script Task in Nexus 3.6. 

Note: My requirement to call an uploaded script via a task is causesd by some automation limitations I have with my puppet code which I can not easily change.

Assuming I have uploaded my Script with 

# File MyScript123.json
{
  "name": "MyScript123",
  "type": "groovy",
  "content": "repository.createMavenHosted('private')"
}

curl -v -X POST -u username:password --header "Content-Type: application/json" 'http://localhost:8081/service/siesta/rest/v1/script' -d @MyScript123.json

Can someone point me to the Groovy Code I have to call ( in the "Script Task") to call an Script uploaded as "MyScript123" with parameters ? 

Regards, 
Robert 


Michael Worthington

unread,
Jan 11, 2018, 12:22:32 PM1/11/18
to Robert Heinzmann, Nexus Users
I'm a little confused, but it looks like you want to create a new maven repository, but you need to initiate the call from the scheduled task.

The "Script Task" and "Integrations API" are basically the same, so you can just put "repository.createMavenHosted('private')" in your scheduled task configuration and run the task.

____________________
Michael Worthington
Customer Success Engineer
Sonatype
Mobile: 717-919-2227




--
You received this message because you are subscribed to the Google Groups "Nexus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nexus-users+unsubscribe@glists.sonatype.com.
To post to this group, send email to nexus...@glists.sonatype.com.
To view this discussion on the web visit https://groups.google.com/a/glists.sonatype.com/d/msgid/nexus-users/3dfc6e09-1ca0-4168-9bfa-2ced81c4c72f%40glists.sonatype.com.
For more options, visit https://groups.google.com/a/glists.sonatype.com/d/optout.

Robert Heinzmann

unread,
Jan 11, 2018, 2:28:07 PM1/11/18
to Nexus Users, elcon...@googlemail.com
Hello, 

actually the example Above is really just an Example.

I want to upload a rather large groovy script and reverence it multiple times in Tasks (calling it with different options doing different things) without storing multiple copies of the script.

Any Idea ?

Regards, 
Robert

Kelly Robinson

unread,
Jan 11, 2018, 3:12:04 PM1/11/18
to Robert Heinzmann, Nexus Users
Here's how you can execute a stored Script from another Script (whether run as a Scheduled Task or not):
import org.sonatype.nexus.script.ScriptManager
import org.sonatype.nexus.common.script.ScriptService

def scriptManager = container.lookup(ScriptManager.name)
def scriptService = container.lookup(ScriptService.name)
def script = scriptManager.get('test')
Object result = scriptService.eval(script.type, script.content, [log:log])

Hopefully this gets you where you want to go :)


--
You received this message because you are subscribed to the Google Groups "Nexus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nexus-users+unsubscribe@glists.sonatype.com.
To post to this group, send email to nexus...@glists.sonatype.com.

Alexei Znamensky

unread,
Jan 11, 2018, 3:37:02 PM1/11/18
to Kelly Robinson, Robert Heinzmann, Nexus Users
Hi Robert,

I am working on a similar concept, although not strictly equal. I am setting up some scripts to provision the configuration of Nexus upon installation, so I also need to reuse a bunch of code and copying and pasting it in many different places is definitely not the Good Way (TM) of doing that. That said, what I came up with was:

image.png
Inside the folder /my/nexus/groovy/lib I can then drop as many scripts as I want. In order to provide some consistency, I am going to write an public interface definition that will define a "standard" method signature to be called from this script above, passing the parameters and whatever else is necessary.

Not calling other scripts registered in nexus, but calling whichever classes I put into that one particular directory in the filesystem.

Hope that helps.
Cheers,
Alexei

On Fri, Jan 12, 2018 at 9:12 AM Kelly Robinson <krob...@sonatype.com> wrote:
Here's how you can execute a stored Script from another Script (whether run as a Scheduled Task or not):
import org.sonatype.nexus.script.ScriptManager
import org.sonatype.nexus.common.script.ScriptService

def scriptManager = container.lookup(ScriptManager.name)
def scriptService = container.lookup(ScriptService.name)
def script = scriptManager.get('test')
Object result = scriptService.eval(script.type, script.content, [log:log])

Hopefully this gets you where you want to go :)

On Thu, Jan 11, 2018 at 11:28 AM, 'Robert Heinzmann' via Nexus Users <nexus...@glists.sonatype.com> wrote:
Hello, 

actually the example Above is really just an Example.

I want to upload a rather large groovy script and reverence it multiple times in Tasks (calling it with different options doing different things) without storing multiple copies of the script.

Any Idea ?

Regards, 
Robert

On Thursday, January 11, 2018 at 6:22:32 PM UTC+1, Michael Worthington wrote:
I'm a little confused, but it looks like you want to create a new maven repository, but you need to initiate the call from the scheduled task.

The "Script Task" and "Integrations API" are basically the same, so you can just put "repository.createMavenHosted('private')" in your scheduled task configuration and run the task.

--
You received this message because you are subscribed to the Google Groups "Nexus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nexus-users...@glists.sonatype.com.

To post to this group, send email to nexus...@glists.sonatype.com.

--
You received this message because you are subscribed to the Google Groups "Nexus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nexus-users...@glists.sonatype.com.

To post to this group, send email to nexus...@glists.sonatype.com.


--
Alexei "RUSSOZ" Znamensky | russoz EM gmail com
"I don't know... fly casual!" -- Han Solo
Reply all
Reply to author
Forward
0 new messages