Fullmenu null

 

14 November 2017

In this article we will discuss how we can group our scripts in a jar, in this way that are accessible to the team (or the world) in a comfortable and simple way.

As you can see, in the rest of the articles we treat scripts that fulfill a function (search for files, transform data, invoke web services, etc.) but the situation may arise that all, or part of them, are useful for certain users in different situations and we want you to be able to use them either by executing them directly or as a basis for other scripts.

For example, we could have the following situations:

  • Direct utility scripts

  • Base scripts intended to be customized (extended) by others

  • Scripts with business logic that we are interested in hiding.

  • Scripts that depend on others for their correct execution

  • etc

Shared directory

The first solution that is presented to us to be able to reuse our scripts is to locate them in a shared folder, either in a network folder for example SAMBA or in a local folder of a machine that we can access.

The advantage of this solution obviously is the simplicity with the addition that if this folder is versioned (SVN, Git, etc) it is very easy to have it updated.

Obviously a disadvantage of these scripts is that they will be restricted to running in the environment of this machine.

For example we have a script that scans a directory looking for a file determined to work with him it would only make sense to search it in this machine and not in the our for example.

Hosted in SVN / Git

Another option is to have a SCM (source control manager) like SVN, git or similar where users can clone the project on their machines and run it.

Simply running update commands such as git pull we can refresh the local directory with the latest version and we could execute the command.

However, we need to install version control on the machine where we want to execute the scripts, we should check that we do the pull in a usual way etc.

HTTP Server

Thanks to Groovy’s ability to execute scripts in remote URLs that are hosted on a web server (Apache, Nginx, or similar) and that the users can invoke via http

groovy http://groovy-lang.gitlab.io/101-scripts/scripts/office/ExtractPdf.groovy https://www.boe.es/boe/dias/2017/09/21/pdfs/BOE-B-2017-54046.pdf

As we can see in this example we just need to have Groovy installed and run scripts that reside in a web server at the same time that we pass arguments

Jar

Using Groovy’s ability to run scripts contained in a zip/jar we can group our files with a simple command:

java cvf mis_scripts.jar *.groovy //(1)
groovy jar:file:mis_scripts.jar'!'/FindFile.groovy  //(2)
  1. We package our scripts in a jar (what is being a zip)

  2. Through this URI we can locate our script inside the jar and thus be able to execute it

Publishing Jar

If we have a Maven server (or an account in MavenCentral, Bintray, etc) we can also publish this jar to be able to use it through @Grape in other scripts.

In this section we will explain how to prepare a Gradle project that allows us to generate the Jar and publish it in Bintray so that the rest of our scripts can use it (you must have an account created in Bintray)

Directories structure

It establishes a structure of directories like that of the image. Our scripts will be located in the resources directory

+-------------+
| mis_scripts |
+-------------+
  |
  | +-----+
  +>| src |
    +-----+
       |    +------+
       +--->| main |
            +------+
               |    +-----------+
               +--->| resources |
                    +-----------+

Gradle

In the root directory mis_scripts copy the following file:

build.gradle
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
    }
}

apply plugin: 'groovy'
apply plugin: 'java-library-distribution'
apply plugin: 'maven'
apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.bintray'

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.4.12'
    testCompile 'junit:junit:4.12'
}

javadoc {
    title = "$project.description $project.version API"
}

task sourceJar(type: Jar, dependsOn: classes) {
    classifier = 'sources'
    from sourceSets.main.allSource
}

task javadocJar(type: Jar) {
    classifier = "javadoc"
    from javadoc
}

distZip.shouldRunAfter(build)
publishing {
    publications {
        maven(MavenPublication) {
            artifactId 'mis-scripts'  //(1)
            from components.java
            artifact sourceJar {
                classifier "sources"
            }
        }
    }
}
bintray {
    user = System.getenv("BINTRAY_USER") ?: project.hasProperty("bintrayUser") ? project.bintrayUser : ''  //(2)
    key = System.getenv("BINTRAY_KEY") ?: project.hasProperty("bintrayKey") ? project.bintrayKey : ''
    publications = ['maven']
    publish = true
    pkg {
        repo = "mi-repositorio"   // (3)
        userOrg = project.hasProperty('userOrg') ? project.userOrg : 'mi-organization'  //(4)
        name = "mis-scripts"
        desc = "Groovy Scripts"
        websiteUrl = "https://tuwebsite.com"
        licenses = ['Apache-2.0']
        publicDownloadNumbers = true
        version {
            name = project.version
        }
    }
}
  1. Indicate the name of the artifact you want to publish in Bintray

  2. User/token configuration by environment variable, gradle.properties or default value

  3. The name of the repository where you want to publish in Bintray (you must have created it before in Bintray)

  4. The organization name (you must have created it before in Bintray)

Publishing

To package and publish your artifact simply execute:

gradle bintrayUpload

If all ik ok, your jar will have been created with the embedded scripts in it and it will have been uploaded to Bintray

"Grapeando"

Starting from here our device is accessible by any dependency manager such as Maven or Gradle and of course Grape so we can create new scripts that depend on our artifact simply putting the appropriate "coordinates" (repository, organization, artifact and version) in the script


Script
println "Hola mundo"