Upgrading Brightspot Gradle Plugins
Goals
- Establish and maintain Gradle best practices for all Brightspot projects
- Improve build performance
- Simplify future upgrades
Automated upgrade
The brightspotGradleRewrite task does most of the steps in this guide.
- In
settings.gradle, set thecom.brightspot.gradleversion to 3.14.0 or higher, and remove thecom.brightspot.sonarline. - Fix the constructs below if the project has them. The build fails on them before the doctor can run.
- A
tasks.named('jakarta-substitution-war') { ... }block: delete it. The plugin has no such task, and the Jakarta migration already excludes*.zipfiles. - Version keys in
brightspot.versionsother thanjavaandproject: replace each one with its BOMs in thebrightspot.dependenciesblock, at the same version<v>:1// versions.brightspot = '<v>'2implementation enforcedPlatform('com.psddev:brightspot-bom:<v>')3implementation enforcedPlatform('com.psddev:brightspot-dependencies:<v>')45// versions.brightspotGo = '<v>'6implementation enforcedPlatform('com.brightspot.go:bom:<v>')7implementation enforcedPlatform('com.brightspot.go:go-dependencies:<v>')89// versions.componentLib = '<v>'10implementation enforcedPlatform('com.psddev.component-lib:bom:<v>')11implementation enforcedPlatform('com.psddev.component-lib:cl-dependencies:<v>')1213// versions.sharedTests = '<v>'14sharedTest enforcedPlatform('com.brightspot.shared-tests:bom:<v>')
- A
- Commit these changes.
- Run the doctor, and review its edits and its report:
1./gradlew brightspotGradleRewrite2git diff
- Do the items in the report that the doctor cannot do, and commit the changes.
- Run the upgrade recipes that the classpath brings. The doctor commits each recipe.
1./gradlew brightspotDoctor
- Verify the build.
Use the steps below for a project that the doctor cannot upgrade, or to understand the changes.
Later upgrades
To upgrade the Brightspot BOMs after this upgrade, run ./gradlew brightspotUpgrade, and review the commits that it made.
It sets the new versions and then runs brightspotDoctor.
See brightspotUpgrade.
To upgrade a BOM or a dependency after this upgrade:
- Change the version by any means: by hand, or with Dependabot or Renovate.
- Run
./gradlew brightspotDoctor, and review the commits that it made.
To move the build to the latest release of the plugins, run ./gradlew brightspotUpgrade.
See Upgrade recipes.
Conventions
Set $PROJECT to the project name
Throughout this guide, $PROJECT will be used to refer to the project's normalized kebab-case name.
- ✅ Right:
PROJECT='example-co' - ❌ Wrong:
PROJECT='Example Co, Inc.' - 🤡 Extra Wrong:
PROJECT='$PROJECT'
In all cases, the string $PROJECT must be substituted for the project name in all file contents before committing changes to the file.
Before running any of the prescribed commands on your command line, export PROJECT='my-project-name'. This will automate the $PROJECT substitution in those cases.
Resolve // XXX comments
Comments prefixed with XXX are there for you, the reader of this guide, to review, resolve, and remove before committing changes to the file.
- ✅ Right:
brightspotVersion = 4.5.15.8 - ❌ Wrong:
brightspotVersion = 4.5.15.8 // XXX Copy this version number from the legacy build.gradle - 🤡 Extra Wrong:
brightspotVersion = 4.5.x // XXX Copy this version number from the legacy build.gradle
In all cases, any comment starting with // XXX must be deleted after satisfying the advice within.
Project Structure
Projects sometimes have different module naming conventions. For example, older projects have a site module that generates the war file, while newer projects call that the web module. Throughout this guide, we will use the "new" conventions, but this table should help you to map these names to those found in your project:
| Name | New Path | Old Path |
|---|---|---|
| core | ./core | ./core |
| web | ./web | ./site |
| frontend | ./frontend | ./frontend |
| root styleguide | ./frontend/styleguide | ./styleguide |
| bundles | ./frontend/bundles | ./themes |
Version Control Hygiene
Each step in this guide should be committed to the git repository. Suggested commit messages are detailed inline. The suggested command git commit -a adds and commits all changes in the entire repo, so make sure there aren't any unintended changes included.
Command Line Working Directory
The working directory for all command line operations detailed in this guide is assumed to be the root directory of the project unless otherwise noted.
Example
Along with this guide, this pull request can be used as a reference.
0. Current build for verification
If a styleguide upgrade is necessary, do it first to simplify the verification of the gradle plugin upgrade.
Before changing anything, run a full build and set the gradle scan and war file aside for verification at the end.
The gradlew command will output a Gradle scan URL. Bookmark this URL to compare at the end.
If the current version of component-lib is before 4.2.188 or 4.5.23, upgrade to one of those versions before this step.
1./gradlew clean build --no-build-cache --scan2cp web/build/libs/$PROJECT*.war /tmp/$PROJECT-before.war
1. Upgrade Gradle Wrapper
Remove gradle or gradle/ from the .gitignore file in the project root, if present.
1./gradlew wrapper --gradle-version=8.14.12./gradlew wrapper --gradle-version=8.14.13git commit -a -m "Upgrade Gradle Wrapper to 8.14.1"
Be sure to run the ./gradlew wrapper command twice so it downloads the latest files.
2. Delete /build.gradle
Just set it aside for now, as we may need to copy some details from it later.
1mv ./build.gradle /tmp/$PROJECT-build.gradle2git commit -a -m "Gradle plugin upgrade: Remove /build.gradle"
Take special note of any forced dependency constraints, substitutions, or exclusions. These should be converted to dependencyConstraints, dependencySubstitutions, or dependencyExclusions - see Common overrides for details.
3. Update settings.gradle
Edit settings.gradle to match the following.
The snippet below is annotated for an in-flight upgrade. For the configuration to aim for once the upgrade is done, see Recommended settings.gradle.
1pluginManagement {2repositories {3gradlePluginPortal() {4content {5excludeGroupByRegex 'com\\.brightspot($|\\..+)'6excludeGroupByRegex 'com\\.psddev($|\\..+)'7}8}9maven {10url 'https://artifactory.psdops.com/public'11}12}13}1415plugins {16id 'com.brightspot.gradle' version '3.14.0'17}1819rootProject.name = '$PROJECT'2021brightspot {22projectGroup = 'com.$PROJECT' // XXX This is probably already set in the existing build.gradle. If so, use that value here.2324versions {25java = 21 // XXX Use the project's current Java version if it cannot move to 21 yet26}2728dependencies {29// XXX Copy the versions for these from existing build.gradle, if present.30implementation enforcedPlatform('com.psddev:brightspot-bom:4.5.15.19')31implementation enforcedPlatform('com.psddev:brightspot-dependencies:4.5.15.19')3233sharedTest enforcedPlatform('com.brightspot.shared-tests:bom:1.0.1')3435// XXX If BSP Go isn't in the existing build.gradle, leave these two lines out.36implementation enforcedPlatform('com.brightspot.go:bom:1.4.6')37implementation enforcedPlatform('com.brightspot.go:go-dependencies:1.4.6')3839// XXX If Component Lib isn't in the existing build.gradle, leave these two lines out.40// XXX If on version 4.2.188 or lower, or 4.5.23 or lower, you will need to upgrade to at least one of those versions.41implementation enforcedPlatform('com.psddev.component-lib:bom:4.5.26')42implementation enforcedPlatform('com.psddev.component-lib:cl-dependencies:4.5.26')4344// XXX Only include these if "com.psddev.migration:bom" is already included.45// XXX (not "com.psddev:migration-bom" or "com.psddev.migration:migration-bom")46// XXX Note that it may be in another build.gradle such as migration/build.gradle47implementation enforcedPlatform('com.psddev.migration:bom:4.7.0.4')48implementation enforcedPlatform('com.psddev.migration:dependencies:4.7.0.4')49}50}5152// XXX Project-specific lines similar to the following (until XXX END) should already be present in the53// XXX existing settings.gradle. Leave them alone.5455include(':core')56include(':web')57include(':frontend')58include(':bundle-default')5960project(':bundle-default').projectDir = file('frontend/bundles/bundle-default')6162// XXX END63
The plugins apply Develocity, accept its terms of use, and publish a scan only when you pass --scan. A com.gradle.develocity or com.gradle.enterprise plugin declaration and a develocity or gradleEnterprise block are not needed. The plugins also configure SonarQube, so a com.brightspot.sonar plugin declaration is not needed. Remove them if the existing settings.gradle has them. If the existing settings.gradle has a brightspotLibs version catalog, remove it; the dependencies block replaces it. See Build scans.
Most common dependency substitutions are now managed by the Brightspot Gradle plugins, so remove any of the project's own substitutions that match a default. Custom substitutions can still be added. See the default substitutions.
Set the org.apache.solr:solr-solrj constraint like the example below. If your project is moving to cloud and a SOLR upgrade has been applied to the cluster, set the value to 8.11.1, otherwise use the value forced in {site/web}/build.gradle (likely 8.6.1). Any forced dependency constraints from the now-deleted /build.gradle must be added to dependencyConstraints in settings.gradle as well.
1brightspot {2dependencyConstraints = [3'org.apache.solr:solr-solrj': '8.11.1', // XXX or forced value in {site/web}/build.gradle4]5}6
1git commit -a -m "Gradle plugin upgrade: Update settings.gradle"
4. Update core/build.gradle
Add the brightspot java plugin to the very top of the file:
1plugins {2id 'com.brightspot.java-core'3}
Standardize on using api rather than compile for all dependencies in core/build.gradle. Either use a text editor to find and replace, or do it on the command line like so:
1sed -i '' 's/ compile / api /; s/constraints.compile(/constraints.api(/;' core/build.gradle
Check the file to ensure the changes are what you expect: All compile lines are changed to api.
Similarly, runtime should be replaced with runtimeOnly, testCompile with testImplementation, and testRuntime with testRuntimeOnly. compileOnly and testCompileOnly should remain as-is. More on deprecated configurations in the Gradle documentation
Testing dependencies have been updated. Remove these dependencies and run a build of the core project to ensure that no custom test work relied on these.
1dependencies {2testImplementation 'com.psddev.component-lib:test-utils'3testImplementation 'com.psddev.component-lib:shared-unit-tests'4}
Assuming no build failures from the previous step, add shared unit tests to the dependencies to replace the old tests:
1dependencies {2sharedTest 'com.brightspot.shared-tests:pack-standard-backend-unit-tests'3}
Remove the com.psddev.component-lib:materialize dependency, if present.
Remove any lines like this:
1project.afterEvaluate {2jar.dependsOn(test)3}
If core/host.gradle exists and it is included into core/build.gradle via apply from: file("host.gradle"), remove the include and move the dependencies from core/host.gradle into the dependencies in core/build.gradle. Be sure to remove the core/host.gradle file as well.
Any instances of force = true in core/build.gradle need to be converted into dependency constraints in settings.gradle.
For projects using com.brightspot.gradle at version 1.1.1 or later, any dependency exclusions should be moved into settings.gradle. See exclusions for the syntax.
If the build fails with javadoc parsing errors, you can either fix them or skip javadoc processing like so:
1tasks.withType(Javadoc).configureEach { enabled = false }
1git commit -a -m "Gradle plugin upgrade: Update core/build.gradle"
5. Update frontend/build.gradle
frontend/build.gradle should only contain the following lines; anything else in this file can be deleted:
1plugins {2id 'com.brightspot.viewgen'3}45description = '$PROJECT: Frontend'
If the root styleguide directory is in the project root (rather than under the frontend/ directory), add the following to frontend/build.gradle:
1viewgen {2styleguideDir = '../styleguide'3}
Any files in frontend/src/main/webapp must be moved to web/src/main/webapp (or site/src/main/webapp). For example, frontend/src/main/webapp/WEB-INF/web.xml to web/src/main/webapp/WEB-INF/web.xml
Please pay special attention to web.xml. It is imperative that this file is located within the web (or site) directory listed above. If you encounter any errors pertaining to org.apache.jasper.JasperException or com.psddev.dari.web.NoCurrentWebRequestException please ensure that web.xml is in the correct location before troubleshooting further.
1git commit -a -m "Gradle plugin upgrade: Update frontend/build.gradle"
6. Update each bundle's build.gradle
Each frontend bundle's build.gradle should contain the following:
1plugins {2id 'com.brightspot.bundle'3}45description = 'bundle-default' // XXX Just leave this description alone if it's set already,6// XXX otherwise set it to match the bundle directory name78bundle {9yarnBuildCommand = 'build'10styleguideOutputDir = 'styleguide'11zipOutputPath = 'theme.zip'1213// for CdnCssTest14cdnCssFiles = [15'styles/default/All.min.css' // XXX Change this to the correct path(s) of All.min.css16]17}1819node {20yarnVersion = '1.22.19' // XXX Copy these versions from the existing /build.gradle21version = '18.17.1' // XXX If the existing node version is earlier than 14, you'll also need to upgrade styleguide.22download = true23}2425dependencies {26sharedTest 'com.brightspot.shared-tests:pack-standard-frontend-unit-tests'27}
If the bundle/theme's build.gradle contains a test {} block that sets maxHeapSize, remove it and set the heap size in the project's gradle.properties:
1brightspot.test.maxHeapSize=4g
1git commit -a -m "Gradle plugin upgrade: Update bundle build.gradle"
7. Update web/build.gradle
The build.gradle file that generates the war file should contain the following:
Some additional api, compile, or implementation dependencies, such as org.apache.solr:solr-solrj may be present in the existing web/build.gradle. These must be removed and added to core/build.gradle! Additionally, any instances of force = true in web/build.gradle need to be converted into dependency constraints in settings.gradle (see Update settings.gradle above for an example).
1plugins {2id 'com.brightspot.war'3}45description = '$PROJECT: Web'67dependencies {8// XXX Project-specific lines similar to the following three lines should already be present in the9// XXX existing web/build.gradle. Make sure they're using implementation instead of api, and otherwise leave them alone.10implementation project(':frontend')11implementation project(':core')12implementation project(':bundle-default')1314// for expansion into the war15compileOnly 'com.psddev:cms-tool-ui'1617// Tests18sharedTest 'com.brightspot.go:lib-util-gradle-dynamic-test' // XXX Only if this is already present19sharedTest 'com.brightspot.shared-tests:pack-standard-backend-integration-tests'20}
1git commit -a -m "Gradle plugin upgrade: Update web/build.gradle"
8. Update any other subproject build.gradle files
An example of one of these would be a migration/build.gradle file.
These must be treated on a case-by-case basis but can probably follow the same process as core/build.gradle.
9. Verify the build
As before, run the build, bookmark the gradle scan, and set the war file aside for comparison.
1./gradlew clean build --no-build-cache --scan2cp web/build/libs/$PROJECT*.war /tmp/$PROJECT-after.war
We're going to execute a very simple test that compares the filenames in the "before" war file to the filenames in the "after" war file. This isn't a perfect test, as the file contents could differ, but this simple test is sufficient for the purpose of this upgrade.
1diff <(unzip -l /tmp/$PROJECT-before.war) <(unzip -l /tmp/$PROJECT-after.war)
The expected output from this command is something like this:
11c12< Archive: /tmp/$PROJECT-before.war3---4> Archive: /tmp/$PROJECT-after.war
Anything else indicates that one or more files have been added or removed. The steps to remediate depend on the file that has changed, and are outside the scope of this document.
The build scans taken in the first and last steps may be useful in troubleshooting this or other issues.
10. Other Configuration Changes
Move ignore test files after gradle upgrade
If the ignore files for the below tests are under site/src/test/resources/brightspot/, they need to move to core/src/test/resources/brightspot/ so those tests can continue to be ignored
1AlterationClassAnnotationProcessorTest.ignored.txt2AlterationFieldAnnotationProcessorTest.ignored.txt3NoAlterationMethodsExistTest.ignored.txt4ModificationFieldInternalNamePrefixTest.ignored.txt5NoAfterCreateOutsideModificationsTest.ignored.txt6NoFinalFieldsInRecordsTest.ignored.txt7JobStatusTest.ignored.txt8RecordableHasNullaryConstructorTest.ignored.txt
docker-compose.yml
The war file name has changed as part of this update, so you'll need to change docker-compose.yml to reflect this.
1services: // XXX lines omitted for brevity2tomcat:3environment:4- ROOT_WAR=/code/web/build/libs/${PROJECT}-web.war
gradle.properties
Double-check the file gradle.properties to ensure it contains a property like org.gradle.caching=true.