4

When try to create AntBuidler object in groovy file I am getting below exception

java.lang.NoClassDefFoundError: org/apache/tools/ant/BuildException
at java.lang.Class.getDeclaredConstructors0(Native Method)
  at java.lang.Class.privateGetDeclaredConstructors(Class.java:2493)
  at java.lang.Class.getDeclaredConstructors(Class.java:1901)
  .....
  at at features.step_definitions.RewardEventsGeneration.GetEventXML(RewardEventsGeneration.groovy:40)

at ✽.Then updateLoyaltyInfo event should be generated

I have added relevant jar to my lib folder and then placed below code under the build.gradle

repositories {
    mavenCentral()
    flatDir {
        dirs 'lib'
    }
}

My code as below

def GetEventXML (userId, eventTypeIn)
{
    def Host = "10.77.69.14"
    def UserName = "system"
    def Password = "password"
    def Path = "/temp"

    def eventTypeToLookFor = "eventType=\"$eventTypeIn\""
    def resultAsString = "" as String

    def commandToRun = "grep -lH $userId $Path/*.xml | xargs grep -l '$eventTypeToLookFor' | cut -d: -f1"
    def antEventCheck = new AntBuilder();   ********** Error line ******************


        antEventCheck.sshexec(  trust:'true',
                host:Host,
                username:UserName,
                password:Password,
                command:commandToRun,
                verbose:'true',
                timeout:'10000',
                failonerror:'false',
                outputproperty:'eventCheckResult');

        resultAsString = antEventCheck.properties.eventCheckResult.toString()  

    return resultAsString
}

build.gradle

dependencies {
    ext.groovyVersion = "2.0.4"
    ext.cucumberJvmVersion = "1.1.5"
    ext.httpclientVersion = "4.2.1"

    cucumberRuntime files("${jar.archivePath}")

    compile ('com.jcraft:jsch:0.1.49')   

    compile('com.github.groovy-wslite:groovy-wslite:0.8.0')
    groovy("org.codehaus.groovy:groovy-all:${groovyVersion}")
    compile("org.apache.httpcomponents:httpmime:4.1.2")
    compile("org.codehaus.groovy.modules.http-builder:http-builder:0.5.2") {
        exclude group: "org.codehaus.groovy", module: "groovy"
    }
    compile("net.sf.json-lib:json-lib:2.4:jdk15")
    compile("javax.mail:mail:1.4.5")
    compile("org.apache.httpcomponents:httpclient:${httpclientVersion}")
    compile("org.codehaus.geb:geb-core:0.7.2") {
        exclude group: "org.codehaus.geb", module: "geb-implicit-assertions"
    }

    drivers.each { driver ->
        testCompile "org.seleniumhq.selenium:selenium-$driver-driver:$version.selenium"
    }

    compile("org.seleniumhq.selenium:selenium-support:2.25.0")
    compile("log4j:log4j:1.2.17")

    testCompile("junit:junit:4.10")
    testCompile("info.cukes:cucumber-groovy:${cucumberJvmVersion}")
    testCompile("info.cukes:cucumber-junit:${cucumberJvmVersion}")
}

Appreciate your comments

Shabar
  • 2,617
  • 11
  • 57
  • 98
  • What's in the `dependencies` block? Is the Ant dependency declared there? – Peter Niederwieser Oct 06 '14 at 23:55
  • @PeterNiederwieser I didn't put anything under `dependencies `. Just added in the `build.gradle ` file under `repositories ` as I specified in the question as per `http://stackoverflow.com/questions/20700053/how-to-add-local-jar-file-dependency-to-build-gradle-file ` I could see the class file under my project `lib ` inside here `ant-1.7.0.jar --> \org\apache\tools\ant\BuildException.class ` – Shabar Oct 07 '14 at 00:06
  • Added `dependencies ` part of `build.gradle ` file to the question – Shabar Oct 07 '14 at 02:49
  • 1
    The build script is missing a dependency declaration for Ant (or the lib dir). Specifying a `flatDir` repository alone doesn't do anything. – Peter Niederwieser Oct 07 '14 at 05:57
  • @PeterNiederwieser I created `compile("ant:1.7.0") ` entry under `dependencies ` . Because that is the `jar ` file relating to complaining class in the error Restarted `intelliJ `. I am getting same error though. Is there anything else I need to do? – Shabar Oct 07 '14 at 23:12
  • Try from the command line first. I'm surprised this dependency declaration doesn't give an error (I'd expect you need at least `":ant:1.7.0"`). – Peter Niederwieser Oct 07 '14 at 23:25
  • Thax @PeterNiederwieser `compile("ant:ant:1.7.0") ` did work. – Shabar Oct 07 '14 at 23:25

1 Answers1

1

Following works perfectly

As specified by Peter's answer adding flatDir is not gonna be enough. Need to add same to the dependencies as well

repositories {
    mavenCentral()
    flatDir {
        dirs 'lib'
    }
}


dependencies {
    compile("ant:ant:1.7.0")
}

Thanks Peter

Shabar
  • 2,617
  • 11
  • 57
  • 98