2

I want use Nexus 3's embedded groovy instead of installing groovy package that installs openjdk java. Is there an easy way to do this?

I want to run this command using the embedded groovy:

groovy addUpdateScript.groovy -u "admin" -p "admin123" -n "raw" -f "rawRepositories.groovy" -h "http://localhost:8081"

This is to add groovy script support to Nexus3 Chef cookbook: https://supermarket.chef.io/cookbooks/nexus3

Dennis Hoer
  • 3,039
  • 2
  • 23
  • 34

3 Answers3

3

The groovy that is embedded in the Nexus Repository Manager is purely available on the classpath of the application. It will be easier to either install a client side groovy system or just upload the groovy script with a shell script that uses simple bash commands. Examples for that are also part of the script examples. You can find them in the examples repository at https://github.com/sonatype/nexus-book-examples/tree/nexus-3.x/scripting/simple-shell-example

I also put together a blog post and video demos of all this at http://www.sonatype.org/nexus/2016/06/08/integrating-nexus-repository-3/

I hope that helps.

Nicolae
  • 441
  • 6
  • 14
Manfred Moser
  • 29,539
  • 13
  • 92
  • 123
  • So you are saying I could upload a complex groovy script via curl PUT switching content-type to text/plain like this? curl -v -X PUT -u admin:admin123 --header "Content-Type: text/plain" "http://localhost:8081/service/siesta/rest/v1/script/$name" -d @$myComplexGroovyFile – Dennis Hoer Aug 02 '16 at 23:19
  • Yes. There are example shell scripts for creating, deleting and running jobs in the repo. And a few more and check the video demo to see them in action. – Manfred Moser Aug 02 '16 at 23:43
  • Thanks, I will give that a try. I already seen the videos, but didn't realize I could upload a groovy file as well. I thought it had to be a json file only. I did reference videos as well as the scripts: https://github.com/dhoer/chef-nexus3/tree/master/api_examples#api_examples. Thanks for putting those presentations and examples out there. I hope add more examples like ldap config etc... to the chef resource. – Dennis Hoer Aug 02 '16 at 23:48
  • That didn't work. I received a "415 Unsupported Media Type" error when text/plain content type. The goal is to not require installing groovy package, since that installs OpenJDK. – Dennis Hoer Aug 07 '16 at 21:12
  • You have to make json files that contain the groovy scripts and upload them just like in the videos. It works fine when you do that. – Manfred Moser Aug 08 '16 at 16:19
  • Could you resolve the problem? I'm facing the same issue and I don't know how to fix it – Jessica Jan 07 '23 at 08:36
1

In Nexus 3.10, you have a Groovy 2.4.11 for free, in $NEXUS_HOME/nexus-3.10.0-04/system/org/codehaus/groovy/groovy-all/2.4.11

You can write a test script:

println "hello world"

and execute it with

java -jar groovy-all-2.4.11.jar printme.groovy

Of course you still need a JDK/JRE ...

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Pierluigi Vernetto
  • 1,954
  • 1
  • 25
  • 27
0

enter code hereI found the solution! you have to first convert that groovy script to a JSON file and pass that JSON file to your curl or API(for creating script) that you are working with and we can do it with this python file:

import json

with open("[THE GROOVY FILE]", "r") as inputfile:
    filedata = inputfile.read()
    jsondata = {}
    jsondata['name'] = 'testscript2'
    jsondata['type'] = 'groovy'
    jsondata['content'] = filedata

    with open("[OUTPUT FILE]", "w") as outputfile:
        outputfile.write(json.dumps(jsondata))

the output file is something like this:

{
"name": "name",
"type": "groovy",
"content": "..."
}

then use this output file which is a JSON file and pass this to that command and there you go! the command for creat script with REST API is:

curl -v -X POST -u admin:admin --header "Content-Type: application/json" "http://<url>/service/rest/v1/script" -d @{output}.json

the above command will create a script and you can execute it with:

curl -v -X POST -u admin:admin --header "Content-Type: application/json" "http://<url>/service/rest/v1/script/{name}/run
Jessica
  • 127
  • 2
  • 10