Skip to content

Instantly share code, notes, and snippets.

@am4dr
Last active November 11, 2017 01:43
Show Gist options
  • Save am4dr/9257ee7f1020bc948cc32a19c3b976c5 to your computer and use it in GitHub Desktop.
Save am4dr/9257ee7f1020bc948cc32a19c3b976c5 to your computer and use it in GitHub Desktop.
make automatic modules modular
configurations {
reactivestreams
rxjava2
rxjava2_interop
}
dependencies {
reactivestreams 'org.reactivestreams:reactive-streams:1.0.1'
rxjava2 'io.reactivex.rxjava2:rxjava:2.1.5'
rxjava2_interop 'com.github.akarnokd:rxjava2-jdk9-interop:0.1.5'
}
task modularizeReactiveStreams(type: ModularizeJar) {
targetJar = configurations.reactivestreams.resolve()[0]
}
task modularizeRxJava2(type: ModularizeJar, dependsOn: modularizeReactiveStreams) {
modulePath = files(modularizeReactiveStreams.modularJar)
addModules = ['reactive.streams']
targetJar = configurations.rxjava2.resolve()[0]
}
task modularizeRxJava2Interop(type: ModularizeJar, dependsOn: modularizeRxJava2) {
modulePath = files(modularizeRxJava2.modularJar, modularizeReactiveStreams.modularJar)
addModules = ['reactive.streams', 'io.reactivex.rxjava2']
targetJar = configurations.rxjava2_interop.resolve()[0]
}
//task jlink(type: Exec) {
// dependsOn ':launcher:installDist', modularizeReactiveStreams, modularizeRxJava2, modularizeRxJava2Interop
// inputs.files(modularizeReactiveStreams.modularJar, modularizeRxJava2.modularJar, modularizeRxJava2Interop.modularJar)
// inputs.dir('launcher/build/install/launcher/lib')
// //outputs.dir("$buildDir/jlink/<app-name>")
// executable 'jlink'
// args '--module-path', "${System.getProperty('java.home')}\\jmods${File.pathSeparator}${files(modularizeReactiveStreams.modularJar, modularizeRxJava2.modularJar, modularizeRxJava2Interop.modularJar).asPath}${File.pathSeparator}launcher/build/install/launcher/lib",
// '--compress=2',
// '--output', "$buildDir/jlink/<app-name>",
// '--add-modules', '<app-module-name>',
// // '--launcher', 'fusen=<app-module-name>/<main-class-name>'
// doFirst {
// delete outputs
// }
//}
class ModularizeJar extends DefaultTask {
File targetJar
File outputDir
FileCollection modulePath = project.files()
List<String> addModules = []
@Input
List<String> getAddModules() { return this.addModules }
@Input
File getTargetJar() { return this.targetJar }
@InputFiles
FileCollection getModulePath() { return this.modulePath }
@OutputDirectory
File getOutputDir() {
if (!this.outputDir) {
this.outputDir = project.file("$project.buildDir/modularize/$targetJar.name")
}
return this.outputDir
}
@OutputFile
File getModularJar() { return project.file("${getOutputDir()}/$targetJar.name") }
String getExtractDir() { return "$outputDir/extract" }
@TaskAction
void process() {
logger.debug "targetJar: $targetJar, outputDir: $outputDir, modular-jar: $modularJar, modules: ${modulePath?.asPath ?: 'not-specified'}"
final mod = ['--module-path', "${System.getProperty('java.home')}/jmods$File.pathSeparator$modulePath.asPath" ]
final add = addModules.collectMany { ['--add-modules', it] }
project.file(extractDir).mkdir()
project.exec {
workingDir = outputDir
commandLine = ['jdeps', '--generate-module-info', extractDir, *mod, *add, targetJar.toString()]
logger.info commandLine.join(' ')
}
final moduleInfoSrc = project.fileTree(outputDir) { include '**/module-info.java' }
final moduleName = moduleInfoSrc.files[0].parentFile.name
project.exec {
workingDir = outputDir
executable = 'javac'
args = [
'-d', extractDir,
*mod,
*add,
"--patch-module", "$moduleName=$targetJar",
moduleInfoSrc.asPath,
]
logger.info commandLine.join(' ')
}
project.copy {
from targetJar
into outputDir
}
project.exec {
workingDir = outputDir
commandLine = ['jar', 'uf', modularJar.toString(), '-C', extractDir, 'module-info.class']
logger.info commandLine.join(' ')
}
project.delete extractDir
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment