A sustainable pattern with Gradle

  • Default configuration. Something that works for most projects.
  • Given configuration. Something that can optionally be supplied by the project, to turn on/off features.

The problem

  • Java projects, typically a library.
  • Violation, Java projects but with some small adjustments that I use in my violations-lib and its tools.
  • Gradle plugins.
  • Be signed with PGP.
  • Relocate, shadow, dependencies into a fat jar.
  • Produce a changelog
  • Have Spotbugs, static code analysis, configured and optionally fail build based on violations found.
  • Be configured to publish artifacts to Maven Central (along with all POM requirements Central demands). Or any other Nexus server.
  • Don’t need to duplicate Gradle code in all my projects.
  • Can toggle features.

The solution

apply plugin: 'java'

buildscript {
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
classpath 'se.bjurr.gradle:gradle-scripts:2.+'
}
}
project.ext.buildConfig = [
// Your can supply a given config here, a subset of defaultConfig.
]
apply from: project.buildscript.classLoader.getResource('main.gradle').toURI()
dependencies {
// Dependencies of the project here just like you would in any other project
}
def defaultConfig = [
repoType: "DEFAULT",
staticCodeAnalysis: [
maxViolations: 9999,
],
publishing: [
mavenRepositoryUrl: 'https://oss.sonatype.org/service/local/staging/deploy/maven2/',
nexusCloseAndRelease: true,
sign: true,
relocate: [],
],
gradlePlugin: [
tags: []
],
changelog: [
enabled: true,
],
]
project.ext.buildConfig = []
apply from: project.buildscript.classLoader.getResource('main.gradle').toURI()
project.ext.buildConfig = [
repoType: "VIOLATIONS",
publishing: [
relocate: [
'com.google',
'com.jakewharton'
]
]
]
apply from: project.buildscript.classLoader.getResource('main.gradle').toURI()
project.ext.buildConfig = [
repoType: "GRADLE",
gradlePlugin: [
tags: ['violation', 'static code analysis', 'Checkstyle']
],
]
apply from: project.buildscript.classLoader.getResource('main.gradle').toURI()

Closure

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store