import java.nio.charset.StandardCharsets import java.util.function.Function import groovy.json.JsonSlurper /* * Hibernate, Relational Persistence for Idiomatic Java * * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ apply from: rootProject.file( 'gradle/module.gradle' ) apply plugin: 'org.hibernate.orm.build.doc-pub' apply plugin: 'org.hibernate.orm.build.jdks' apply plugin: 'idea' idea.module { } // skip building this when `build` task is run from root, as many of our CI jobs do tasks.build.dependsOn.clear() def stageIntegrationGuideTask = tasks.register( "stageIntegrationGuide", Copy ) { group "documentation" description "Stages the Integration Guide as part of preparing for release" dependsOn ":documentation:renderIntegrationGuides" from project.provider { project( ":documentation" ).layout.buildDirectory.dir( "asciidoc/integrationguide" ) } into layout.buildDirectory.dir( "documentation/integrationguide" ) } def stageQuickstartTask = tasks.register( "stageQuickstart", Copy ) { group 'documentation' description "Stages the Getting Started Guide as part of preparing for release" dependsOn ':documentation:renderGettingStartedGuides' from project.provider { project( ":documentation" ).layout.buildDirectory.dir( "asciidoc/quickstart" ) } into layout.buildDirectory.dir( "documentation/quickstart" ) } def stageTopicalGuideTask = tasks.register( "stageTopicalGuide", Copy ) { group 'documentation' description "Stages the Topical Guide as part of preparing for release" dependsOn ':documentation:renderTopicalGuides' from project.provider { project( ":documentation" ).layout.buildDirectory.dir( "asciidoc/topical" ) } into layout.buildDirectory.dir( "documentation/topical" ) } def stageIntroductionGuideTask = tasks.register( "stageIntroductionGuide", Copy ) { group 'documentation' description "Stages the Introduction Guide as part of preparing for release" dependsOn ':documentation:renderIntroductionGuides' from project.provider { project( ":documentation" ).layout.buildDirectory.dir( "asciidoc/introduction" ) } into layout.buildDirectory.dir( "documentation/introduction" ) } def stageQueryGuideTask = tasks.register( "stageQueryGuide", Copy ) { group 'documentation' description "Stages the Query Language Guide as part of preparing for release" dependsOn ':documentation:renderQueryLanguageGuides' from project.provider { project( ":documentation" ).layout.buildDirectory.dir( "asciidoc/querylanguage" ) } into layout.buildDirectory.dir( "documentation/querylanguage" ) } def stageRepositoriesGuideTask = tasks.register( "stageRepositoriesGuide", Copy ) { group 'documentation' description "Stages the Repositories Guide as part of preparing for release" dependsOn ':documentation:renderRepositories' from project.provider { project( ":documentation" ).layout.buildDirectory.dir( "asciidoc/repositories" ) } into layout.buildDirectory.dir( "documentation/repositories" ) } def stageUserGuideTask = tasks.register( "stageUserGuide", Copy ) { group 'documentation' description "Stages the User Guide as part of preparing for release" dependsOn ':documentation:renderUserGuides' description "Stages the User Guide as part of preparing for release" dependsOn ':documentation:renderUserGuides' from project.provider { project( ":documentation" ).layout.buildDirectory.dir( "asciidoc/userguide" ) } into layout.buildDirectory.dir( "documentation/userguide" ) } def stageMigrationGuideTask = tasks.register( "stageMigrationGuide", Copy ) { group 'documentation' description "Stages the Migration Guide as part of preparing for release" dependsOn ':documentation:renderMigrationGuide' from project.provider { project( ":documentation" ).layout.buildDirectory.dir( "asciidoc/migration-guide" ) } into layout.buildDirectory.dir( "documentation/migration-guide" ) } tasks.named( "publishMigrationGuide" ).configure { dependsOn stageMigrationGuide } def stageIncubationReportTask = tasks.register( "stageIncubationReport", Copy ) { task -> group 'documentation' description "Stages ORM @Incubating report" dependsOn ':documentation:generateIncubationReport' tasks.stageOrmReports.dependsOn task from project( ":documentation" ).tasks.generateIncubationReport into layout.buildDirectory.dir( "documentation/incubating" ) } def stageInternalsReportTask = tasks.register( "stageInternalsReport", Copy ) { task -> group 'documentation' description "Stages the @Internal report" dependsOn ':documentation:generateInternalsReport' from project( ":documentation" ).tasks.generateInternalsReport into layout.buildDirectory.dir( "documentation/internals" ) } def stageDeprecationReportTask = tasks.register( "stageDeprecationReport", Copy ) { group 'documentation' description "Stages the @Deprecated/@Remove report" dependsOn ':documentation:generateDeprecationReport' from project( ":documentation" ).tasks.generateDeprecationReport into layout.buildDirectory.dir( "documentation/deprecated" ) } def stageLoggingReportTask = tasks.register( "stageLoggingReport", Copy ) { task -> group 'documentation' description "Stages the logging report" dependsOn ':documentation:renderLoggingReport' from project( ":documentation" ).tasks.renderLoggingReport into layout.buildDirectory.dir( "documentation/logging" ) } def stageDialectReportTask = tasks.register( "stageDialectReport", Copy ) { task -> group 'documentation' description "Stages the supported Dialects report" dependsOn ':documentation:renderDialectReport' from project( ":documentation" ).tasks.renderDialectReport into "${buildDir}/documentation/dialect" } def stageOrmReportsTask = tasks.register( "stageOrmReports" ) { group 'documentation' description "Stages all ORM reports as part of preparing for release" dependsOn ':documentation:generateReports' dependsOn stageIncubationReportTask dependsOn stageInternalsReportTask dependsOn stageDeprecationReportTask dependsOn stageLoggingReportTask dependsOn stageDialectReportTask } def stageJavadocsTask = tasks.register( "stageJavadocs", Copy ) { group 'documentation' description "Stages the aggregated Javadocs" dependsOn ':documentation:javadoc' from project( ":documentation" ).tasks.javadoc into "${buildDir}/documentation/javadocs" } /** * Assembles all documentation into the {buildDir}/documentation directory. * * Depends on building the docs */ def assembleDocumentationTask = tasks.register( "assembleDocumentation" ) { group 'documentation' description 'Assembles all documentation into the {buildDir}/documentation directory' dependsOn ':documentation:buildDocsForPublishing' dependsOn stageJavadocsTask dependsOn stageQuickstartTask dependsOn stageIntroductionGuideTask dependsOn stageUserGuideTask dependsOn stageQueryGuideTask dependsOn stageRepositoriesGuideTask dependsOn stageIntegrationGuideTask dependsOn stageTopicalGuideTask dependsOn stageMigrationGuideTask dependsOn stageOrmReportsTask } tasks.named( "uploadDocumentation" ) { group = "documentation" description = "Uploads assembled documentation to the doc server" dependsOn assembleDocumentationTask doFirst { if ( rootProject.ormVersion.isSnapshot ) { logger.error( "Cannot perform upload of SNAPSHOT documentation" ); throw new RuntimeException( "Cannot perform upload of SNAPSHOT documentation" ); } else { logger.lifecycle( "Uploading documentation ..." ) } } doLast { logger.lifecycle( 'Done uploading documentation' ) } } def releaseChecksTask = tasks.register( "releaseChecks" ) { group 'Release' description 'Checks and preparation for release' doFirst { logger.lifecycle("Checking that the working tree is clean...") String uncommittedFiles = executeGitCommand('status', '--porcelain') if (!uncommittedFiles.isEmpty()) { throw new GradleException( "Cannot release because there are uncommitted or untracked files in the working tree.\n" + "Commit or stash your changes first.\n" + "Uncommitted files:\n " + uncommittedFiles ); } String gitBranchLocal String gitRemoteLocal if (project.hasProperty('gitBranch') && !project.property('gitBranch').isEmpty()) { gitBranchLocal = project.property('gitBranch') } else { gitBranchLocal = executeGitCommand( 'branch', '--show-current' ).trim() } if (project.hasProperty('gitRemote') && !project.property('gitRemote').isEmpty()) { gitRemoteLocal = project.property('gitRemote') } else { final String remotes = executeGitCommand( 'remote', 'show' ).trim() final List tokens = remotes.tokenize() if ( tokens.size() != 1 ) { throw new GradleException( "Could not determine `gitRemote` property for `releaseChecks` tasks." ) } gitRemoteLocal = tokens.get( 0 ) } project.ext { gitBranch = gitBranchLocal gitRemote = gitRemoteLocal } logger.lifecycle("Switching to branch '${project.gitBranch}'...") executeGitCommand('checkout', project.gitBranch) logger.lifecycle("Checking that all commits are pushed...") String diffWithUpstream = executeGitCommand('diff', '@{u}') if (!diffWithUpstream.isEmpty()) { throw new GradleException( "Cannot perform `ciRelease` tasks because there are un-pushed local commits .\n" + "Push your commits first." ); } } } def preVerifyReleaseTask = tasks.register( "preVerifyRelease" ) { group 'Release' description 'Pre-verifies a release job execution (Run locally before a CI release)' dependsOn tasks.clean dependsOn assembleDocumentationTask } def changeLogFileTask = tasks.register( "changeLogFile" ) { group 'Release' description 'Updates the changelog.txt file based on the change-log report from Jira' dependsOn releaseChecksTask doFirst { logger.lifecycle( "Appending version `${project.releaseVersion}` to changelog..." ) ChangeLogFile.update( ormVersion.fullName ); } } def changeToReleaseVersionTask = tasks.register( "changeToReleaseVersion" ) { group 'Release' description 'Updates `gradle/version.properties` file to the specified release-version' dependsOn releaseChecksTask doFirst { logger.lifecycle( "Updating version-file to release-version : `${project.releaseVersion}`" ) updateVersionFile( project.releaseVersion ) } } def gitPreparationForReleaseTask = tasks.register( 'gitPreparationForRelease' ) { dependsOn releaseChecksTask dependsOn changeLogFileTask dependsOn changeToReleaseVersionTask doLast { logger.lifecycle( "Performing pre-steps Git commit : `${project.releaseVersion}`" ) executeGitCommand( 'add', '.' ) executeGitCommand( 'commit', '-m', "Pre-steps for release : `${project.ormVersion.fullName}`" ) } } def changeToDevelopmentVersionTask = tasks.register( 'changeToDevelopmentVersion' ) { group 'Release' description 'Updates `gradle/version.properties` file to the specified development-version' dependsOn releaseChecksTask doFirst { logger.lifecycle( "Updating version-file to development-version : `${project.developmentVersion}`" ) updateVersionFile( project.developmentVersion ) } } def releasePreparePostGitTask = tasks.register( 'gitTasksAfterRelease' ) { dependsOn changeToDevelopmentVersionTask doLast { if ( project.createTag ) { logger.lifecycle( "Tagging release : `${project.releaseTag}`..." ) executeGitCommand( 'tag', '-a', project.releaseTag, '-m', "Release $project.ormVersion.fullName" ) } logger.lifecycle( "Performing post-steps Git commit : `${project.releaseVersion}`" ) executeGitCommand( 'add', '.' ) executeGitCommand( 'commit', '-m', "Post-steps for release : `${project.ormVersion.fullName}`" ) } } void updateVersionFile(String version) { logger.lifecycle( "Updating `gradle/version.properties` version to `${version}`" ) project.ormVersionFile.text = "hibernateVersion=${version}" } def publishReleaseArtifactsTask = tasks.register( 'publishReleaseArtifacts' ) { mustRunAfter gitPreparationForReleaseTask dependsOn uploadDocumentation dependsOn uploadDocumentationDescriptor } def releasePerformPostGitTask = tasks.register( 'gitTasksAfterReleasePerform' ) { doLast { if ( project.createTag ) { logger.lifecycle( "Pushing branch and tag to remote `${project.gitRemote}`..." ) executeGitCommand( 'push', '--atomic', project.gitRemote, project.gitBranch, project.releaseTag ) } else { logger.lifecycle( "Pushing branch to remote `${project.gitRemote}`..." ) executeGitCommand( 'push', project.gitRemote, project.gitBranch ) } } } def releasePrepareTask = tasks.register( 'releasePrepare' ) { group 'Release' description 'Performs release preparations on local check-out, including updating changelog' // we want to assemble the docs here so that we catch problems early (and even during "dry run" for CI releases) dependsOn assembleDocumentationTask dependsOn gitPreparationForReleaseTask finalizedBy releasePreparePostGitTask } def releasePerformTask = tasks.register( 'releasePerform' ) { group 'Release' description 'Performs a release on local check-out, including updating changelog and ' dependsOn publishReleaseArtifactsTask finalizedBy releasePerformPostGitTask } def releaseTask = tasks.register( 'release' ) { group 'Release' description 'Performs a release on local check-out, including updating changelog and ' dependsOn releasePrepareTask dependsOn releasePerformTask } def ciReleaseTask = tasks.register( 'ciRelease' ) { group 'Release' description 'Performs a release: the hibernate version is set and the changelog.txt file updated, the changes are pushed to github, then the release is performed, tagged and the hibernate version is set to the development one.' dependsOn releaseTask } static String executeGitCommand(Object ... subcommand){ List command = ['git'] Collections.addAll( command, subcommand ) def proc = command.execute() def code = proc.waitFor() def stdout = inputStreamToString( proc.getInputStream() ) def stderr = inputStreamToString( proc.getErrorStream() ) if ( code != 0 ) { throw new GradleException( "An error occurred while executing " + command + "\n\nstdout:\n" + stdout + "\n\nstderr:\n" + stderr ) } return stdout } static String inputStreamToString(InputStream inputStream) { inputStream.withCloseable { ins -> new BufferedInputStream(ins).withCloseable { bis -> new ByteArrayOutputStream().withCloseable { buf -> int result = bis.read(); while (result != -1) { buf.write((byte) result); result = bis.read(); } return buf.toString( StandardCharsets.UTF_8.name()); } } } } class ChangeLogFile { // Get the Release Notes from Jira and add them to the Hibernate changelog.txt file static void update(String releaseVersion) { def text = "" File changelog = new File( "changelog.txt" ) def newReleaseNoteBlock = getNewReleaseNoteBlock(releaseVersion) changelog.eachLine { line -> if ( line.startsWith( "Note:" ) ) { text += line + System.lineSeparator() + System.lineSeparator() + newReleaseNoteBlock } else { text += line + System.lineSeparator() } } changelog.text = text } // Get the Release Notes from Jira static String getNewReleaseNoteBlock(String releaseVersion) { def restReleaseVersion; if ( releaseVersion.endsWith( ".Final" ) ) { restReleaseVersion = releaseVersion.replace( ".Final", "" ) } else { restReleaseVersion = releaseVersion } def apiString = "https://hibernate.atlassian.net/rest/api/2/search/?jql=project=HHH%20AND%20fixVersion=${restReleaseVersion}%20order%20by%20issuetype%20ASC" def apiUrl = new URL( apiString ) def jsonReleaseNotes = new JsonSlurper().parse( apiUrl ) def releaseDate = new Date().format( 'MMMM dd, YYYY' ) def versionId = getVersionId( jsonReleaseNotes, restReleaseVersion ) ReleaseNote releaseNotes = new ReleaseNote( releaseVersion, releaseDate, versionId ) def issuetype jsonReleaseNotes.issues.each { issue -> if ( issuetype != issue.fields.issuetype.name ) { issuetype = issue.fields.issuetype.name releaseNotes.addEmptyLine(); releaseNotes.addLine( "** ${issue.fields.issuetype.name}" ) } releaseNotes.addLine( " * [" + issue.key + "] - " + issue.fields.summary ) } releaseNotes.addEmptyLine() return releaseNotes.notes } private static getVersionId(jsonReleaseNotes, String restReleaseVersion) { def fixVersions = jsonReleaseNotes.issues.get( 0 ).fields.fixVersions for ( def fixVersion : fixVersions ) { if ( fixVersion.name.equals( restReleaseVersion ) ) { return fixVersion.id } } throw new GradleException( "Unable to determine the version id of the current release." ) } } class ReleaseNote { String notes; String notesHeaderSeparator = "------------------------------------------------------------------------------------------------------------------------" ReleaseNote(String releaseVersion, String releaseDate, String versionId) { notes = "Changes in ${releaseVersion} (${releaseDate})" + System.lineSeparator() addHeaderSeparator() addEmptyLine() addLine( "https://hibernate.atlassian.net/projects/HHH/versions/${versionId}" ) } void addLine(String text) { notes += text + System.lineSeparator() } void addHeaderSeparator() { addLine( notesHeaderSeparator ) } void addEmptyLine() { notes += System.lineSeparator() } void addEmptyLines(int numberOfLines) { for ( i in 1..numberOfLines ) { notes += System.lineSeparator() } } } gradle.getTaskGraph().whenReady {tg-> if ( ( tg.hasTask( project.tasks.releasePrepare ) || tg.hasTask( project.tasks.releasePerform ) ) && ! project.getGradle().getStartParameter().isDryRun() ) { String releaseVersionLocal String developmentVersionLocal def console = tg.hasTask( project.tasks.release ) && !tg.hasTask( project.tasks.ciRelease ) ? System.console() : null if (project.hasProperty('releaseVersion')) { releaseVersionLocal = project.property('releaseVersion') } else { if (console) { // prompt for `releaseVersion` releaseVersionLocal = console.readLine('> Enter the release version: ') } else { throw new GradleException( "`release`-related tasks require the following properties: 'releaseVersion', 'developmentVersion'" ) } } if (project.hasProperty('developmentVersion')) { developmentVersionLocal = project.property('developmentVersion') } else { if (console) { // prompt for `developmentVersion` developmentVersionLocal = console.readLine('> Enter the next development version: ') } else { throw new GradleException( "`release`-related tasks require the following properties: 'releaseVersion', 'developmentVersion'" ) } } assert releaseVersionLocal != null && developmentVersionLocal != null; // set up information for the release-related tasks project.ext { releaseVersion = releaseVersionLocal; developmentVersion = developmentVersionLocal; createTag = !project.hasProperty('noTag') releaseTag = project.createTag ? determineReleaseTag(releaseVersionLocal) : '' } } } static String determineReleaseTag(String releaseVersion) { return releaseVersion.endsWith( '.Final' ) ? releaseVersion.replace( ".Final", "" ) : releaseVersion; }