Skip to content

Instantly share code, notes, and snippets.

@dirkraft
Last active June 23, 2017 15:00
Show Gist options
  • Save dirkraft/8230302 to your computer and use it in GitHub Desktop.
Save dirkraft/8230302 to your computer and use it in GitHub Desktop.
A build.gradle project template, which bootstraps all necessary parts of a Java, Maven, IntelliJ project on GitHub with the minimum artifact publishing requirements to Sonatype's Maven central.
/* Root build.gradle for Java, Maven, and IntelliJ with Maven central POM. Assumes GitHub.
You should eventually read through and understand this entire Gradle script. Last tried with IntelliJ 2017.1.
Quick start:
- Copy this file into a directory named for your project.
- Choose one Gradle integration mode. Once chosen, stick to it.
IntelliJ Gradle integration:
- Import the project directory as a Gradle project.
- To change Gradle files, enable auto-synchronize or click the synchronize button in the Gradle panel.
Gradle idea plugin:
- Add the plugin into allprojects{}: apply plugin: 'idea'
- Run: gradle wrapper && ./gradlew idea
- Import the .ipr project file into IntelliJ.
- After changing Gradle files, run: ./gradlew idea.
- Gradle idea plugin reference: https://docs.gradle.org/current/userguide/idea_plugin.html
You can start coding now. Eventually...
- Review and edit the 'Project constants' section to match your project.
- Choose a license. Update project_pom, and consider providing src/license/HEADER which will
be prepended to all your source files.
Common Tasks:
- Add new sub-project:
Make a new Gradle project: touch path/to/sub-project/build.gradle
In settings.gradle add: include 'path:to:sub-project'
Synchronize IntelliJ with your Gradle project files.
- Publish maven artifacts:
In select projects' build.gradle add: uploadArchives { enabled true }
./gradlew -PdeployUrl=http://server/artifactory/repo -PdeployUser=user -PdeployPass=pass uploadArchives
Sonatype snapshot url: https://oss.sonatype.org/content/repositories/snapshots
Sonatype staging url: https://oss.sonatype.org/service/local/staging/deploy/maven2
See Sonatype docs for more: http://central.sonatype.org/pages/requirements.html
*/
plugins {
id "com.github.hierynomus.license" version "0.13.1"
}
ext {
// Project constants
github_org = 'organization'
project_name = 'project'
artifact_group = 'com.github.' + github_org + '.' + project_name
project_version = '0.0.1-SNAPSHOT'
project_description = ''
project_jdk = '1.8'
project_pom = {
name project_name
description project_description
url "https://github.com/${github_org}/${project_name}"
developers {
developer {
id 'developer'
name 'developer'
}
}
licenses {
license {
name 'MIT License'
url 'http://opensource.org/licenses/MIT'
distribution 'repo'
}
}
scm {
url "https://github.com/${github_org}/${project_name}.git"
connection "scm:git:https://github.com/${github_org}/${project_name}.git"
developerConnection "scm:git:[email protected]:${github_org}/${project_name}.git"
}
organization {
name github_org
url "https://github.com/${github_org}"
}
}
/** Function returns a new manifest that can be customized per module */
defaultManifest = {
return manifest {
def git_cmd = 'git rev-parse HEAD'
def git_proc = git_cmd.execute()
attributes 'SCM-Revision': git_proc.text.trim()
attributes 'Timestamp': String.valueOf(System.currentTimeMillis())
attributes 'Build-Host': InetAddress.localHost.hostName
}
}
/** Used where gradle task evaluation would fail because of an undefined value, even if the task wasn't targeted. */
defaultBlank = { closure ->
try {
closure()
} catch (MissingPropertyException e) {
''
}
}
}
allprojects {
group = artifact_group
version = project_version
apply plugin: 'java'
sourceCompatibility = project_jdk
targetCompatibility = project_jdk
apply plugin: 'maven'
apply plugin: 'signing'
license { header rootProject.file('src/license/HEADER') }
assemble.dependsOn(licenseFormat)
repositories {
mavenCentral()
}
jar { doFirst { manifest = defaultManifest() } }
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from 'build/docs/javadoc'
doFirst { manifest = defaultManifest() }
}
task sourcesJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.allSource
doFirst { manifest = defaultManifest() }
}
artifacts {
archives jar
archives javadocJar
archives sourcesJar
}
signing {
required { !version.endsWith('SNAPSHOT') && gradle.taskGraph.hasTask("uploadArchives") }
sign configurations.archives
}
uploadArchives {
dependsOn licenseFormat
enabled false
repositories.mavenDeployer {
beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
repository(url: defaultBlank({ deployUrl })) {
// If these are not defined assemble needlessly fails for unrelated tasks, hence, defaultBlank.
authentication(userName: defaultBlank({ deployUser }), password: defaultBlank({ deployPass }))
}
pom.project project_pom
}
}
}
task wrapper(type: Wrapper) {
gradleVersion = '4.0'
distributionUrl = 'https://services.gradle.org/distributions/gradle-4.0-all.zip'
}
@deanhiller
Copy link

not sure but getting this error trying to use this code(on multiproject build)..

Could not find method license() for arguments [build_eiigg76myo3wuvq219kdx5rxk$_run_closure4$_closure21@342eaa4d] on project ':core' of type org.gradle.api.Project

@dirkraft
Copy link
Author

dirkraft commented Apr 6, 2017

Heh I'm so late. For future visitors, not sure what might have caused that. I just tried the latest of this and it seems ok.

mkdir /tmp/abc
cd $_
# copied the template above into build.gradle
mkdir subproject
touch subproject/build.gradle

created settings.gradle with

rootProject.name = 'test-root'
include 'subproject'

Then did all this. The gradle version is likely pretty significant. I'm not sure what versions will work with the above template.

jasond@void /tmp/abc
$ gradle --version

------------------------------------------------------------
Gradle 3.1
------------------------------------------------------------

Build time:   2016-09-19 10:53:53 UTC
Revision:     13f38ba699afd86d7cdc4ed8fd7dd3960c0b1f97

Groovy:       2.4.7
Ant:          Apache Ant(TM) version 1.9.6 compiled on June 29 2015
JVM:          1.8.0_101 (Oracle Corporation 25.101-b13)
OS:           Mac OS X 10.11.6 x86_64

jasond@void /tmp/abc
$ gradle idea
Parallel execution with configuration on demand is an incubating feature.
:subproject:ideaModule
:ideaModule
:ideaProject
:subproject:idea
:ideaWorkspace
:idea

BUILD SUCCESSFUL

Total time: 0.636 secs
jasond@void /tmp/abc
$ gradle build
Parallel execution with configuration on demand is an incubating feature.
:assemble UP-TO-DATE
:subproject:compileJava
:license UP-TO-DATE
:subproject:compileJava UP-TO-DATE
:check UP-TO-DATE
:build
:subproject:compileGroovy UP-TO-DATE
:build UP-TO-DATE
:subproject:processResources UP-TO-DATE
:subproject:classes UP-TO-DATE
:subproject:jar UP-TO-DATE
:subproject:javadoc UP-TO-DATE
:subproject:javadocJar UP-TO-DATE
:subproject:licenseFormatMain UP-TO-DATE
:subproject:licenseFormatTest UP-TO-DATE
:subproject:sourcesJar UP-TO-DATE
:subproject:signArchives SKIPPED
:subproject:assemble UP-TO-DATE
:subproject:licenseMain UP-TO-DATE
:subproject:licenseTest UP-TO-DATE
:subproject:license UP-TO-DATE
:subproject:compileTestJava UP-TO-DATE
:subproject:compileTestGroovy UP-TO-DATE
:subproject:processTestResources UP-TO-DATE
:subproject:testClasses UP-TO-DATE
:subproject:test UP-TO-DATE
:subproject:check UP-TO-DATE
:subproject:build UP-TO-DATE

BUILD SUCCESSFUL

Total time: 0.688 secs

Even opened up the .ipr in intellij. Seems ok.

screen shot 2017-04-05 at 5 18 31 pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment