Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

SeaLights build scanner (sl-build-scanner.jar) can be used to update build.gradle script with the changes needed to run your Gradle build using SeaLights Gradle plugin in a similar fashion , similarly as for Maven pom integration.

Note

Automatic integration of Sealights settings into your build.gradle script is currently supported only for Groovy language.
If your script is written in Kotlin, please use the method described in the following article: Gradle - Scanning Builds and Tests using Sealights plugin

Configuration file

Create a JSON configuration file with the following parameters to provide the necessary configuration fields to the SeaLights Gradle plugin:

...

Code Block
languagejson
{
  "executionType": "fullscanonly",
  "tokenFile": "./sltoken.txt",
  "createBuildSessionId": true,
  "appName": "${System.getenv('JOB_NAME')}",
  "branchName": "master",
  "buildName": "${System.getenv('BUILD_NUMBER')}",
  "packagesIncluded": "*com.example.*",
  "packagesExcluded": "",
  "filesIncluded": "*.class",
  "filesExcluded": "*test-classes*",
  "recursive": true,
  "includeResources": true,
  "testTasksAndStages": {"test":"Unit Tests",
"junitPlatformTest":"Unit Tests", "integrationTest":"Integration Tests"},
  "labId": null,
  "logEnabled": false,
  "logDestination": "console",
  "logLevel": "off",
  "logFolder": "/tmp",
  "gradleProjectConfig": {
    "excludedProjects": ["subProject1", "subProject2"],
    "repoConfig": "mavenLocal()"
  }
}

...

Note
  • Do not call the JSON file 'sealights.json' as the agent uses this file name for override options

  • For troubleshooting purpose,  you can use "buildName":"SL_Timestamp" in your JSON file to have the Sealights Gplugin generating automatically a time stamp (yyyy.MM.dd-hh.mm format) as a default buildname

Integrating into the Build.gradle script

Before running your Gradle build, you run the build scanner with -gradle flag to integrate the SeaLights Gradle plugin into the build.gradle file. This will create a sealights.gradle script with all the configuration taken from the JSON config file and append a reference to it in build.gradle.

The standard parameters the build scanner receives are:

  • configfile - The path to the JSON configuration you created with the parameters to be provided to the SeaLights Gradle plugin

  • workspacepath - The base path to the location of the build.gradle file to update

Code Block
languagebash
java -jar sl-build-scanner.jar -gradle -configfile sl-config.json -workspacepath .

...

Note

This step will update the root build.gradle file and back it up to build.gradle.slbackcopy in the same directory
If your workspace is not reset for each build, you will need to restore them at the end of the run (see command below).

Next The next step is to run your regular Gradle build command, e.g. gradle clean buildbuild’.

Restoring the build.gradle file to its previous state

In case If the project is to be restored to its previous state before the SeaLights plugin was applied, use the build scanner with the -restoreGradle flag on the workspace where the gradle.build file is located:

...

This command will (1) restore gradle.build from its backup, (2) delete the backup file, and (3) delete the sealights.gradle file. 
Failure at any of these steps won’t prevent the next ones , so that the project is restored as much as possible.

Configuring SCM for Build Scan

SeaLights, by default, provides all links to the SCM for Github. You can configure SeaLights to prepare the links for Bitbucket and Gitlab as well with the following parameters placed under the sealightsJvmParams section:

  • sl.scm.provider - set to github, Bitbucket or gitlab

  • sl.scm.baseUrl - When working with an on-premise installation of your SCM and access from the build machine is different than the one accessed by the users, then you can provide the base URL to use

  • sl.scm.version - set to the version of the on-premise version you use

Code Block
"sealightsJvmParams": {
    "sl.scm.provider": "bitbucket",
    "sl.scm.baseUrl": "https://my.bitbucket.com/projects/ABCD/repos/XYZ/browse/A1",
    "sl.scm.version": "4.9.0"
}

Pre-downloading the agents

The Gradle plugin downloads the recommended agent at the beginning of the run. If you want to pre-download them and provide them to the plugin, you can do so with the flags scannerJarlistenerJar.

...

Code Block
"scannerJar": "./sl-build-scanner.jar"
"listenerJar": "./sl-test-listener.jar"

Using a local repo

The Gradle plugin, by default, is downloaded from Maven Central. If you want to update the repo it downloads from, you can do so using the gradleProjectConfig->repoConfig option

...

Code Block
"gradleProjectConfig": {"repoConfig": "maven{\nurl \"https://artifactory.company.com/artifactory/remote-repos\"\ncredentials {\nusername = \"USERNAME\"\npassword = \"PASSWORD\"\n}\n}"}

Sample shell script 

Info

This script is very often added to a new pre-build step in your CI configuration (i.e. Jenkins).

Code Block
languagebash
echo "Downloading Sealights Agents..."
wget -nv https://agents.sealights.co/sealights-java/sealights-java-latest.zip
unzip -o sealights-java-latest.zip
echo "Sealights agent version used is:" `cat sealights-java-version.txt`

echo  '{ 
    "executionType": "scanonly",
    "tokenFile": "sltoken.txt", 
    "createBuildSessionId": true,
    "appName": "${System.getenv(\"JOB_NAME\")}",   
    "branchName": "${System.getenv(\"GIT_BRANCH\")}",
    "buildName": "SL_Timestamp",
    "packagesIncluded": "*com.example.*",   
    "packagesExcluded": "",
    "filesIncluded": "*.class",
    "filesExcluded": "*test-classes*",
    "recursive": true,
    "includeResources": true,
    "proxy": null,
    "logEnabled": false,
    "logDestination": "console",
    "logLevel": "off",
    "logFolder": "/tmp/sl-logs",
    "scannerJar": "./sl-build-scanner.jar",
    "listenerJar": "./sl-test-listener.jar",
    "sealightsJvmParams": {
      "sl.scm.provider": "bitbucket"
    }
  }' > sl-config.json
 
echo "Updating Build.gradle with Sealights..."
java -jar sl-build-scanner.jar -gradle -configfile sl-config.json -workspacepath .

...