Skip to content

Gradle Build Scripts

Tom Evans edited this page Jan 30, 2023 · 5 revisions

Initialization Lifecycle Phase

Collapse

  1. settings.gradle:
  • Declares the configuration required to instantiate and configure the hierarchy of Project instances which are to participate in a build.

  • There is a one-to-one correspondence between a Settings instance and a settings.gradle settings file. Before Gradle assembles the projects for a build, it creates a Settings instance and executes the settings file against it.

  • Open Liberty's settings.gradle file loads properties from either ~/gradle.startup.properties or it loads the property file passed as an option -Dbuild.properties.path on an automated build. These are the expected properties (and typical values for a continuous integration build):

pass-thru-gradle.properties:

artifactory.download.server  = eu.artifactory.swg-devops.com
artifactory.download.token   = (token)
artifactory.download.user    = [email protected]
artifactory.upload.server    = na.artifactory.swg-devops.com
artifactory.upload.token     = (token)
artifactory.upload.user      = [email protected]
buildLabel                   = 20190704-1647
buildResultUUID              = _VAzRUJ5xEemn7JSbMpQERQ
dhe.password                 = (password)
dhe.user                     = [email protected]
disable.run.runUnitTests     = false
ghe.build.type               = Continuous
gradle-tasks.log             = /home/jazz_build/_VAzRUJ5xEemn7JSbMpQERQ-EBC.PROD.WASRTC-BT4-001-00-00/jbe/build/open-liberty/dev/gradle-tasks.log
gradle.log                   = /home/jazz_build/_VAzRUJ5xEemn7JSbMpQERQ-EBC.PROD.WASRTC-BT4-001-00-00/jbe/build/open-liberty/dev/gradle.log
gradle.opts                  = --stacktrace
gradle.properties.output.path= /home/jazz_build/_VAzRUJ5xEemn7JSbMpQERQ-EBC.PROD.WASRTC-BT4-001-00-00/jbe/build.launch/pass-thru-gradle.properties
intranet.id                  = [email protected]
intranet.password            = (password)
intranet.user                = [email protected]
junit.report.zip             = /home/jazz_build/_VAzRUJ5xEemn7JSbMpQERQ-EBC.PROD.WASRTC-BT4-001-00-00/jbe/build/open-liberty/dev/open-liberty.unitTest.results.zip
published.gradle.log         = /home/jazz_build/_VAzRUJ5xEemn7JSbMpQERQ-EBC.PROD.WASRTC-BT4-001-00-00/jbe/build/open-liberty/dev/tmp/gradle.log

Other properties that may be included:

artifactory.force.external.repo = true
is.personal              = true
is.continuous.build      = true
is.release               = true
buildLabelPrefix         =
is.public.publishing     = true
build.os.native.packages = true
openLibertyBuild.disable = true
dont.build.open.liberty  = true
  • Properties are loaded onto a Properties instance called gradle.ext.userProps. It is also referred to as gradle.userProps in build scripts that use it.
  • If the build is not using Artifactory, it will download Gradle plugins from https://plugins.gradle.org/m2/ and the default values for repository properties are set to:
is.using.artifactory      = false
fetch.oss.repository      = aQute.bnd.repository.maven.provider.MavenBndRepository;name = RemotePublic;releaseUrl=https://repo.maven.apache.org/maven2/;index=${build}/oss_dependencies.maven
fetch.oss.ibm.repository  = aQute.bnd.repository.maven.provider.MavenBndRepository;name = RemoteIBM;local=~/.ibmdhe/repository;releaseUrl=https://public.dhe.ibm.com/ibmdl/export/pub/software/olrepo/;index=${build}/oss_ibm.maven
fetch.ibm.java.repository = aQute.bnd.repository.maven.provider.MavenBndRepository;name = DummyRepo
push.ibm.repository       = aQute.bnd.repository.maven.provider.MavenBndRepository;name = RemotePublish
  • If all the expected Artifactory properties are set and artifactory.force.external.repo is false, the build configures itself to use Artifactory to download Gradle plugins and sets non-default values for the repository properties:
