Last active
June 23, 2017 15:00
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
created settings.gradle with
Then did all this. The gradle version is likely pretty significant. I'm not sure what versions will work with the above template.
Even opened up the .ipr in intellij. Seems ok.