-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathExtraJavaModuleInfoPluginExtension.java
More file actions
329 lines (295 loc) · 13.6 KB
/
ExtraJavaModuleInfoPluginExtension.java
File metadata and controls
329 lines (295 loc) · 13.6 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// SPDX-License-Identifier: Apache-2.0
package org.gradlex.javamodule.moduleinfo;
import javax.annotation.Nullable;
import javax.inject.Inject;
import org.gradle.api.Action;
import org.gradle.api.NamedDomainObjectProvider;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ConfigurationContainer;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.MinimalExternalModuleDependency;
import org.gradle.api.attributes.Attribute;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.MapProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.SourceSet;
/**
* A data class to collect all the module information we want to add.
* Here the class is used as extension that can be configured in the build script
* and as input to the ExtraModuleInfoTransform that add the information to Jars.
*/
@SuppressWarnings("unused")
public abstract class ExtraJavaModuleInfoPluginExtension {
static Attribute<Boolean> JAVA_MODULE_ATTRIBUTE = Attribute.of("javaModule", Boolean.class);
@Inject
protected abstract ObjectFactory getObjects();
@Inject
protected abstract ConfigurationContainer getConfigurations();
public abstract MapProperty<String, ModuleSpec> getModuleSpecs();
public abstract Property<Boolean> getFailOnMissingModuleInfo();
public abstract Property<Boolean> getFailOnAutomaticModules();
public abstract Property<Boolean> getFailOnModifiedDerivedModuleNames();
public abstract Property<Boolean> getSkipLocalJars();
public abstract Property<Boolean> getDeriveAutomaticModuleNamesFromFileNames();
public abstract Property<String> getVersionsProvidingConfiguration();
public abstract Property<Dependency> getPlatformDependency();
/**
* Add full module information for a given Jar file.
*
* @param identifier group:name coordinates _or_ Jar file name
* @param moduleName the Module Name of the Module to construct
*/
public void module(String identifier, String moduleName) {
module(identifier, moduleName, (String) null);
}
/**
* Add full module information for a given Jar file.
*
* @param alias group:name coordinates alias from version catalog
* @param moduleName the Module Name of the Module to construct
*/
public void module(Provider<MinimalExternalModuleDependency> alias, String moduleName) {
module(alias.get().getModule().toString(), moduleName);
}
/**
* Add full module information for a given Jar file.
*
* @param identifier group:name coordinates _or_ Jar file name
* @param moduleName the Module Name of the Module to construct
* @param moduleVersion version to write into the module-info.class
*/
public void module(String identifier, String moduleName, @Nullable String moduleVersion) {
module(identifier, moduleName, moduleVersion, m -> {
m.exportAllPackages();
if (identifier.contains(":")) { // only if the identifier is a coordinates (not a Jar)
m.requireAllDefinedDependencies();
}
});
}
/**
* Add full module information for a given Jar file.
*
* @param alias group:name coordinates alias from version catalog
* @param moduleName the Module Name of the Module to construct
* @param moduleVersion version to write into the module-info.class
*/
public void module(
Provider<MinimalExternalModuleDependency> alias, String moduleName, @Nullable String moduleVersion) {
module(alias.get().getModule().toString(), moduleName, moduleVersion);
}
/**
* Add full module information for a given Jar file.
*
* @param identifier group:name coordinates _or_ Jar file name
* @param moduleName the Module Name of the Module to construct
* @param conf configure exported packages and dependencies, see {@link ModuleInfo}
*/
public void module(String identifier, String moduleName, @Nullable Action<? super ModuleInfo> conf) {
module(identifier, moduleName, null, conf);
}
/**
* Add full module information for a given Jar file.
*
* @param alias group:name coordinates alias from version catalog
* @param moduleName the Module Name of the Module to construct
* @param conf configure exported packages and dependencies, see {@link ModuleInfo}
*/
public void module(
Provider<MinimalExternalModuleDependency> alias,
String moduleName,
@Nullable Action<? super ModuleInfo> conf) {
module(alias.get().getModule().toString(), moduleName, conf);
}
/**
* Add full module information for a given Jar file.
*
* @param identifier group:name coordinates _or_ Jar file name
* @param moduleName the Module Name of the Module to construct
* @param moduleVersion version to write into the module-info.class
* @param conf configure exported packages, dependencies and Jar merging, see {@link ModuleInfo}
*/
public void module(
String identifier,
String moduleName,
@Nullable String moduleVersion,
@Nullable Action<? super ModuleInfo> conf) {
ModuleInfo moduleInfo = new ModuleInfo(identifier, moduleName, moduleVersion, getObjects());
if (conf != null) {
conf.execute(moduleInfo);
}
this.getModuleSpecs().put(identifier, moduleInfo);
}
/**
* Add full module information for a given Jar file.
*
* @param alias group:name coordinates alias from version catalog
* @param moduleName the Module Name of the Module to construct
* @param moduleVersion version to write into the module-info.class
* @param conf configure exported packages, dependencies and Jar merging, see {@link ModuleInfo}
*/
public void module(
Provider<MinimalExternalModuleDependency> alias,
String moduleName,
@Nullable String moduleVersion,
@Nullable Action<? super ModuleInfo> conf) {
module(alias.get().getModule().toString(), moduleName, moduleVersion, conf);
}
/**
* Add an Automatic-Module-Name to a given Jar file.
*
* @param identifier group:name coordinates _or_ Jar file name
* @param moduleName the Module Name of the Module to construct
*/
public void automaticModule(String identifier, String moduleName) {
automaticModule(identifier, moduleName, null);
}
/**
* Add an Automatic-Module-Name to a given Jar file.
*
* @param alias group:name coordinates alias from version catalog
* @param moduleName the Module Name of the Module to construct
*/
public void automaticModule(Provider<MinimalExternalModuleDependency> alias, String moduleName) {
automaticModule(alias.get().getModule().toString(), moduleName, null);
}
/**
* Add an Automatic-Module-Name to a given Jar file.
*
* @param identifier group:name coordinates _or_ Jar file name
* @param moduleName the Module Name of the Module to construct
* @param conf configure Jar merging, see {@link AutomaticModuleName}
*/
public void automaticModule(
String identifier, String moduleName, @Nullable Action<? super AutomaticModuleName> conf) {
AutomaticModuleName automaticModuleName = new AutomaticModuleName(identifier, moduleName);
if (conf != null) {
conf.execute(automaticModuleName);
}
getModuleSpecs().put(identifier, automaticModuleName);
}
/**
* Add an Automatic-Module-Name to a given Jar file.
*
* @param alias group:name coordinates alias from version catalog
* @param moduleName the Module Name of the Module to construct
* @param conf configure Jar merging, see {@link AutomaticModuleName}
*/
public void automaticModule(
Provider<MinimalExternalModuleDependency> alias,
String moduleName,
@Nullable Action<? super AutomaticModuleName> conf) {
automaticModule(alias.get().getModule().toString(), moduleName, conf);
}
/**
* Let the plugin know about an existing module on the module path.
* This may be needed when 'requiresDirectivesFromMetadata(true)' is used.
*
* @param coordinates group:name coordinates
* @param moduleName the Module Name of the Module referred to by the coordinates
*/
public void knownModule(String coordinates, String moduleName) {
getModuleSpecs().put(coordinates, new KnownModule(coordinates, moduleName));
}
/**
* Let the plugin know about an existing module on the module path.
* This may be needed when 'requiresDirectivesFromMetadata(true)' is used.
*
* @param alias group:name coordinates alias from version catalog
* @param moduleName the Module Name of the Module referred to by the coordinates
*/
public void knownModule(Provider<MinimalExternalModuleDependency> alias, String moduleName) {
knownModule(alias.get().getModule().toString(), moduleName);
}
/**
* Activate the plugin's functionality for dependencies of all scopes of the given source set
* (runtimeClasspath, compileClasspath, annotationProcessor).
* Note that the plugin activates the functionality for all source sets by default.
* Therefore, this method only has an effect for source sets for which a {@link #deactivate(SourceSet)}
* has been performed.
*
* @param sourceSet the Source Set to activate (e.g. sourceSets.test)
*/
public void activate(SourceSet sourceSet) {
NamedDomainObjectProvider<Configuration> runtimeClasspath =
getConfigurations().named(sourceSet.getRuntimeClasspathConfigurationName());
NamedDomainObjectProvider<Configuration> compileClasspath =
getConfigurations().named(sourceSet.getCompileClasspathConfigurationName());
NamedDomainObjectProvider<Configuration> annotationProcessor =
getConfigurations().named(sourceSet.getAnnotationProcessorConfigurationName());
activate(runtimeClasspath);
activate(compileClasspath);
activate(annotationProcessor);
}
/**
* Activate the plugin's functionality for a single resolvable Configuration.
* This is useful to use the plugins for scopes that are not tied to a Source Set,
* for which the plugin does not activate automatically.
*
* @param resolvable a resolvable Configuration (e.g. configurations["customClasspath"])
*/
public void activate(Configuration resolvable) {
resolvable.getAttributes().attribute(JAVA_MODULE_ATTRIBUTE, true);
}
/**
* Variant of {@link #activate(SourceSet)} and {@link #activate(Configuration)} that accepts either a
* Provider of {@link SourceSet} or a Provider of {@link Configuration}. This is a convenience to use
* notations like 'activate(sourceSets.main)' in Kotlin DSL.
*
* @param sourceSetOrResolvable the Source Set or Configuration to activate
*/
public void activate(NamedDomainObjectProvider<?> sourceSetOrResolvable) {
Object realized = sourceSetOrResolvable.get();
if (realized instanceof SourceSet) {
activate((SourceSet) realized);
} else if (realized instanceof Configuration) {
activate((Configuration) realized);
} else {
throw new RuntimeException("Not SourceSet or Configuration: " + realized);
}
}
/**
* Deactivate the plugin's functionality for dependencies of all scopes of the given source set
* (runtimeClasspath, compileClasspath, annotationProcessor).
*
* @param sourceSet the Source Set to deactivate (e.g. sourceSets.test)
*/
public void deactivate(SourceSet sourceSet) {
NamedDomainObjectProvider<Configuration> runtimeClasspath =
getConfigurations().named(sourceSet.getRuntimeClasspathConfigurationName());
NamedDomainObjectProvider<Configuration> compileClasspath =
getConfigurations().named(sourceSet.getCompileClasspathConfigurationName());
NamedDomainObjectProvider<Configuration> annotationProcessor =
getConfigurations().named(sourceSet.getAnnotationProcessorConfigurationName());
deactivate(runtimeClasspath);
deactivate(compileClasspath);
deactivate(annotationProcessor);
}
/**
* Deactivate the plugin's functionality for a single resolvable Configuration.
* This is useful if selected scopes do not use the Module Path and therefore
* module information is not required.
*
* @param resolvable a resolvable Configuration (e.g. configurations.annotationProcessor)
*/
public void deactivate(Configuration resolvable) {
resolvable.getAttributes().attribute(JAVA_MODULE_ATTRIBUTE, false);
}
/**
* Variant of {@link #deactivate(SourceSet)} and {@link #deactivate(Configuration)} that accepts either a
* Provider of {@link SourceSet} or a Provider of {@link Configuration}. This is a convenience to use
* notations like 'deactivate(sourceSets.test)' in Kotlin DSL.
*
* @param sourceSetOrResolvable the Source Set or Configuration to activate
*/
public void deactivate(NamedDomainObjectProvider<?> sourceSetOrResolvable) {
Object realized = sourceSetOrResolvable.get();
if (realized instanceof SourceSet) {
deactivate((SourceSet) realized);
} else if (realized instanceof Configuration) {
deactivate((Configuration) realized);
} else {
throw new RuntimeException("Not SourceSet or Configuration: " + realized);
}
}
}