is.using.artifactory      = true
fetch.oss.repository      = aQute.bnd.repository.maven.provider.MavenBndRepository;name = RemotePublic;local=~/.ibmartifactory/repository;releaseUrl=https://eu.artifactory.swg-devops.com/artifactory/wasliberty-open-liberty;snapshotUrl=https://eu.artifactory.swg-devops.com/artifactory/wasliberty-open-liberty;index=${build}/oss_dependencies.maven
fetch.oss.ibm.repository  = aQute.bnd.repository.maven.provider.MavenBndRepository;name = RemoteIBM;local=~/.ibmartifactory/repository;releaseUrl=https://eu.artifactory.swg-devops.com/artifactory/wasliberty-open-liberty;snapshotUrl=https://eu.artifactory.swg-devops.com/artifactory/wasliberty-open-liberty;index=${build}/oss_ibm.maven
fetch.ibm.java.repository = aQute.bnd.repository.maven.provider.MavenBndRepository;name = IBMInternalJava;local=~/.ibmartifactory/repository;releaseUrl=https://eu.artifactory.swg-devops.com/artifactory/wasliberty-open-liberty;snapshotUrl=https://eu.artifactory.swg-devops.com/artifactory/wasliberty-open-liberty;index=${build}/ibm_java.maven
push.ibm.repository       = aQute.bnd.repository.maven.provider.MavenBndRepository;name = RemotePublish;releaseUrl=https://na.artifactory.swg-devops.com/artifactory/wasliberty-open-liberty;snapshotUrl=https://na.artifactory.swg-devops.com/artifactory/wasliberty-open-liberty;index=${build}/openliberty.maven

1.1. wlp-gradle/propertiesSettings.gradle:

  • Additional properties are set to gradle.ext.userProps. The properties from generated.properties are set if the file exists.
  • If these properties were not already set, then they are set (with these default values):
is.automated.build       = false
is.personal              = false
is.continuous.build      = false
is.release               = false
is.public.publishing     = false
disable.run.runUnitTests = false
build.os.native.packages = false
javac.bootclasspath.1.4  = (varies by environment)
javac.bootclasspath.1.6  = (varies by environment)
javac.bootclasspath.1.7  = (varies by environment)
javac.bootclasspath.1.8  = (varies by environment)
version.qualifier        = (timestamp with the format yyyyHHddHHmm)
buildLabel               = (timestamp with the format yyyyHHddHHmm)
  • If the build is using Artifactory, then these properties are set with non-default values:
javac.bootclasspath.1.4 = ${internaljavac.bootclasspath.1.4}
javac.bootclasspath.1.6 = ${internaljavac.bootclasspath.1.6}
javac.bootclasspath.1.7 = ${internaljavac.bootclasspath.1.7}
javac.bootclasspath.1.8 = ${internaljavac.bootclasspath.1.8}

1.2. wlp-gradle/bndSettings.gradle:

  • This file is responsible for including all the Project instances to the Gradle build. See include for more information on how Project instances are added to the Gradle build.
  • First, it configures bnd plugin dependencies to the bndSettings.gradle build script and rootProject build script instances. That enables bndSettings.gradle to initialize using bnd classes and rootProject to apply the biz.aQute.bnd plugin to other projects.
  • Then it instantiates a aQute.bnd.build.Workspace instance and sets each property in gradle.userProps to a Properties instance on the Workspace. This is so we can use the properties in any .bnd file in the workspace. Specifically, we heavily use the properties in cnf/build.bnd and other files included by that. More on these files soon.
  • Lastly, the "cnf" project is included in the Gradle build, along with "build.image", "wlp-bndPlugins", "wlp-rasInstrumentation", and any project returned by workspace.getAllProjects(). If a project returned by workspace.getAllProjects() includes a sub directory containing a springboot.gradle build script, that directory is included as a project in the Gradle build too.
  • The Workspace initialized here is named rootProject.ext.bndWorkspace, so any Project instance may reference it as rootProject.bndWorkspace.
  • The collection of bnd project names is named rootProject.ext.gradleBndProject, so Project instances that need to know about projects in the entire workspace may reference that as rootProject.gradleBndProject.

1.2.1 cnf/build.bnd

  • More to come.

1.3. wlp-gradle/buildCacheSettings.gradle:

Configuration Lifecycle Phase

Collapse

  1. build.gradle:
  • There is a one-to-one relationship between a Project and a build.gradle file. The rootProject is configured by this build.gradle file with the name "dev". Any project in the hierarchy can refer to it by the rootProject property. Because of this, we set some commonly used extra properties onto the rootProject. cnf is a reference to the Project named "cnf". buildImage is a reference to the Project named "build.image". storeProps is a named function to write some of the properties determined at build time to a file named generated.properties for future builds.
  • Throughout the remainder of the configuration lifecycle phase, keep in mind that tasks may have doFirst or doLast code configured. doFirst and doLast code will be configured, but not executed during this phase.

2.1. wlp-gradle/java.gradle:

  • Each sub project of the rootProject has the java plugin applied to it, adding many of the common development tasks to these projects.
  • This build script makes various configuration tweaks on tasks applied by the java plugin, such as test tasks are configured to ignore failures. JavaCompile type tasks set source and target compatibility to "1.7" if they would have been build with "1.6" and the JVM is compatible with Java 12, and if the JVM is compatible with Java 9 or higher, they add a --release argument to the compiler. Lastly, Javadoc type tasks add a Xdoclint:none and -quiet option if the JVM is compatible with Java 9.
  • The build script adds the testReport and testResults tasks to generate a HTML test report of all unit test results.

2.2. wlp-gradle/biz.aQute.bnd.gradle:

  • This build script is responsible for applying plugins to bnd Projects (or projects that have a .bnd file). That is determined by selecting projects for which bndWorkspace.getProject(name) does not return null.
  • First, the biz.aQute.bnd plugin is applied. Doing that adds release, releaseNeeded and several other development tasks commonly used in the build.
  • Next, the build scripts in wlp-gradle/subprojects are applied as described below.
  • Lastly, if a bnd Project does not have the bnd properties fat.project and test.project set to true, and does not set -nobundles, then it is considered a runtime project and the net.ossindex.audit plugin is applied, causing the the audit task to be added.

2.2.1. wlp-gradle/subprojects/repos.gradle

2.2.2. wlp-gradle/subprojects/publish.gradle

  • Applies the maven-publish plugin, which allows the use of publishing blocks and the publish development task, so projects' build scripts can declare additional artifacts to upload into the local cnf/release Maven repository.

2.2.3. wlp-gradle/subprojects/anttaskdefs.gradle

  • This build script runs ant.taskdef for several Ant tasks built in the workspace so that they can be used by Gradle tasks.

2.2.4. wlp-gradle/subprojects/tasks.gradle

  • Configure 'globalize', 'copyPiiFiles', 'verifyBundleNLS', 'compileJava', 'test', 'apiSpiJavadoc', and 'zipJavadoc' tasks.

2.2.5. wlp-gradle/subprojects/jacoco.gradle

  • This build script applies the Jacoco plugin, which adds the jacocoTestReport task, and that is configured to depend on test tasks and generate an HTML report.

2.2.6. wlp-gradle/subprojects/assemble.gradle

  • Create 'publishWLPJars', 'publishJavadoc', 'publishToolScripts', 'publishToolJars', 'publishSchemaResources', 'publishPlatformManifests', 'publishPlatformFiles', 'publishTemplates', 'publishBinScripts', 'publishClientScripts', and 'publishLibNative' tasks. Configure 'assemble' to depend on above tasks.

2.2.7. wlp-gradle/subprojects/fat.gradle

  • Configure 'buildfat', 'runfat', 'buildandrun', and 'cleanFat' tasks on FAT projects.

Execution Lifecycle Phase

Collapse

  • When the build reaches a task to execute, it runs doFirst code, executes, then runs doLast code.

  • The build executes tasks in the order they were configured by the plugin's defaults and then the task's dependsOn property. The build skips executing UP-TO-DATE, FROM-CACHE, and SKIPPED tasks.

    • UP-TO-DATE and FROM-CACHE tasks are determined by the inputs and outputs of a task. These inputs/outputs can be files or properties. If the inputs and outputs have same hash values as stored in ~/.gradle/caches or a remote Gradle cache, then the tasks will be marked FROM-CACHE. If the outputs are already built, the tasks will be marked UP-TO-DATE.
    • SKIPPED tasks are determined by a task's enabled property. If the property evaluates to false, the task is skipped.

Development Tasks

[project:]taskName - TaskType
Depends on: List of tasks this task depends on
Description of this task.
cnf:initialize
Depends on: cleanRepos, copyMavenLibs, and updatePluginClasses
Aggregate task that deletes "cnf/release", "cnf/release.mvn", "cnf/local", and "cnf/local.mvn", copies several libraries into "cnf/mavenlibs", and builds "wlp-bndPlugins" into "cnf/bndplugins/classes" and refreshes the bnd Project instances.
This must be run once after a clone, clean, or checking out a new branch.

Java Plugin

Figure 1. Java plugin - tasks Figure 1. Java plugin - tasks

