-
Notifications
You must be signed in to change notification settings - Fork 15
/
OneJarPlugin.groovy
148 lines (132 loc) · 4.6 KB
/
OneJarPlugin.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.smokejumperit.gradle
import org.gradle.api.*
import org.gradle.api.plugins.*
import org.gradle.api.artifacts.*
import org.gradle.api.tasks.*
class OneJarPlugin extends SjitPlugin {
private final String jarName = "one-jar-ant-task-0.98.jar";
void apply(Project project) {
project.apply(plugin:'java')
project.apply(plugin:ExecPlugin)
final oneJarDir = new File(project.rootProject.buildDir, "one-jar")
def root = project.rootProject
if(!root.getTasksByName('unpackOneJar', false)) {
root.task('unpackOneJar') {
description = "Unpacks the one-jar distribution"
onlyIf { !oneJarDir.exists() }
doFirst {
oneJarDir.mkdirs()
def jarFile = writeOutJar(oneJarDir)
ant.unzip(
src:jarFile.absolutePath,
dest:oneJarDir.absolutePath,
failOnEmptyArchive: true
)
}
}
}
if(!root.getTasksByName('cleanOneJar', false)) {
root.task('cleanOneJar', type:Delete) {
delete oneJarDir
}
}
root.gradle.taskGraph.whenReady { graph ->
def oneJarTask = project.tasks.oneJar
if(oneJarTask && graph.hasTask(oneJarTask)) {
def jarTask = project.tasks.jar
if(!jarTask.manifest.attributes.containsKey('Main-Class')) {
throw new InvalidUserDataException(oneJarTask.path + " requires the manifest's Main-Class attribute to be set on " + jarTask.path)
}
}
}
project.task('typedefOneJar', dependsOn:root.tasks.unpackOneJar) {
description = "Defines the one-jar task on ant"
doFirst {
project.ant.property(name:"one-jar.dist.dir", value:oneJarDir.absolutePath)
project.ant.import(file:new File(oneJarDir, "one-jar-ant-task.xml").absolutePath, optional:false)
}
}
project.task('oneJar', dependsOn:[project.tasks.jar, project.tasks.typedefOneJar]) {
def jar = project.tasks.jar
File jarFile = new File(jar.destinationDir, jar.archiveName - ("." + jar.extension) + "-standalone." + jar.extension)
description = "Makes the fat jar file"
inputs.files([jar.outputs.files, project.configurations.getByName("compile"), project.configurations.getByName("runtime")])
outputs.files jarFile
doFirst {
def runConf = project.configurations.getByName("runtime").resolve()
project.logger.debug("Runtime files to consider for OneJar (${runConf.size()}):\n\t${runConf.join("\n\t")}")
def (runtimeDirs, runtimeLibs) = runConf.split { it.isDirectory() }
if (logger.isEnabled(org.gradle.api.logging.LogLevel.DEBUG)) {
System.setProperty("one-jar.verbose", "true")
System.setProperty("one-jar.info", "true")
System.setProperty("one-jar.statistics", "true")
System.setProperty("one-jar.show.properties", "true")
}
def manifestFile = writeOneJarManifestFile(jar)
ant.'one-jar'(destFile:jarFile.absolutePath, manifest:manifestFile.absolutePath) {
ant.main(jar:jar.archivePath.absolutePath) {
runtimeDirs.each { depDir ->
project.logger.debug("Adding ${depDir.absolutePath} to OneJar main")
ant.fileset(dir:depDir.absolutePath)
}
}
ant.lib {
runtimeLibs.each { depFile ->
project.logger.debug("Adding ${depFile.absolutePath} to OneJar lib")
ant.fileset(file:depFile.absolutePath)
}
}
}
Date date = new Date()
String name = jar.baseName
project.artifacts.add('archives',
[
getClassifier: { -> "standalone" },
getDate: {-> date },
getExtension: {-> "jar" },
getType: {-> "jar" },
getFile: {-> jarFile },
getName: {-> name }
] as PublishArtifact
)
}
}
/*
project.task("execOneJar", dependsOn:[project.tasks.oneJar]) {
doFirst {
def workingDir = new File(new File(project.buildDir, "one-jar"), "exec")
workingDir.mkdirs()
def jarFile = project.tasks.oneJar.outputs.files.singleFile
project.execIn(workingDir,
"${System.getProperty("java.home", "")}/bin/java", "-jar", jarFile.absolutePath
)
}
}
*/
}
File writeOneJarManifestFile(jar) {
def manifestFile = File.createTempFile("one-jar-manifest", "mf")
manifestFile.withWriter { writer ->
def manifest = jar.manifest.effectiveManifest
String main = manifest.attributes.remove("Main-Class")
if(main) {
manifest.attributes.put("One-Jar-Main-Class", main)
}
manifest.attributes.put("One-Jar-Show-Expand", false)
manifest.attributes.put("One-Jar-Confirm-Expand", false)
manifest.writeTo(writer)
}
manifestFile.deleteOnExit()
return manifestFile
}
File writeOutJar(File dir) {
def jarFile = new File(dir, jarName)
jarFile.delete()
jarFile.withOutputStream { os ->
this.class.getResourceAsStream("/${jarName}").eachByte { b ->
os.write(b)
}
}
return jarFile
}
}