How to include dependencies for Groovy script?

On issue close, I have a scripted post function that is intended to send information to another application through a REST interface. In my IDE, I use grape to get some of my dependencies:

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.6' ) @GrabExclude('xml-apis:xml-apis')

Everything works fine in the IDE... But when I try to run my script in our JIRA instance, I get:

javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:

Script1.groovy: 9: unable to resolve class groovyx.net.http.RESTClient

 @ line 9, column 1.

   import groovyx.net.http.RESTClient

   ^



Script1.groovy: 10: unable to resolve class groovyx.net.http.ContentType

 @ line 10, column 1.

   import static groovyx.net.http.ContentType.JSON

   ^



2 errors

I'd like to know how to pull together the dependencies downloaded by grape in my IDE and store them under "C:\CustomGroovy\classes". I've tried every suggested solution I could find on the web, but after a day of banging my head on the problem I've gotten nowhere. Any help would be appreciated!

Thanks,

Jason

4 answers

1 accepted

It took far more effort than I probably should have put in, but I ditched grape and used maven to create an uber-jar that contained all the dependencies I needed to use the groovy RESTClient. I dropped the new jar in the WEB-INF/lib folder and ran my script without issue.

Interesting point - I started out by throwing everything + the kitchen sink into the jar. Once it worked, I backed out every single part I could. In the end, I had to include groovy to make it work.

For anyone who needs it, here's my final POM:

<?xml version="1.0" encoding="UTF-8"?>
<!--
    Generated from archetype; please customize.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.plasmatherm.PTuberjar</groupId>
    <artifactId>PTuberjar</artifactId>
    <name>PTuberjar project</name>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>antlr</groupId>
            <artifactId>antlr</artifactId>
            <version>2.7.7</version>
        </dependency>
        <dependency>
            <groupId>asm</groupId>
            <artifactId>asm</artifactId>
            <version>3.2</version>
        </dependency>
        <dependency>
            <groupId>asm</groupId>
            <artifactId>asm-analysis</artifactId>
            <version>3.2</version>
        </dependency>
        <dependency>
            <groupId>asm</groupId>
            <artifactId>asm-commons</artifactId>
            <version>3.2</version>
        </dependency>
        <dependency>
            <groupId>asm</groupId>
            <artifactId>asm-tree</artifactId>
            <version>3.2</version>
        </dependency>
        <dependency>
            <groupId>asm</groupId>
            <artifactId>asm-util</artifactId>
            <version>3.2</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>net.sf.ezmorph</groupId>
            <artifactId>ezmorph</artifactId>
            <version>1.0.6</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-lang</groupId>
                    <artifactId>commons-lang</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.16</version>
            <exclusions>
                <exclusion>
                    <groupId>xerces</groupId>
                    <artifactId>xercesImpl</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.2.1</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>commons-codec</groupId>
                    <artifactId>commons-codec</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.groovy.modules.http-builder</groupId>
            <artifactId>http-builder</artifactId>
            <version>0.6</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-lang</groupId>
                    <artifactId>commons-lang</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.codehaus.groovy</groupId>
                    <artifactId>groovy</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>commons-collections</groupId>
                    <artifactId>commons-collections</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>commons-beanutils</groupId>
                    <artifactId>commons-beanutils</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy</artifactId>
            <version>1.8.5</version>
        </dependency>
        <dependency>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
            <version>1.3.04</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>xml-resolver</groupId>
            <artifactId>xml-resolver</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.gmaven</groupId>
                <artifactId>gmaven-plugin</artifactId>
                <version>1.5</version>
                <configuration>
                    <providerSelection>1.8</providerSelection>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>generateStubs</goal>
                            <goal>compile</goal>
                            <goal>generateTestStubs</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>attached</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Sorry this was so painful... right now this is about the only way to do it. The confluence version of this plugin handles this sort of thing much better - eventually these changes will make their way to the jira plugin.

