-
-
Save mautini/51e770a99f14dd13dfc7687217df20fc to your computer and use it in GitHub Desktop.
Build Gradle example for Medium Article https://medium.com/@nmauti/sign-and-publish-on-maven-central-a-project-with-the-new-maven-publish-gradle-plugin-22a72a4bfd4b
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
apply plugin: 'java' | |
apply plugin: 'maven-publish' | |
apply plugin: 'signing' | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
compile 'com.google.code.findbugs:jsr305:3.0.1' | |
compile 'com.google.guava:guava:19.0' | |
compile 'com.google.code.gson:gson:2.5' | |
testCompile 'junit:junit:4.+' | |
testCompile 'com.google.truth:truth:0.28' | |
} | |
test { | |
// show standard out and standard error of the test JVM(s) on the console | |
testLogging.showStandardStreams = true | |
} | |
task sourceJar(type: Jar) { | |
classifier "sources" | |
from sourceSets.main.allJava | |
} | |
task javadocJar(type: Jar, dependsOn: javadoc) { | |
classifier "javadoc" | |
from javadoc.destinationDir | |
} | |
artifacts { | |
archives jar | |
archives sourceJar | |
archives javadocJar | |
} | |
signing { | |
sign configurations.archives | |
} | |
publishing { | |
publications { | |
mavenJava(MavenPublication) { | |
customizePom(pom) | |
groupId 'com.github.mautini' | |
artifactId 'schemaorg-java' | |
version '1.0.1' | |
from components.java | |
// create the sign pom artifact | |
pom.withXml { | |
def pomFile = file("${project.buildDir}/generated-pom.xml") | |
writeTo(pomFile) | |
def pomAscFile = signing.sign(pomFile).signatureFiles[0] | |
artifact(pomAscFile) { | |
classifier = null | |
extension = 'pom.asc' | |
} | |
} | |
artifact(sourceJar) { | |
classifier = 'sources' | |
} | |
artifact(javadocJar) { | |
classifier = 'javadoc' | |
} | |
// create the signed artifacts | |
project.tasks.signArchives.signatureFiles.each { | |
artifact(it) { | |
def matcher = it.file =~ /-(sources|javadoc)\.jar\.asc$/ | |
if (matcher.find()) { | |
classifier = matcher.group(1) | |
} else { | |
classifier = null | |
} | |
extension = 'jar.asc' | |
} | |
} | |
} | |
} | |
repositories { | |
maven { | |
url "https://oss.sonatype.org/service/local/staging/deploy/maven2" | |
credentials { | |
username sonatypeUsername | |
password sonatypePassword | |
} | |
} | |
} | |
} | |
def customizePom(pom) { | |
pom.withXml { | |
def root = asNode() | |
// eliminate test-scoped dependencies (no need in maven central POMs) | |
root.dependencies.removeAll { dep -> | |
dep.scope == "test" | |
} | |
// add all items necessary for maven central publication | |
root.children().last() + { | |
resolveStrategy = Closure.DELEGATE_FIRST | |
description 'Serialize and Deserialize Json-LD into Java entities' | |
name 'Schema.org Java' | |
url 'https://github.com/mautini/schemaorg-java' | |
organization { | |
name 'com.github.mautini' | |
url 'https://github.com/mautini' | |
} | |
issueManagement { | |
system 'GitHub' | |
url 'https://github.com/mautini/schemaorg-java/issues' | |
} | |
licenses { | |
license { | |
name 'Apache License 2.0' | |
url 'https://github.com/mautini/schemaorg-java/blob/master/LICENSE' | |
distribution 'repo' | |
} | |
} | |
scm { | |
url 'https://github.com/mautini/schemaorg-java' | |
connection 'scm:git:git://github.com/mautini/schemaorg-java.git' | |
developerConnection 'scm:git:ssh://git@github.com:mautini/schemaorg-java.git' | |
} | |
developers { | |
developer { | |
name 'Mautini' | |
} | |
} | |
} | |
} | |
} | |
model { | |
tasks.generatePomFileForMavenJavaPublication { | |
destination = file("$buildDir/generated-pom.xml") | |
} | |
tasks.publishMavenJavaPublicationToMavenLocal { | |
dependsOn project.tasks.signArchives | |
} | |
tasks.publishMavenJavaPublicationToMavenRepository { | |
dependsOn project.tasks.signArchives | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment