Issue
You can use mavenCentral()
to download jars and have them added to your classpath automatically. But what if you need to download a tarball and compile a C
library to add to LD_LIBRARY_PATH
in addition to the jar that is needed to compile and run?
Using Gradle, what do you think would be the best approach to incorporate this type of 3rd-party software into your build?
I don't think we can accomplish this declaratively, so my naive approach would be to create a task that downloads and extracts the tarball, compiles the software, and copies the libraries to build/lib or something, and make javaCompile depend upon it. Is there a better way? Would it be wise to put the task and libraries in buildSrc instead?
Our goals are to avoid redistributing software and to make it easier to use our software.
Our first example is HDF Group's HDF5 tarball. If you use this software, what was your approach?
Solution
This is what I came up with. While it accomplishes my needs, I'm open to improving it. In buildSrc/build.gradle
, I have:
apply from: "script-plugins/hdf5.gradle"
Here is hdf5.gradle
:
// Download and build HDF5.
//
// TODO Next time around, parameterize the version and possibly the URL
task buildHdf5() {
def tmp = file("$buildDir/tmp/hdf5")
def hdf5 = file("$tmp/hdf5-1.12.2")
def lib = file("$buildDir/lib")
outputs.file "$lib/jarhdf5-1.12.2.jar"
doLast() {
tmp.mkdirs()
// Use the SHA256 links at https://www.hdfgroup.org/downloads/hdf5/source-code/ to determine the URL.
exec {
workingDir tmp
commandLine "curl", "-o", "hdf5-1.12.2.#1", "https://hdf-wordpress-1.s3.amazonaws.com/wp-content/uploads/manual/HDF5/HDF5_1_12_2/source/hdf5-1.12.2.{tar.bz2,sha256}"
}
exec {
workingDir tmp
commandLine "tar", "-xf", "hdf5-1.12.2.tar.bz2"
}
exec {
workingDir hdf5
commandLine "sh", "-c", "./configure --with-zlib=/usr --prefix=$buildDir --enable-threadsafe --with-pthread=/usr --enable-unsupported --enable-java"
}
exec {
workingDir hdf5
commandLine "make"
}
exec {
workingDir hdf5
commandLine "make", "install"
}
copy {
from("$tmp/lib") {
include "*.jar"
}
into "$lib"
}
}
}
assemble.dependsOn buildHdf5
In gradle.properties
, I have:
ziggyDependencies = buildSrc/build
In build.gradle
, I have:
ext.ziggyDependencies = "$rootDir/$ziggyDependencies"
…
repositories {
mavenCentral()
flatDir {
dirs "$ziggyDependencies/lib"
}
}
…
dependencies {
…
compile ':jarhdf5:1.12.+'
…
And that's it!
Answered By - Bill Wohler
Answer Checked By - Mary Flores (JavaFixing Volunteer)