At least I finally figured it out. Like they say... it's easy when you know how. :)

I did this by unzipping the jar and packaging some extra jars/classes in there. Probably not the best way but works.

Hi Harry - I did figure out that if I went into my .groovy/grapes folder, I could find the groovyx.net.http.jar. If I unzipped it, pulled out the whole package path (don't know the right terminology) and dropped it in a folder in my classpath, I get a different set of "no class def errors". If I am reading it correctly, it appears that the http module has its own set of dependencies. I might be able to keep repeating this process until I find everything, but there has to be a "right" way to do this...
0 vote

It's not very simple in OSGi world. You might want to put the jar in WEB-INF/lib, and try that. You should also exclude groovy itself when Grabbing http-builder.

The complete list of dependencies is below.

[INFO] The following files have been resolved:
[INFO]    antlr:antlr:jar:2.7.7:compile
[INFO]    asm:asm:jar:3.2:compile
[INFO]    asm:asm-analysis:jar:3.2:compile
[INFO]    asm:asm-commons:jar:3.2:compile
[INFO]    asm:asm-tree:jar:3.2:compile
[INFO]    asm:asm-util:jar:3.2:compile
[INFO]    com.google.appengine:appengine-api-1.0-sdk:jar:1.3.8:compile
[INFO]    commons-beanutils:commons-beanutils:jar:1.8.0:compile
[INFO]    commons-codec:commons-codec:jar:1.3:compile
[INFO]    commons-collections:commons-collections:jar:3.2.1:compile
[INFO]    commons-io:commons-io:jar:1.4:test
[INFO]    commons-lang:commons-lang:jar:2.4:compile
[INFO]    commons-logging:commons-logging:jar:1.1.1:compile
[INFO]    junit:junit:jar:4.5:test
[INFO]    log4j:log4j:jar:1.2.15:test
[INFO]    net.sf.ezmorph:ezmorph:jar:1.0.6:compile
[INFO]    net.sf.json-lib:json-lib:jar:jdk15:2.3:compile
[INFO]    net.sourceforge.nekohtml:nekohtml:jar:1.9.9:compile
[INFO]    oauth.signpost:signpost-commonshttp4:jar:1.2.1.1:compile
[INFO]    oauth.signpost:signpost-core:jar:1.2.1.1:compile
[INFO]    org.apache.httpcomponents:httpclient:jar:4.0.3:compile
[INFO]    org.apache.httpcomponents:httpcore:jar:4.0.1:compile
[INFO]    org.codehaus.groovy:groovy:jar:1.7.12-SNAPSHOT:compile
[INFO]    xerces:xercesImpl:jar:2.8.1:compile
[INFO]    xml-resolver:xml-resolver:jar:1.2:compile

I still can't make this work. Just today I've tried:

  1. Compiling my class to a jar file by itself, and placing it in the WEB-INF/lib. Result - java.lang.NoClassDefFoundError: groovy/lang/GroovyObject
  2. Compiling my class to a jar file with all dependencies, and placing it in the WEB-INF/lib. Result - couldn't start JIRA, SEVERE error in catalina log file (forgot to capture it).
  3. Compiled my class to a jar file with every dependency except the ones that caused JIRA to fail to load (groovy and commons.logging). Result - java.lang.NoClassDefFoundError: groovy/lang/GroovyObject
  4. Compiled my class to a .CLASS file and placed it in the appropriate WEB-INF/classes subfolder. Result - java.lang.NoClassDefFoundError: groovy/lang/GroovyObject
  5. Placed my class.groovy file in the appropriate WEB-INF/classes subfolder. Result - same error listed in the original post
  6. Added the following to the top of my groovy file:
    @Grapes([
    @Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.6' ),
    @GrabExclude('xml-apis:xml-apis'),
    @GrabExclude('org.codehaus.groovy:groovy')
    ])

    Result - java.lang.NoClassDefFoundError: org/apache/ivy/core/settings/IvySettings


    The problem is that I'm a complete noob when it comes to Java/Groovy and all the associated claptrap (maven, etc.). I just don't know nearly enough about what I'm doing to make an educated guess as to what to do next, or even where to look to figure it out. Any help or advice would be appreciated.

I decided to try something that should have been extremely simple. I made this class:

package com.plasmatherm.packagetest

class TestClass {
    int squareInt(int x) {
        return x*x
    }
}

I compiled it into a JAR using this pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!--
    Generated from archetype; please customize.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.plasmatherm.packagetest</groupId>
    <artifactId>PackageTest</artifactId>
    <name>Example Project</name>
    <version>1.0-SNAPSHOT</version>

    <dependencies>

        <dependency>
            <groupId>org.codehaus.groovy.maven.runtime</groupId>
            <artifactId>gmaven-runtime-1.6</artifactId>
            <version>1.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.codehaus.groovy</groupId>
                    <artifactId>groovy-all-minimal</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>1.8.5</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

Continued Next Comment...

<build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.gmaven</groupId>
                <artifactId>gmaven-plugin</artifactId>
                <version>1.5</version>
                <configuration>
                    <providerSelection>1.8</providerSelection>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>generateStubs</goal>
                            <goal>compile</goal>
                            <goal>generateTestStubs</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

NOTE - just learning maven, if something looks strange here, please tell me.

I put the jar file in WEB-INF/lib. I created a groovy script and tied it to a postfunction:

import org.apache.log4j.Level
import org.apache.log4j.Logger
import com.plasmatherm.packagetest.TestClass

def Logger log = Logger.getLogger('com.plasmatherm.TestClass')
log.setLevel(Level.DEBUG)
log.debug 'BEGIN TestClass'
log.debug 'Classpath is: ' + System.getProperty('java.class.path').toString()
def theSquare = new TestClass()
log.debug 'The square of 4 is: ' + theSquare.squareInt(4)

Very simple, right? The problem is, it doesn't work. I just get a "java.lang.NoClassDefFoundError: groovy/lang/GroovyObject".

Any idea what I can try next?

Thanks,

Hi all -- noob here. We recently upgraded from 5.0.7 to 6.2 without considering the Groovy scripts we've got in place (that we did not develop.)

Notably we have a script that is trying to include groovyx.net.http.* as well groovyx.net.http.ContentType.* and groovyx.net.http.Method.*, which I quickly found out are not included in the standard Groovy Runner 2.1.7 plugin.

A little digging found that the 2.0.7 version of the plugin we had has a whole mess of extra libraries, notably the missing http-builder-0.6.jar file.

Is there a good HOWTO somebody can point me to on how to properly add this jar to the newer 2.1.7 plugin? Thanks!

They weren't there previously... someone must have added them. I know of a few people that did that.

Consider upgrading to 3.0-beta, which does include them, you can get it from here: https://jamieechlin.atlassian.net/wiki/display/GRV/Upgrading+to+3.0

Ah, thanks for the tip Jamie. I ended up just unjarring the old and new plugins, copying over the missing libraries and rejarring it. Seemed to work.

We'll try 3.0 on our Dev box.

Hi Darry, We have exactly the same issue. Could you succeed in making this work by unjarring the old and new plugins, copying over the missing libraries? Please let me know. Cheers, Priya

Hi Priya -- yes, getting the missing libraries from the old plugin and copying them into the new and rejarring did work.

It will work fine but I recommend you use @Grab. http://groovy.codehaus.org/Grape

Thanks Darry and Jamie. I shall give it a go. Cheers, Priya

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Jira

Mission-critical battery manufacturer fulfills FAA software requirements with Commit Policy Plugin

EaglePicher Technologies is a leading manufacturer of battery systems for diverse industries like defense, aviation, space or medical. As they operate in highly regulated industries, keeping a clear ...

169 views 0 2
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you