Skip to content

Instantly share code, notes, and snippets.

@garfieldnate
Created July 8, 2018 14:27
Show Gist options
  • Save garfieldnate/de0d67078b8e00d301c534b03d64f847 to your computer and use it in GitHub Desktop.
Save garfieldnate/de0d67078b8e00d301c534b03d64f847 to your computer and use it in GitHub Desktop.
Example gradle build that uploads to Bintray and Maven Central
// Useful tasks:
// (default) jar: creates library jar
// fatJar: creates executable jar
// bintrayUpload: performs full publishingTasks/publication
apply plugin: 'java'
wrapper {
gradleVersion = '4.8.1'
}
repositories {
mavenCentral()
}
// needed for custom dependencies in the fatJar task
configurations {
fatJarConf
}
dependencies {
// EXAMPLE: compile group: 'commons-io', name: 'commons-io', version: '2.6'
// EXAMPLE: fatJarConf group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
}
//////////////////////////////////////////////////////////
// Configure all of our artifact-generating tasks below //
//////////////////////////////////////////////////////////
// print all of the compiler warnings
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
options.compilerArgs << "-Xlint:all"
}
jar {
// library clients should configure slf4j themselves
exclude('logback.xml')
}
task fatJar(type: Jar) {
description "Build an executable jar with all dependencies included"
manifest {
attributes 'Main-Class': project.group + '.MyMainClass'
}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
from { configurations.fatJarConf.collect { it.isDirectory() ? it : zipTree(it) } }
// output to STDOUT in the executable jar
from(sourceSets.main.resources) {
include('logback.xml')
}
// this determines the name of the output artifact and how it is imported as a dependency in other builds
classifier 'executable'
with jar
}
// these two are required for acceptance in Maven Central
task sourcesJar(type: Jar) {
description "Build a jar containing just the java source files"
from sourceSets.main.allJava
classifier 'sources'
}
task javadocsJar(type: Jar) {
description "Build a jar containing just the JavaDocs"
from javadoc
classifier 'javadoc'
}
// add all of the publishing tasks, which rely on the existence of the artifact tasks above
project.ext.artifactsToPublish = [sourcesJar, javadocsJar, fatJar]
apply from: 'scripts/publishingTasks.gradle'
group=io.github.yourName
version=0.0.1
description=A description of your project
vcsUrl=https://github.com/yourGithubName/yourProject
website=http://yourproject.io
githubRepo=yourGithubName/yourProject
licenses=Apache 2.0,GPL-3.0
labels=your,project,tags,here
sourceCompatibility=1.8
targetCompatibility=1.8
# place in ~/.gradle/gradle.properties
# ... other properties up here...
org.gradle.daemon=true
org.gradle.parallel=true
# ... the important parts:
# Bintray
bintray.user=yourBintrayName
bintray.key=yourBintrayKey
# Maven central
sonatype.user=yourSonatypeName
sonatype.password=yourSonatypePassword
# GPG
signing.keyId=yourKeyId
signing.password=yourKeyPassword
signing.secretKeyRingFile=/Users/yourName/.gnupg/secring.gpg
// place in scripts/publishingTasks.gradle
// Contains publication-related tasks
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.3'
}
}
apply plugin: com.jfrog.bintray.gradle.BintrayPlugin
apply plugin: 'maven'
apply plugin: 'maven-publish'
// TODO: configure this using project properties
def pomConfig = {
licenses {
license {
name "GPL 2.0"
url "https://www.gnu.org/licenses/gpl-2.0.en.html"
}
}
developers {
developer {
id "yourGithubId"
name "Your Name"
email "your.name@gmail.com"
}
}
scm {
url project.vcsUrl
}
}
publishing {
publications {
ProjectPublication(MavenPublication) {
from components.java
project.artifactsToPublish.forEach({
artifact it
})
groupId project.group
artifactId project.name
version project.version
pom.withXml {
def root = asNode()
root.appendNode('description', project.description)
root.appendNode('name', project.name)
root.appendNode('url', project.website)
root.children().last() + pomConfig
}
}
}
}
bintray {
user = project.property('bintray.user')
key = project.property('bintray.key')
publications =['ProjectPublication']
pkg {
repo = 'general'
name = project.name
desc = project.description
userOrg = user
licenses = project.licenses.split(',')
vcsUrl = project.vcsUrl
issueTrackerUrl = project.vcsUrl + "/issues"
websiteUrl = project.website
githubRepo = project.githubRepo
githubReleaseNotesFile = 'CHANGELOG.md'
labels = project.labels.split(',')
publicDownloadNumbers = true
version {
name = project.version + '-Final'
desc = project.name + ' ' + project.version + ' final'
vcsTag = project.version
gpg {
sign = true
passphrase = project.property("signing.password")
}
mavenCentralSync {
user = project.property('sonatype.user')
password = project.property('sonatype.password')
}
}
}
}
rootProject.name = 'YourProjectHer'
enableFeaturePreview('STABLE_PUBLISHING')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment