forked from iDiamondhunter/More-bows
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild.gradle
193 lines (160 loc) · 7.07 KB
/
build.gradle
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
buildscript {
repositories {
mavenCentral()
maven { url = "https://repo1.maven.org/maven2/" }
maven { url = "https://maven.google.com/" }
maven {
name = "forge"
url = "https://maven.minecraftforge.net/"
}
maven {
name = "sonatype"
url = "https://oss.sonatype.org/content/repositories/snapshots/"
}
}
dependencies {
classpath ('com.anatawa12.forge:ForgeGradle:1.2-1.1.+') {
changing = true
}
classpath 'com.guardsquare:proguard-gradle:7.3.2'
}
}
plugins {
id 'java-library'
id 'maven-publish'
}
// Reproducible builds! https://docs.gradle.org/4.9/userguide/working_with_files.html#sec:reproducible_archives
tasks.withType(AbstractArchiveTask).configureEach {
preserveFileTimestamps = false
reproducibleFileOrder = true
}
apply plugin: 'java'
apply plugin: 'forge'
// Defines a Java 7 compatible JDK. Java 6 would be ideal, but it isn't usable on my computer anymore :(
// This is used to get properties requiring a reference to a JDK (rt.jar etc).
// Unofficially, the codebase of MoreBows attempts to retain Java 5 compatibility, for ease of backports in the future, but Gradle doesn't support building with Java 5 in toolchains yet. I should make a bug report about it at some point...
def compiler = javaToolchains.compilerFor {
languageVersion = JavaLanguageVersion.of(7)
}
// Tells Gradle to use the previously defined Java 7 compatible JDK.
java {
toolchain {
compiler
}
}
// Tells Gradle to always compile with the previously defined Java 7 compatible JDK.
tasks.withType(JavaCompile).configureEach {
javaCompiler = compiler
}
import groovy.json.JsonSlurper
import groovy.json.JsonOutput
version = project.mod_version
group = project.mod_group
def modName = project.mod_name
def build_release = System.getenv("BUILD_RELEASE") == "true"
// Add snapshot suffix to version if not building a release.
// To build a release, set the environment variable BUILD_RELEASE to true.
if (!build_release) {
version += "-SNAPSHOT"
}
// Information for ForgeGradle to configure the Minecraft / Forge version.
minecraft {
version = project.minecraft_version + "-" + project.forge_version
runDir = "run"
}
processResources {
// This will ensure that this task is redone when the versions change.
inputs.property "version", project.version
inputs.property "mcversion", project.minecraft.version
inputs.property "modName", project.mod_name
// Replace values in only mcmod.info.
filesMatching('mcmod.info') {
expand 'version': version, 'modName': modName, 'mcversion': project.minecraft.version
}
// Here's a super unnecessary micro-optimisation: a mcmod.info file is a JSON file, and JSON files can be minified!
doLast {
fileTree(dir: outputs.files.asPath, include: "**/mcmod.info").each {
File file -> file.text = JsonOutput.toJson(new JsonSlurper().parse(file))
}
}
}
tasks.withType(JavaCompile).configureEach {
// Ensures that the encoding of source files is set to UTF-8, see http://yodaconditions.net/blog/fix-for-java-file-encoding-problems-with-gradle.html
options.encoding = "UTF-8"
}
// This task creates a .jar file containing the source code of this mod.
task sourcesJar(type: Jar, dependsOn: classes) {
archiveClassifier = "sources"
from sourceSets.main.allSource
}
// This task creates a .jar file containing a deobfuscated version of this mod, for ProGuard to minimise.
task unmappedJar(type: Jar) {
archiveClassifier = "unmapped"
from sourceSets.main.output
}
// This task uses ProGuard to shrink the built .jar's file size and optimise the bytecode. TODO Cleanup.
task proguard(dependsOn: unmappedJar, type: proguard.gradle.ProGuardTask) {
verbose
// Use the non-remapped .jar as the input file, because ForgeGradle has a convenient task to map it to obfuscate Minecraft class names for us when we're done optimising it
injars "build/libs/" + rootProject.name + "-" + version + "-unmapped.jar"
outjars "build/libs/" + rootProject.name + "-" + version + ".jar"
// Get the location of rt.jar from the provided toolchain.
// Note to self: probably support building with not outdated versions of Java in the future.
libraryjars compiler.get().metadata.installationPath.asFile.absolutePath + "/lib/rt.jar"
// Probably linux?
libraryjars compiler.get().metadata.installationPath.asFile.absolutePath + "/jre/lib/rt.jar"
// Dumb MacOS stuff, Apple's Legacy JDK 6 "rt.jar" equivalent is not in any standard place
libraryjars compiler.get().metadata.installationPath.asFile.absolutePath + "/../Classes/classes.jar"
// Add relevant Minecraft libraries to ProGuard's list of libraries to obfuscate against. See ForgeGradle 1.2's UserConstants.
libraryjars project.configurations.minecraft
libraryjars project.configurations.minecraftDeps
// The rest of the configs are in this file
configuration 'guard.pro'
}
def optimisedJar = proguard.outputs.files.singleFile
// This task remaps the ProGuard obfuscated .jar file from MCP mappings into Minecraft mappings. TODO Find a less cursed way to do this that doesn't only half-remap the .jar file, document more.
task remapProguardProperly(dependsOn: ['genSrgs', 'proguard'], type: net.minecraftforge.gradle.tasks.user.reobf.ReobfTask) {
def forgeGradlePlugin = plugins.findPlugin 'forge'
exceptorCfg = forgeGradlePlugin.delayedFile net.minecraftforge.gradle.user.UserConstants.EXC_SRG
srg = forgeGradlePlugin.delayedFile net.minecraftforge.gradle.user.UserConstants.REOBF_SRG
fieldCsv = forgeGradlePlugin.delayedFile net.minecraftforge.gradle.user.UserConstants.FIELD_CSV
fieldCsv = forgeGradlePlugin.delayedFile net.minecraftforge.gradle.user.UserConstants.METHOD_CSV
mcVersion = forgeGradlePlugin.delayedString '{MC_VERSION}'
reobf(optimisedJar) { arg ->
def javaConv = project.convention.plugins.get 'java'
arg.classpath = javaConv.getSourceSets().getByName('main').compileClasspath
}
extraSrg = forgeGradlePlugin.extension.srgExtra
afterEvaluate {
if (forgeGradlePlugin.extension.decomp) {
deobfFile = tasks.deobfuscateJar.delayedOutput
recompFile = forgeGradlePlugin.delayedDirtyFile forgeGradlePlugin.srcDepName, null, 'jar'
}
}
}
// TODO Not sure if this should be kept.
reobf.dependsOn 'remapProguardProperly'
jar {
archiveClassifier = "debug" // Contains things like variables with actual names, line numbers etc. Useful for debugging, but these things take up file size, so they have to go.
}
// Creates the listed artifacts on building the mod.
artifacts {
archives sourcesJar
archives optimisedJar
}
build.finalizedBy(cleanUnmappedJar)
publishing {
publications {
mavenJava(MavenPublication) {
artifact(jar) {
builtBy build
}
artifact(sourcesJar) {
builtBy sourcesJar
}
artifact(optimisedJar) {
builtBy remapProguardProperly
}
}
}
}