Repository navigation
Expand file tree
/
Copy pathbuild.gradle
More file actions
222 lines (196 loc) · 9.62 KB
/
Copy pathbuild.gradle
File metadata and controls
222 lines (196 loc) · 9.62 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// For the Gradle Plugin Portal: publishPlugins, run by the release workflow once Maven Central serves the
// version's jars and native images — the plugins depend on them. The key comes from GRADLE_PUBLISH_KEY and
// GRADLE_PUBLISH_SECRET. It applies java-gradle-plugin and maven-publish itself, and once the signing
// plugin is applied it signs what it uploads.
apply plugin: 'com.gradle.plugin-publish'
apply plugin: 'groovy'
apply plugin: 'signing'
apply plugin: 'open-java-format.publishing-conventions'
description = 'Gradle plugins for open-java-format: formatting tasks, the Spotless step and IntelliJ configuration'
configurations {
pluginClasspath {
canBeConsumed = false
canBeResolved = true
}
}
dependencies {
compileOnly libs.spotless.gradlePlugin
implementation gradleApi()
implementation libs.guava
implementation project(':open-java-format-spi')
implementation project(':open-java-format-jdk-bootstrap')
testImplementation project(':open-java-format')
testImplementation libs.junit.jupiter
testImplementation libs.assertj
testImplementation libs.jackson.dataformat.xml
// @Language on the XML fixtures; used to arrive transitively via gradle-plugin-testing.
testImplementation libs.jetbrainsAnnotations
testRuntimeOnly libs.junit.vintage.engine
// Plain Gradle TestKit drives the generated projects; see
// src/test/java/com/palantir/javaformat/gradle/testing/GradleTestProject.java.
testImplementation gradleTestKit()
// Third-party plugins the generated test projects apply. java-gradle-plugin injects this
// alongside the plugin under test, so `plugins { id '...' }` resolves them without a version.
pluginClasspath libs.spotless.gradlePlugin
}
gradlePlugin {
website = 'https://github.com/openjavaformat/open-java-format'
vcsUrl = 'https://github.com/openjavaformat/open-java-format'
plugins {
openJavaFormat {
id = 'dev.openjavaformat.java-format'
implementationClass = 'com.palantir.javaformat.gradle.PalantirJavaFormatPlugin'
description = 'A modern, lambda-friendly, 120 character Java formatter. Applies all other open-java-format plugins.'
displayName = 'Open Java Format'
tags.set(['java', 'style'])
}
openJavaFormatIdea {
id = 'dev.openjavaformat.java-format-idea'
implementationClass = 'com.palantir.javaformat.gradle.PalantirJavaFormatIdeaPlugin'
description = 'Plugin to configure the open-java-format IDEA plugin based on an optional implementation version of the formatter.'
displayName = 'Open Java Format Idea'
tags.set(['java', 'style'])
}
openJavaFormatSpotless {
id = 'dev.openjavaformat.java-format-spotless'
implementationClass = 'com.palantir.javaformat.gradle.PalantirJavaFormatSpotlessPlugin'
description = 'If spotless is applied, configures a java step that formats using open-java-format.'
displayName = 'Open Java Format Spotless'
tags.set(['java', 'style'])
}
openJavaFormatProvider {
id = 'dev.openjavaformat.java-format-provider'
implementationClass = 'com.palantir.javaformat.gradle.PalantirJavaFormatProviderPlugin'
description = 'Declares palantirJavaFormat, which overrides the formatter that comes inside the open-java-format plugins'
displayName = 'Open Java Format Provider'
tags.set(['java', 'style'])
}
}
}
tasks.withType(PluginUnderTestMetadata).configureEach {
// `from(...)`, not `from += ...`: the latter reads the collection and reassigns it, dropping the
// task dependency on `jar` that java-gradle-plugin put there. It only shows up on a clean build,
// where the jar has not been produced yet.
pluginClasspath.from(configurations.pluginClasspath)
}
configurations {
impl {
canBeConsumed = false
canBeResolved = true
}
formatterNativeImage {
canBeConsumed = false
canBeResolved = true
}
}
dependencies {
impl project(':open-java-format')
formatterNativeImage(project(':open-java-format-native')) {
attributes {
attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, project.objects.named(LibraryElements, 'nativeImage'))
}
}
}
tasks.register("copyNativeImage", Copy.class) {
from(configurations.formatterNativeImage)
// Named like the published artifact, whose extension is its artifact type: bin, and exe on Windows.
// The plugin's ExecutableTransform starts from that type, so a Windows binary must keep .exe.
rename { fileName ->
fileName.endsWith('.exe') ? fileName : String.format("%s.bin", fileName)
}
into("$buildDir/nativeImage")
}
// Split in two so the jar build does not need a GraalVM image: the tests that declare the
// formatter themselves read impl.classpath, which is also what bundleFormatter puts inside the
// plugin; only the ones that drive the binary read nativeImage.path.
//
// Everything the doLast bodies touch is captured here, at configuration time: reaching for
// `configurations` or `tasks` from inside them would pull the whole script into the task and
// the configuration cache rejects that.
def implFiles = configurations.impl
def implClasspathFile = layout.buildDirectory.file("impl.classpath")
def nativeImageDir = layout.buildDirectory.dir("nativeImage")
def nativeImagePathFile = layout.buildDirectory.file("nativeImage.path")
def writeImplClasspath = tasks.register("writeImplClasspath") {
inputs.files(implFiles)
outputs.file(implClasspathFile)
doLast {
implClasspathFile.get().asFile.text = implFiles.asPath
}
}
// The Java-based formatter travels inside the plugin, so that a build needs no repository and no dependency for it,
// and runs the formatter of the plugin's own commit (#20). The jars go in unchanged, each with its own LICENSE and
// NOTICE, next to jars.txt: a line per jar, "<sha256> <name>", in the order of the classpath. BundledFormatter
// extracts them on first use.
def bundledFormatterDir = layout.buildDirectory.dir("generated/bundledFormatter")
def bundledFormatterPath = "open-java-format/formatter"
def bundleFormatter = tasks.register("bundleFormatter", Sync) {
into(bundledFormatterDir)
into(bundledFormatterPath) {
from(implFiles)
}
doLast {
def lines = implFiles.files.collect { jar ->
"${java.security.MessageDigest.getInstance('SHA-256').digest(jar.bytes).encodeHex()} ${jar.name}"
}
bundledFormatterDir.get().file("${bundledFormatterPath}/jars.txt").asFile.text = lines.join('\n') + '\n'
}
}
sourceSets.main.resources.srcDir(bundleFormatter)
// The sources jar takes the resources too; the formatter's jars are not the plugin's sources.
tasks.named('sourcesJar', Jar) {
exclude("${bundledFormatterPath}/**")
}
def writeNativeImagePath = tasks.register("writeNativeImagePath") {
inputs.files(tasks.named("copyNativeImage"))
outputs.file(nativeImagePathFile)
doLast {
nativeImagePathFile.get().asFile.text = nativeImageDir.get().asFile.listFiles()[0].absolutePath
}
}
tasks.named("test").configure {
dependsOn(writeImplClasspath)
// The Gradle the TestKit builds run on, read by GradleTestProject, which defaults to the oldest Gradle 9
// the plugins support. CI's jdk legs pass the oldest release that runs on their JDK; empty means none.
def testkitGradleVersion = providers.gradleProperty('testkitGradleVersion').filter { !it.isEmpty() }
if (testkitGradleVersion.isPresent()) {
systemProperty 'testkit.gradleVersion', testkitGradleVersion.get()
}
if (rootProject.ext.nativeImageRequested) {
dependsOn(writeNativeImagePath)
} else {
// These run the GraalVM binary through NativeImageFormatterService. Without
// -PnativeImage=true there is no binary to run, so they are left to the per-platform
// `native` CI jobs, which pass it. Keep this list in sync with the test classes that
// read build/nativeImage.path.
exclude '**/PalantirJavaFormatPluginTest.class'
exclude '**/PalantirJavaFormatSpotlessPluginTest.class'
exclude '**/FormatDiffTest.class'
}
}
// Signed with the release key, which also signs the Maven Central artifacts and the GitHub release. Only the
// release job has it, in JRELEASER_GPG_SECRET_KEY and JRELEASER_GPG_PASSPHRASE; anywhere else nothing is
// signed.
def signingKey = providers.environmentVariable('JRELEASER_GPG_SECRET_KEY').getOrNull()
def signed = signingKey != null
signing {
required = false
if (signed) {
useInMemoryPgpKeys(signingKey, providers.environmentVariable('JRELEASER_GPG_PASSPHRASE').get())
}
}
// The Portal never takes a version back, so only a signed release version goes up — X.Y.Z or X.Y.Z.N, as for
// Maven Central in open-java-format.release-conventions. --validate-only checks the metadata of any version.
def portalVersion = provider { project.version.toString() }
tasks.named('publishPlugins') {
doFirst {
if (!validateOnly.getOrElse(false) && !signed) {
throw new GradleException('Refusing to publish unsigned plugins to the Gradle Plugin Portal: set '
+ 'JRELEASER_GPG_SECRET_KEY and JRELEASER_GPG_PASSPHRASE, or pass --validate-only.')
}
if (!validateOnly.getOrElse(false) && !(portalVersion.get() ==~ /\d+\.\d+\.\d+(\.\d+)?/)) {
throw new GradleException("Refusing to publish ${portalVersion.get()} to the Gradle Plugin Portal: check "
+ 'out a release tag on a clean working tree, or pass --validate-only.')
}
}
}