Last active
March 14, 2019 18:53
-
-
Save pram/4ca70968fb6b19566d5df70d89747e62 to your computer and use it in GitHub Desktop.
Gradle build file to upload third-party artifacts to a maven repository
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
//Also check - https://dzone.com/articles/how-to-upload-a-list-of-jars-into-nexus-or-artifac | |
// using gradle 1.2 | |
apply plugin: 'maven' | |
group 'org.example.extlib' | |
version = '1.0.0' | |
ext { | |
// location of external files relative to this build.gradle file | |
extFiles = [ | |
main: 'api/extlib.jar', | |
sources: 'api/extlib-src.zip', | |
javadoc: 'api/extlib-doc.zip' | |
] | |
} | |
uploadArchives { | |
repositories.mavenDeployer { | |
pom.project { | |
name 'External supplier library jar' | |
organization { | |
name 'Example Systems' | |
} | |
} | |
// fake repository used for testing | |
repository( id: 'fakeRepo', url: 'file:repository/' ) | |
} | |
} | |
task sourcesJar( type: Jar ) { | |
classifier = 'sources' | |
from fileTree( dir: extractArchive( extFiles.sources ) ) | |
} | |
task javadocJar( type: Jar ) { | |
classifier = 'javadoc' | |
from fileTree( dir: extractArchive( extFiles.javadoc ) ) | |
} | |
artifacts { | |
archives file: file( extFiles.main ), type: 'jar' | |
archives sourcesJar | |
archives javadocJar | |
} | |
task cleanTmp << { | |
delete 'tmp' | |
} | |
clean.dependsOn cleanTmp | |
//---- Support functions -------------------------------------------- | |
// | |
// Extracts a zip archive file to a temporary directory and returns | |
// the name of the directory | |
String extractArchive( String archiveName ) { | |
def archive = file( archiveName ) | |
def outputDir = tempDirFrom( archiveName ) | |
println 'Extracting ' + archiveName + ' to ' + outputDir | |
if ( archive.isFile() ) { | |
ant.unzip( src: archive, dest: outputDir, overwrite: 'true' ) | |
return outputDir | |
} | |
} | |
// Returns the basename of a Unix-style complex filename | |
String basename( String fileName ) { | |
int dot = fileName.lastIndexOf( '.' ); | |
int sep = fileName.lastIndexOf( '/' ); | |
return fileName.substring( sep + 1, dot ); | |
} | |
// Temporary directory name | |
// | |
// E.g. for "lib/extlib-src.zip" this will be "tmp/extlib-src_tmp" | |
String tempDirFrom( String fileName ) { | |
return 'tmp/' + basename( fileName ) + '_tmp' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment