How to resolve external dependencies for a Plugin

I have a question how to resolve external dependencies for a Plugin.

I have a Custom Keyword named com.kazurayam.ksbackyard.ScreenshotDriver. This enables you to take full-page screenshots of web pages. Another post How to package Custom Keyword as Plugin showed us how to make a jar of custom keywords. Following this instruction, I could make a jar which contains the ScreenshotDriver class.

My ScreenshotDriver class depends on an external Java library aShot. How can I resolve the external dependency when I install the Plugin into another Katalon Project? Does Katalon 6.x.x resolves the dependencies to aShot automatically? Or do I have to download the jar of aShot from Maven Central and deploy it into the Drivers directory in into the project?

2 Likes

You have to add dependencies to build.gradle the same way you use the Gradle task katalonCopyDependencies.

Any Example?

1 Like

I’ll add one in a few hours.

Hi @kazurayam,

Please add this script in build.gradle file to embed your custom keyword plugin dependencies into the execution classpath:

jar {
    from fileTree("$buildDir/dependencies/")
}

task extractDependencies(type: Sync) {
    dependsOn configurations.compile

    from (configurations.compile.collect {
        zipTree(it)
    })

    exclude 'META-INF/**'

    into "$buildDir/dependencies/"
}

jar.dependsOn extractDependencies
3 Likes

@duyluong’s solution is one way to achieve this.

If your dependencies are on Maven Central already, you can add them to build.gradle. See the following example:

There is another benefit. Your dependencies will be repackaged in order to avoid conflicts with other plugins as much as possible.

I tried this, but I could not find any build/dependencies folder created. No jar file, of course.

In the build.gradle file, I have do not have

dependencies {
    compile xxxx xxxx xxxx
}

The aShort jar file is located in the <projectDir>/Drivers folder. How can a gradle build process know where the aShot.jar is there?

1 Like

@devalex88,

Sorry, I do not see what you mean. In the example code, where do you have your libraries on Maven Central described?

1 Like

I’ve changed it to “dependencies” e.g. AShot. I used Zip4J in the above example.

I added

dependencies {
    // https://mvnrepository.com/artifact/ru.yandex.qatools.ashot/ashot
    compile group: 'ru.yandex.qatools.ashot', name: 'ashot', version: '1.5.4'

and executed

$ gradle katalonPluginPackage

Then, a huge size of jar file was created. 6.8MB. It contained all of classes including Groovy, WebDriver etc. It seems that all of the class files in the project.configurations.compile are included in the jar. I do not think this is a favorable result.

1 Like

It’s because AShot depends on Selenium, i.e Selenium is a transitive dependency. Since Katalon already contains Selenium, this dependency should be excluded.

dependencies {
  compile 'ru.yandex.qatools.ashot:ashot:1.5.4'
}

configurations {
  compile.exclude group: 'org.seleniumhq.selenium'
}

Thank you for your response. I will try.

1 Like

@devalex88

It worked fine.

Here I copy&pasted my build.gradle file for my ksbackyard project.

plugins {
    id 'java'
    id 'groovy'
    id 'com.github.johnrengelman.shadow' version '4.0.4'
    id "com.katalon.gradle-plugin" version "0.0.6"
}

ext {
    groovyVersion   = '2.4.7'
}

configurations {
    generateDocs

    compile.exclude group: 'org.seleniumhq.selenium'
    compile.exclude group: 'commons-io'
    compile.exclude group: 'com.google.code.gson'
    compile.exclude group: 'org.hamcrest'
    compile.exclude group: 'junit'
    compile.exclude group: 'org.mockito'
}

repositories {
    jcenter()
    mavenCentral()
}

dependencies {
    // https://mvnrepository.com/artifact/ru.yandex.qatools.ashot/ashot
    compile group: 'ru.yandex.qatools.ashot', name: 'ashot', version: '1.5.4'

    generateDocs "org.codehaus.groovy:groovy-all:${groovyVersion}"
}

sourceSets {
    main {
        groovy {
            srcDirs = ['Keywords', 'Include/scripts/groovy']
            srcDir 'Libs'
        }
    }
}

shadowJar {
    exclude 'Temp*.class'
}

katalon {
    dependencyPrefix = "com.katalon"
    minimize = false
}

task groovydoc(type: Groovydoc, overwrite:true) {
    source = sourceSets.main.groovy
    classpath = configurations.compile
    groovyClasspath = project.configurations.generateDocs
    include 'com/kazurayam/ksbackyard/*'
    exclude '**/*Test.groovy'
}
task publishGroovydoc(type: Copy) {
    from 'build/docs/groovydoc'
    into 'docs/api'
}
groovydoc.finalizedBy publishGroovydoc

2 Likes

Great news! We are looking forward to your awesome Keywords :smile:.

One minor point: this should be your namespace (com.kazurayam) so that AShot will be repackaged under com.kazurayam and will not clash with other plugins that use different versions of AShot.

OK,

I did not see what

katalon {
    dependencyPrefix="...."

is for. Now I understand it.

1 Like

This post really save my life :smiley: