-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathJavaModuleDependenciesExtension.java
More file actions
527 lines (470 loc) · 24 KB
/
JavaModuleDependenciesExtension.java
File metadata and controls
527 lines (470 loc) · 24 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/*
* Copyright the GradleX team.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradlex.javamodule.dependencies;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ConfigurationContainer;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.ExternalDependency;
import org.gradle.api.artifacts.ModuleDependency;
import org.gradle.api.artifacts.ProjectDependency;
import org.gradle.api.artifacts.VersionCatalog;
import org.gradle.api.artifacts.VersionCatalogsExtension;
import org.gradle.api.artifacts.VersionConstraint;
import org.gradle.api.artifacts.dsl.DependencyHandler;
import org.gradle.api.attributes.Bundling;
import org.gradle.api.attributes.Category;
import org.gradle.api.attributes.LibraryElements;
import org.gradle.api.attributes.Usage;
import org.gradle.api.attributes.java.TargetJvmEnvironment;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.Directory;
import org.gradle.api.file.ProjectLayout;
import org.gradle.api.file.RegularFileProperty;
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.provider.ProviderFactory;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskProvider;
import org.gradlex.javamodule.dependencies.internal.utils.ModuleInfo;
import org.gradlex.javamodule.dependencies.internal.utils.ModuleInfoCache;
import org.gradlex.javamodule.dependencies.tasks.SyntheticModuleInfoFoldersGenerate;
import org.jspecify.annotations.Nullable;
import javax.inject.Inject;
import java.io.CharArrayReader;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
import static java.util.Optional.empty;
import static org.gradlex.javamodule.dependencies.internal.utils.DependencyDeclarationsUtil.copyVersionConstraint;
import static org.gradlex.javamodule.dependencies.internal.utils.ModuleInfo.Directive.REQUIRES_RUNTIME;
/**
* - Configure behavior of the 'java-module-dependencies' plugin
* - Add additional mappings using {@link #getModuleNameToGA()}
* - Define dependencies and dependency constraints by Module Name
* using {@link #ga(String)}, {@link #gav(String, String)} or {@link #gav(String)}
*/
public abstract class JavaModuleDependenciesExtension {
static final String JAVA_MODULE_DEPENDENCIES = "javaModuleDependencies";
private final @Nullable VersionCatalogsExtension versionCatalogs;
public abstract Property<ModuleInfoCache> getModuleInfoCache();
/**
* Custom mappings can be defined in a property files in your build.
* The default location for this file is 'gradle/modules.properties' (relative to root project).
* Here, the location of the file can be changed.
*/
public abstract RegularFileProperty getModulesProperties();
/**
* Register mapping from Module Name to GA Coordinates (and optionally Capability Coordinates).
* - moduleNameToGA.put("org.slf4j", "org.slf4j:slf4j-api")
* - moduleNameToGA.put("org.slf4j.test.fixtures", "org.slf4j:slf4j-api|org.slf4j:slf4j-api-test-fixtures")
*
* @return the mappings from Module Name to GA coordinates; can be modified
*/
public abstract MapProperty<String, String> getModuleNameToGA();
/**
* If your Module Names all start with a common prefix (e.g. 'com.example.product.module.') followed by a
* name that corresponds to the artifact name and all have the group (e.g. 'com.example.product'), you can
* register a mapping for all these Modules. and with that allow Gradle to map them correctly even if you
* publish some of your Modules or use included builds.
* <p>
* moduleNamePrefixToGroup.put("com.example.product.module.", "com.example.product")
*
* @return the mappings from 'Module Name Prefix' to 'group'
*/
public abstract MapProperty<String, String> getModuleNamePrefixToGroup();
/**
* @return no-op
*/
@Deprecated
public abstract Property<Boolean> getWarnForMissingVersions();
/**
* @return If a Version Catalog is used: which catalog? (default is 'libs')
*/
public abstract Property<String> getVersionCatalogName();
/**
* Fail the build if a Module Name does not fit the corresponding project and source set names;
* defaults to 'true'.
*/
public abstract Property<Boolean> getModuleNameCheck();
/**
* Set this to true to use the analytic help tasks (like :moduleDependencies) of the plugin without performing
* the actual dependency calculation.
*
* @return true – for analyse-only mode
*/
public abstract Property<Boolean> getAnalyseOnly();
public JavaModuleDependenciesExtension(@Nullable VersionCatalogsExtension versionCatalogs, File rootDir) {
this.versionCatalogs = versionCatalogs;
getModuleInfoCache().convention(getProviders().provider(() -> getObjects().newInstance(ModuleInfoCache.class, false)));
getModulesProperties().set(new File(rootDir, "gradle/modules.properties"));
getVersionCatalogName().convention("libs");
getModuleNameCheck().convention(true);
getAnalyseOnly().convention(false);
getModuleNameToGA().putAll(SharedMappings.mappings);
getModuleNameToGA().putAll(parsedModulesProperties().orElse(Collections.emptyMap()));
}
private Provider<Map<String, String>> parsedModulesProperties() {
return getProviders().fileContents(getModulesProperties()).getAsText().map(c -> {
Properties p = new Properties();
try {
p.load(new CharArrayReader(c.toCharArray()));
} catch (IOException e) {
throw new RuntimeException(e);
}
@SuppressWarnings({"rawtypes", "unchecked"})
Map<String, String> result = (Map) p;
return result;
});
}
/**
* Converts 'Module Name' to GA coordinates that can be used in
* dependency declarations as String: "group:name"
*
* @param moduleName The Module Name
* @return Dependency notation
*/
public Provider<String> ga(String moduleName) {
return getModuleNameToGA().getting(moduleName).orElse(mapByPrefix(getProviders().provider(() -> moduleName))).orElse(errorIfNotFound(moduleName));
}
/**
* Converts 'Module Name' to GA coordinates that can be used in
* dependency declarations as String: "group:name"
*
* @param moduleName The Module Name
* @return Dependency notation
*/
public Provider<String> ga(Provider<String> moduleName) {
return moduleName.flatMap(n -> getModuleNameToGA().getting(n)).orElse(mapByPrefix(moduleName)).orElse(errorIfNotFound(moduleName));
}
private Provider<String> mapByPrefix(Provider<String> moduleName) {
return getModuleNamePrefixToGroup().map(
m -> {
Optional<Map.Entry<String, String>> prefixToGroup = m.entrySet().stream()
.filter(e -> moduleName.get().startsWith(e.getKey())).max(Comparator.comparingInt(e -> e.getKey().length()));
if (prefixToGroup.isPresent()) {
String group = prefixToGroup.get().getValue();
String artifact = toProjectName(moduleName.get().substring(prefixToGroup.get().getKey().length()));
return group + ":" + artifact;
}
return null;
}
);
}
private String toProjectName(String moduleNameSuffix) {
List<String> allProjectNames = getProject().getRootProject().getSubprojects().stream().map(Project::getName).collect(Collectors.toList());
Optional<String> perfectMatch = allProjectNames.stream().filter(p -> p.replace("-", ".").equals(moduleNameSuffix)).findFirst();
Optional<String> existingProjectName = allProjectNames.stream().filter(p -> moduleNameSuffix.startsWith(p.replace("-", ".") + "."))
.max(Comparator.comparingInt(String::length));
if (perfectMatch.isPresent()) {
return perfectMatch.get();
} else if (existingProjectName.isPresent()) {
String capabilityClassifier = moduleNameSuffix.substring(existingProjectName.get().length() + 1).replace(".", "-");
return existingProjectName.get() + "|" + capabilityClassifier; // no exact match (assume last segment is capability)
}
return moduleNameSuffix;
}
public Provider<Dependency> create(String moduleName, SourceSet sourceSetWithModuleInfo) {
if (JDKInfo.MODULES.contains(moduleName)) {
// The module is part of the JDK, no dependency required
return getProviders().provider(() -> null);
}
if (getModuleInfoCache().get().isInitializedInSettings()) {
return createPrecise(moduleName);
} else {
return createWithGuessing(moduleName, sourceSetWithModuleInfo);
}
}
private Provider<Dependency> createPrecise(String moduleName) {
return getProviders().provider(() -> {
LocalModule localModule = getModuleInfoCache().get().getLocalModule(moduleName);
if (localModule != null) {
// local project
ProjectDependency projectDependency = (ProjectDependency) getDependencies().create(getProject().project(localModule.getProjectPath()));
projectDependency.because(moduleName);
if (localModule.getCapability() != null) {
projectDependency.capabilities(c -> c.requireCapabilities(localModule.getCapability()));
}
return projectDependency;
} else {
return createExternalDependency(moduleName);
}
});
}
private Provider<Dependency> createWithGuessing(String moduleName, SourceSet sourceSetWithModuleInfo) {
return getProviders().provider(() -> {
Map<String, String> allProjectNamesAndGroups = getProject().getRootProject().getSubprojects().stream().collect(
Collectors.toMap(Project::getName, p -> (String) p.getGroup(), (a, b) -> a));
ModuleInfo moduleInfo = getModuleInfoCache().get().get(sourceSetWithModuleInfo, getProviders());
String ownModuleNamesPrefix = moduleInfo.moduleNamePrefix(getProject().getName(), sourceSetWithModuleInfo.getName(), getModuleNameCheck().get());
String moduleNameSuffix = ownModuleNamesPrefix == null ? null :
moduleName.startsWith(ownModuleNamesPrefix + ".") ? moduleName.substring(ownModuleNamesPrefix.length() + 1) :
ownModuleNamesPrefix.isEmpty() ? moduleName : null;
String parentPath = getProject().getParent() == null ? "" : getProject().getParent().getPath();
Optional<String> perfectMatch = allProjectNamesAndGroups.keySet().stream().filter(p -> p.replace("-", ".").equals(moduleNameSuffix)).findFirst();
Optional<String> existingProjectName = allProjectNamesAndGroups.keySet().stream().filter(p -> moduleNameSuffix != null && moduleNameSuffix.startsWith(p.replace("-", ".") + "."))
.max(Comparator.comparingInt(String::length));
if (perfectMatch.isPresent()) {
Dependency projectDependency = getDependencies().create(getProject().project(parentPath + ":" + perfectMatch.get()));
projectDependency.because(moduleName);
return projectDependency;
} else if (existingProjectName.isPresent()) {
// no exact match -> add capability to point at Module in other source set
String projectName = existingProjectName.get();
ProjectDependency projectDependency = (ProjectDependency) getDependencies().create(getProject().project(parentPath + ":" + projectName));
String capabilityName = projectName + moduleNameSuffix.substring(projectName.length()).replace(".", "-");
projectDependency.capabilities(c -> c.requireCapabilities(
allProjectNamesAndGroups.get(projectName) + ":" + capabilityName));
projectDependency.because(moduleName);
return projectDependency;
}
return createExternalDependency(moduleName);
});
}
private @Nullable ModuleDependency createExternalDependency(String moduleName) {
Provider<String> coordinates = getModuleNameToGA().getting(moduleName).orElse(mapByPrefix(getProviders().provider(() -> moduleName)));
if (coordinates.isPresent()) {
ExternalDependency component;
String capability;
if (coordinates.get().contains("|")) {
String[] split = coordinates.get().split("\\|");
component = findGav(split[0], moduleName);
if (split[1].contains(":")) {
capability = split[1];
} else {
// only classifier was specified
capability = split[0] + "-" + split[1];
}
} else {
component = findGav(coordinates.get(), moduleName);
capability = null;
}
ModuleDependency dependency = (ModuleDependency) getDependencies().create(component);
dependency.because(moduleName);
if (capability != null) {
dependency.capabilities(c -> c.requireCapability(capability));
}
return dependency;
} else {
getProject().getLogger().lifecycle(
"[WARN] [Java Module Dependencies] " + moduleName + "=group:artifact missing in " + getModulesProperties().get().getAsFile());
return null;
}
}
/**
* Converts 'Module Name' and 'Version' to GAV coordinates that can be used in
* dependency declarations as String: "group:name:version"
*
* @param moduleName The Module Name
* @param version The (required) version
* @return Dependency notation
*/
@SuppressWarnings("unused")
public Provider<String> gav(String moduleName, String version) {
return ga(moduleName).map(s -> s + ":" + version);
}
/**
* Converts 'Module Name' and 'Version' to GAV coordinates that can be used in
* dependency declarations as String: "group:name:version"
*
* @param moduleName The Module Name
* @param version The (required) version
* @return Dependency notation
*/
@SuppressWarnings("unused")
public Provider<String> gav(Provider<String> moduleName, Provider<String> version) {
return ga(moduleName).map(s -> s + ":" + version.get());
}
/**
* If a Version Catalog is used:
* Converts 'Module Name' and the matching 'Version' from the Version Catalog to
* GAV coordinates that can be used in dependency Declarations as Map:
* [group: "...", name: "...", version: "..."]
*
* @param moduleName The Module Name
* @return Dependency notation
*/
@SuppressWarnings("unused")
public Provider<ExternalDependency> gav(String moduleName) {
return ga(moduleName).map(ga -> findGav(ga, moduleName));
}
/**
* If a Version Catalog is used:
* Converts 'Module Name' and the matching 'Version' from the Version Catalog to
* GAV coordinates that can be used in dependency Declarations as Map:
* [group: "...", name: "...", version: "..."]
*
* @param moduleName The Module Name
* @return Dependency notation
*/
@SuppressWarnings("unused")
public Provider<ExternalDependency> gav(Provider<String> moduleName) {
return ga(moduleName).map(ga -> findGav(ga, moduleName.get()));
}
private ExternalDependency findGav(String ga, String moduleName) {
Optional<VersionCatalog> catalog = versionCatalogs == null ? empty() : versionCatalogs.find(getVersionCatalogName().get());
Optional<VersionConstraint> version = catalog.flatMap(versionCatalog -> versionCatalog.findVersion(moduleName.replace('_', '.')));
ExternalDependency dependency = (ExternalDependency) getDependencies().create(ga);
version.ifPresent(versionConstraint -> dependency.version(copy -> copyVersionConstraint(versionConstraint, copy)));
return dependency;
}
/**
* Finds the Module Name for given coordinates
*
* @param ga The GA coordinates
* @return the first name found or unset
*/
public Provider<String> moduleName(String ga) {
return moduleName(getProviders().provider(() -> ga));
}
/**
* Finds the Module Name for given coordinates
*
* @param ga The GA coordinates
* @return the first name found or unset
*/
public Provider<String> moduleName(Provider<String> ga) {
return ga.map(groupArtifact -> {
Optional<String> found = getModuleNameToGA().get().entrySet().stream().filter(
e -> e.getValue().equals(groupArtifact)).map(Map.Entry::getKey).findFirst();
if (found.isPresent()) {
return found.get();
} else {
String[] split = groupArtifact.split(":");
String group = split[0];
String artifact = split[1];
Optional<String> modulePrefix = getModuleNamePrefixToGroup().get().entrySet().stream().filter(
e -> e.getValue().equals(group)).map(Map.Entry::getKey).findFirst();
return modulePrefix.map(s -> s + artifact).orElse(null);
}
});
}
/**
* @return information about all modules defined in module-info.java files in the build
*/
public Set<LocalModule> allLocalModules() {
return new TreeSet<>(getModuleInfoCache().get().getAllLocalModules());
}
/**
* @deprecated use the 'org.gradlex.jvm-dependency-conflict-resolution' plugin instead.
*/
@Deprecated
public Configuration versionsFromConsistentResolution(String... versionsProvidingProjects) {
return versionsFromConsistentResolution(Arrays.asList(versionsProvidingProjects));
}
/**
* Use consistent resolution to manage versions consistently through in the main application project(s).
*
* @param versionsProvidingProjects projects which runtime classpaths are the runtime classpaths of the applications/services being built.
*
* @deprecated use the 'org.gradlex.jvm-dependency-conflict-resolution' plugin instead.
*/
@Deprecated
public Configuration versionsFromConsistentResolution(Collection<String> versionsProvidingProjects) {
ObjectFactory objects = getObjects();
Configuration mainRuntimeClasspath = getConfigurations().create("mainRuntimeClasspath", c -> {
c.setCanBeConsumed(false);
c.getAttributes().attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.class, Usage.JAVA_RUNTIME));
c.getAttributes().attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category.class, Category.LIBRARY));
c.getAttributes().attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, objects.named(LibraryElements.class, LibraryElements.JAR));
c.getAttributes().attribute(TargetJvmEnvironment.TARGET_JVM_ENVIRONMENT_ATTRIBUTE, objects.named(TargetJvmEnvironment.class, TargetJvmEnvironment.STANDARD_JVM));
c.getAttributes().attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling.class, Bundling.EXTERNAL));
});
getConfigurations().configureEach(c -> {
if (c.isCanBeResolved() && !c.isCanBeConsumed() && c != mainRuntimeClasspath) {
//noinspection UnstableApiUsage
c.shouldResolveConsistentlyWith(mainRuntimeClasspath);
}
});
for (String versionsProvidingProject : versionsProvidingProjects) {
getDependencies().add(mainRuntimeClasspath.getName(), createDependency(versionsProvidingProject));
}
return mainRuntimeClasspath;
}
private Dependency createDependency(String project) {
boolean isProjectInBuild = project.startsWith(":");
return getDependencies().create(isProjectInBuild
? getDependencies().project(Collections.singletonMap("path", project))
: project);
}
/**
* Adds support for compiling module-info.java in the given source set with the given task,
* if 'requires runtime' dependencies are used.
*
* @param sourceSetForModuleInfo The source set that contains the module-info.java (e.g. 'main')
* @param sourceSetForClasspath The source set that contains the code that is compiled (e.g. 'test')
*/
@SuppressWarnings("unused")
public void addRequiresRuntimeSupport(SourceSet sourceSetForModuleInfo, SourceSet sourceSetForClasspath) {
doAddRequiresRuntimeSupport(sourceSetForModuleInfo, sourceSetForClasspath);
}
void doAddRequiresRuntimeSupport(SourceSet sourceSetForModuleInfo, SourceSet sourceSetForClasspath) {
List<String> requiresRuntime = getModuleInfoCache().get().get(sourceSetForModuleInfo, getProviders()).get(REQUIRES_RUNTIME);
String generatorTaskName = sourceSetForClasspath.getTaskName("generate", "syntheticModuleInfoFolders");
if (requiresRuntime.isEmpty() || getProject().getTasks().getNames().contains(generatorTaskName)) {
// Already active or not needed for this source set
return;
}
ConfigurableFileCollection syntheticModuleInfoFolders = getObjects().fileCollection();
Provider<Directory> moduleInfoFoldersBase = getLayout().getBuildDirectory().dir("tmp/java-module-dependencies/" + sourceSetForClasspath.getName());
TaskProvider<SyntheticModuleInfoFoldersGenerate> generatorTask = getProject().getTasks().register(
generatorTaskName,
SyntheticModuleInfoFoldersGenerate.class, t -> {
t.getModuleNames().set(requiresRuntime);
t.getSyntheticModuleInfoFolder().set(moduleInfoFoldersBase);
});
List<Provider<Directory>> moduleInfoFolders = requiresRuntime.stream().map(moduleName -> moduleInfoFoldersBase.map(b -> b.dir(moduleName))).collect(Collectors.toList());
for (Provider<Directory> syntheticModuleInfoFolder : moduleInfoFolders) {
syntheticModuleInfoFolders.from(syntheticModuleInfoFolder);
}
syntheticModuleInfoFolders.builtBy(generatorTask);
getDependencies().add(sourceSetForClasspath.getCompileOnlyConfigurationName(), syntheticModuleInfoFolders);
}
private <T> Provider<T> errorIfNotFound(String moduleName) {
return getProviders().provider(() -> {
throw new RuntimeException("Unknown Module: " + moduleName);
});
}
private <T> Provider<T> errorIfNotFound(Provider<String> moduleName) {
return getProviders().provider(() -> {
throw new RuntimeException("Unknown Module: " + moduleName.get());
});
}
@Inject
protected abstract Project getProject();
@Inject
protected abstract ObjectFactory getObjects();
@Inject
protected abstract ProviderFactory getProviders();
@Inject
protected abstract ProjectLayout getLayout();
@Inject
protected abstract DependencyHandler getDependencies();
@Inject
protected abstract ConfigurationContainer getConfigurations();
}