compileJava - JavaCompile
Depends on: All tasks which contribute to the compilation classpath, including jar tasks from projects that are on the classpath via project dependencies
Compiles production Java source files using the JDK compiler.
jar - Jar
Depends on: classes
Assembles the production JAR file, based on the classes and resources attached to the main source set.
javadoc - Javadoc
Depends on: classes
Generates API documentation for the production Java source using Javadoc.
compileTestJava - JavaCompile
Depends on: classes, and all tasks that contribute to the test compilation classpath
Compiles test Java source files using the JDK compiler.
test - Test
Depends on: testClasses, and all tasks which produce the test runtime classpath
Runs the unit tests using JUnit or TestNG.
testReport - TestReport
Depends on: test tasks of all subprojects included in the build
Generates an HTML test report from the results of one or more Test tasks.
testResults
Depends on: testReport, and test tasks of all subprojects
Aggregate task to create a test report for all subprojects.
assemble
Depends on: jar, and all other tasks that create artifacts attached to the archives configuration
Aggregate task that assembles all the archives in the project. This task is added by the Base Plugin — which the Java Plugin applies automatically.

Jacoco plugin

jacocoTestReport - JacocoReport
Depends on: test
Task to generate HTML, Xml and CSV reports of Jacoco coverage data.

OSSIndex plugin

audit
Audits a gradle project using the OSS Index REST API v3 to identify known vulnerabilities in its dependencies.

Maven Publish plugin

publish
Depends on: All publishPubNamePublicationToRepoNameRepository tasks
An aggregate task that publishes all defined publications to all defined repositories. It does not include copying publications to the local Maven cache.

biz.aQute.bnd plugin

release
Depends on: jar, and all other tasks that create artifacts attached to the archives configuration
Releases the project's bundles to the -releaserepo, if one is configured for the project.
releaseNeeded
Depends on: jar, and all other tasks that create artifacts attached to the archives configuration
Releases the project and all projects it depends on.
bndproperties
Display the Bnd properties of the project.

Custom tasks

cleanFat - Delete
Deletes the build/autoFVT directory of a project.
buildfat
Depends on: cleanFat, assemble, build, zipProjectFVT
Aggregate task that deletes the build/autoFVT directory of a project, assembles, builds, and creates a zip of the project's autoFVT bucket.
runfat - Exec
Depends on: cleanBeforeRun
Aggregate task that assembles all the archives in the project. This task is added by the Base Plugin — which the Java Plugin applies automatically.
buildandrun
Depends on: buildfat, runfat
Aggregate task that runs builds and runs a FAT.

Internal tasks

cnf tasks

copyMavenLibs - Copy
updatePluginClasses - Copy
cleanRepos - Delete
everythingElseHasBeenReleased
createGradleBootstrap
createESALists
createGeneratedReplacementProjects
index - Index
createIndex
zipGradleBootstrap - Zip
libertyReleaseVersions
printProjectDependencies
checkForUpdates
updateArtifact

NLS tasks

globalize
copyPiiFiles - Copy
verifyBundleNLS

Assemble tasks

publishWLPJars - Copy
apiSpiJavadoc - Javadoc
zipJavadoc - Zip
publishJavadoc - Copy
publishToolScripts - Copy
publishToolJars - Copy
publishSchemaResources - Copy
publishPlatformManifests - Copy
publishPlatformFiles - Copy
publishTemplates - Copy
publishBinScripts - Copy
publishClientScripts - Copy
publishLibNative - Copy

FAT tasks

cleanBeforeRun - Delete
addRequiredLibraries - Copy
addDerby - Copy
copyFeatureBundles
autoFVT
zipAutoFVT - Zip
zipProjectFVT - Zip

build.image tasks

copyPropertiesToBuildImage - Copy
copyReadmeToBuildImage - Copy
copyLicenseToBuildImage - Copy
copySwidTagToBuildImage - Copy
packageOpenLiberty - PackageLibertyWithFeatures
packageOpenLibertyKernel - PackageLibertyWithFeatures
packageOpenLibertyWebProfile8 - PackageLibertyWithFeatures
packageOpenLibertyJavaee8 - PackageLibertyWithFeatures
packageOpenLibertyMicroProfile2 - PackageLibertyWithFeatures
packageOpenLibertyMicroProfile3 - PackageLibertyWithFeatures
zipOpenLiberty - Zip
zipOpenLibertyKernel - Zip
zipOpenLibertyWebProfile8 - Zip
zipOpenLibertyJavaee8 - Zip
zipOpenLibertyMicroProfile2 - Zip
zipOpenLibertyMicroProfile3 - Zip
zipOpenLibertyAll - Zip
createOLRuntimePoms
zipOpenLibertyMaven - Zip
zipTestReport - Zip
createJSONForPublicArtifacts
gatherTestResults
copyOpenLiberty - Copy
updateSpecTemplate - Copy
updateChangelogTemplate - Copy
updateBuildPackageScriptTemplate - Copy
updateRulesTemplate - Copy
buildOsNativePackages - Exec

Helper Tasks

help
tasks [--all]
wrapper
properties
model
Clone this wiki locally