-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
GradleApplicationModelBuilder.java
608 lines (543 loc) · 28.6 KB
/
GradleApplicationModelBuilder.java
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
package io.quarkus.gradle.tooling;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ResolvedArtifact;
import org.gradle.api.artifacts.ResolvedConfiguration;
import org.gradle.api.artifacts.component.ProjectComponentIdentifier;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.FileTree;
import org.gradle.api.initialization.IncludedBuild;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.compile.AbstractCompile;
import org.gradle.language.jvm.tasks.ProcessResources;
import org.gradle.tooling.provider.model.ParameterizedToolingModelBuilder;
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile;
import io.quarkus.bootstrap.BootstrapConstants;
import io.quarkus.bootstrap.model.ApplicationModel;
import io.quarkus.bootstrap.model.ApplicationModelBuilder;
import io.quarkus.bootstrap.model.CapabilityContract;
import io.quarkus.bootstrap.model.PlatformImports;
import io.quarkus.bootstrap.model.gradle.ModelParameter;
import io.quarkus.bootstrap.model.gradle.impl.ModelParameterImpl;
import io.quarkus.bootstrap.workspace.ArtifactSources;
import io.quarkus.bootstrap.workspace.DefaultArtifactSources;
import io.quarkus.bootstrap.workspace.DefaultSourceDir;
import io.quarkus.bootstrap.workspace.DefaultWorkspaceModule;
import io.quarkus.bootstrap.workspace.SourceDir;
import io.quarkus.bootstrap.workspace.WorkspaceModule;
import io.quarkus.fs.util.ZipUtils;
import io.quarkus.gradle.dependency.ApplicationDeploymentClasspathBuilder;
import io.quarkus.maven.dependency.ArtifactCoords;
import io.quarkus.maven.dependency.ArtifactDependency;
import io.quarkus.maven.dependency.ArtifactKey;
import io.quarkus.maven.dependency.DependencyFlags;
import io.quarkus.maven.dependency.GACT;
import io.quarkus.maven.dependency.GACTV;
import io.quarkus.maven.dependency.GAV;
import io.quarkus.maven.dependency.ResolvedDependency;
import io.quarkus.maven.dependency.ResolvedDependencyBuilder;
import io.quarkus.paths.PathCollection;
import io.quarkus.paths.PathList;
import io.quarkus.runtime.LaunchMode;
import io.quarkus.runtime.util.HashUtil;
public class GradleApplicationModelBuilder implements ParameterizedToolingModelBuilder<ModelParameter> {
private static final String MAIN_RESOURCES_OUTPUT = "build/resources/main";
private static final String CLASSES_OUTPUT = "build/classes";
/* @formatter:off */
private static final byte COLLECT_TOP_EXTENSION_RUNTIME_NODES = 0b001;
private static final byte COLLECT_DIRECT_DEPS = 0b010;
private static final byte COLLECT_RELOADABLE_MODULES = 0b100;
/* @formatter:on */
@Override
public boolean canBuild(String modelName) {
return modelName.equals(ApplicationModel.class.getName());
}
@Override
public Class<ModelParameter> getParameterType() {
return ModelParameter.class;
}
@Override
public Object buildAll(String modelName, Project project) {
final ModelParameterImpl modelParameter = new ModelParameterImpl();
modelParameter.setMode(LaunchMode.DEVELOPMENT.toString());
return buildAll(modelName, modelParameter, project);
}
@Override
public Object buildAll(String modelName, ModelParameter parameter, Project project) {
final LaunchMode mode = LaunchMode.valueOf(parameter.getMode());
final ApplicationDeploymentClasspathBuilder classpathBuilder = new ApplicationDeploymentClasspathBuilder(project, mode);
final Configuration classpathConfig = classpathBuilder.getRuntimeConfiguration();
final Configuration deploymentConfig = classpathBuilder.getDeploymentConfiguration();
final PlatformImports platformImports = classpathBuilder.getPlatformImports();
boolean workspaceDiscovery = LaunchMode.DEVELOPMENT.equals(mode) || LaunchMode.TEST.equals(mode)
|| Boolean.parseBoolean(System.getProperty(BootstrapConstants.QUARKUS_BOOTSTRAP_WORKSPACE_DISCOVERY));
if (!workspaceDiscovery) {
Object o = project.getProperties().get(BootstrapConstants.QUARKUS_BOOTSTRAP_WORKSPACE_DISCOVERY);
if (o != null) {
workspaceDiscovery = Boolean.parseBoolean(o.toString());
}
}
final ResolvedDependency appArtifact = getProjectArtifact(project, workspaceDiscovery);
final ApplicationModelBuilder modelBuilder = new ApplicationModelBuilder()
.setAppArtifact(appArtifact)
.addReloadableWorkspaceModule(appArtifact.getKey())
.setPlatformImports(platformImports);
collectDependencies(classpathConfig.getResolvedConfiguration(), workspaceDiscovery,
project, modelBuilder, appArtifact.getWorkspaceModule().mutable());
collectExtensionDependencies(project, deploymentConfig, modelBuilder);
return modelBuilder.build();
}
public static ResolvedDependency getProjectArtifact(Project project, boolean workspaceDiscovery) {
final ResolvedDependencyBuilder appArtifact = ResolvedDependencyBuilder.newInstance()
.setGroupId(project.getGroup().toString())
.setArtifactId(project.getName())
.setVersion(project.getVersion().toString());
SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class);
final WorkspaceModule.Mutable mainModule = WorkspaceModule.builder()
.setModuleId(new GAV(appArtifact.getGroupId(), appArtifact.getArtifactId(), appArtifact.getVersion()))
.setModuleDir(project.getProjectDir().toPath())
.setBuildDir(project.getBuildDir().toPath())
.setBuildFile(project.getBuildFile().toPath());
initProjectModule(project, mainModule, sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME),
SourceSet.MAIN_SOURCE_SET_NAME, ArtifactSources.MAIN);
if (workspaceDiscovery) {
initProjectModule(project, mainModule, sourceSets.getByName(SourceSet.TEST_SOURCE_SET_NAME),
SourceSet.TEST_SOURCE_SET_NAME, ArtifactSources.TEST);
}
final PathList.Builder paths = PathList.builder();
collectDestinationDirs(mainModule.getMainSources().getSourceDirs(), paths);
collectDestinationDirs(mainModule.getMainSources().getResourceDirs(), paths);
return appArtifact.setWorkspaceModule(mainModule).setResolvedPaths(paths.build()).build();
}
private static void collectDestinationDirs(Collection<SourceDir> sources, final PathList.Builder paths) {
for (SourceDir src : sources) {
if (!Files.exists(src.getOutputDir())) {
continue;
}
final Path path = src.getOutputDir();
if (paths.contains(path)) {
continue;
}
paths.add(path);
}
}
private void collectExtensionDependencies(Project project, Configuration deploymentConfiguration,
ApplicationModelBuilder modelBuilder) {
final ResolvedConfiguration rc = deploymentConfiguration.getResolvedConfiguration();
for (ResolvedArtifact a : rc.getResolvedArtifacts()) {
if (a.getId().getComponentIdentifier() instanceof ProjectComponentIdentifier) {
ProjectComponentIdentifier projectComponentIdentifier = (ProjectComponentIdentifier) a.getId()
.getComponentIdentifier();
final Project projectDep = project.getRootProject().findProject(projectComponentIdentifier.getProjectPath());
Objects.requireNonNull(projectDep, "project " + projectComponentIdentifier.getProjectPath() + " should exist");
SourceSetContainer sourceSets = projectDep.getExtensions().getByType(SourceSetContainer.class);
SourceSet mainSourceSet = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME);
ResolvedDependencyBuilder dep = modelBuilder.getDependency(
toAppDependenciesKey(a.getModuleVersion().getId().getGroup(), a.getName(), a.getClassifier()));
if (dep == null) {
dep = toDependency(a, mainSourceSet);
modelBuilder.addDependency(dep);
}
dep.setDeploymentCp();
dep.clearFlag(DependencyFlags.RELOADABLE);
} else if (isDependency(a)) {
ResolvedDependencyBuilder dep = modelBuilder.getDependency(
toAppDependenciesKey(a.getModuleVersion().getId().getGroup(), a.getName(), a.getClassifier()));
if (dep == null) {
dep = toDependency(a);
modelBuilder.addDependency(dep);
}
dep.setDeploymentCp();
dep.clearFlag(DependencyFlags.RELOADABLE);
}
}
}
private void collectDependencies(ResolvedConfiguration configuration,
boolean workspaceDiscovery, Project project, ApplicationModelBuilder modelBuilder,
WorkspaceModule.Mutable wsModule) {
final Set<ResolvedArtifact> resolvedArtifacts = configuration.getResolvedArtifacts();
// if the number of artifacts is less than the number of files then probably
// the project includes direct file dependencies
final Set<File> artifactFiles = resolvedArtifacts.size() < configuration.getFiles().size()
? new HashSet<>(resolvedArtifacts.size())
: null;
configuration.getFirstLevelModuleDependencies()
.forEach(d -> {
collectDependencies(d, workspaceDiscovery, project, artifactFiles, new HashSet<>(),
modelBuilder,
wsModule,
(byte) (COLLECT_TOP_EXTENSION_RUNTIME_NODES | COLLECT_DIRECT_DEPS | COLLECT_RELOADABLE_MODULES));
});
if (artifactFiles != null) {
// detect FS paths that aren't provided by the resolved artifacts
for (File f : configuration.getFiles()) {
if (artifactFiles.contains(f) || !f.exists()) {
continue;
}
// here we are trying to represent a direct FS path dependency
// as an artifact dependency
// SHA1 hash is used to avoid long file names in the lib dir
final String parentPath = f.getParent();
final String group = HashUtil.sha1(parentPath == null ? f.getName() : parentPath);
String name = f.getName();
String type = ArtifactCoords.TYPE_JAR;
if (!f.isDirectory()) {
final int dot = f.getName().lastIndexOf('.');
if (dot > 0) {
name = f.getName().substring(0, dot);
type = f.getName().substring(dot + 1);
}
}
// hash could be a better way to represent the version
final String version = String.valueOf(f.lastModified());
final ResolvedDependencyBuilder artifactBuilder = ResolvedDependencyBuilder.newInstance()
.setGroupId(group)
.setArtifactId(name)
.setType(type)
.setVersion(version)
.setResolvedPath(f.toPath())
.setDirect(true)
.setRuntimeCp();
processQuarkusDependency(artifactBuilder, modelBuilder);
modelBuilder.addDependency(artifactBuilder);
}
}
}
private void collectDependencies(org.gradle.api.artifacts.ResolvedDependency resolvedDep, boolean workspaceDiscovery,
Project project, Set<File> artifactFiles, Set<ArtifactKey> processedModules, ApplicationModelBuilder modelBuilder,
WorkspaceModule.Mutable parentModule,
byte flags) {
WorkspaceModule.Mutable projectModule = null;
for (ResolvedArtifact a : resolvedDep.getModuleArtifacts()) {
final ArtifactKey artifactKey = toAppDependenciesKey(a.getModuleVersion().getId().getGroup(), a.getName(),
a.getClassifier());
if (!isDependency(a) || modelBuilder.getDependency(artifactKey) != null) {
continue;
}
final ArtifactCoords depCoords = toArtifactCoords(a);
final ResolvedDependencyBuilder depBuilder = ResolvedDependencyBuilder.newInstance()
.setCoords(depCoords)
.setRuntimeCp();
if (isFlagOn(flags, COLLECT_DIRECT_DEPS)) {
depBuilder.setDirect(true);
flags = clearFlag(flags, COLLECT_DIRECT_DEPS);
}
if (parentModule != null) {
parentModule.addDependency(new ArtifactDependency(depCoords));
}
PathCollection paths = null;
if (workspaceDiscovery && a.getId().getComponentIdentifier() instanceof ProjectComponentIdentifier) {
final Project projectDep = project.getRootProject().findProject(
((ProjectComponentIdentifier) a.getId().getComponentIdentifier()).getProjectPath());
SourceSetContainer sourceSets = projectDep == null ? null
: projectDep.getExtensions().findByType(SourceSetContainer.class);
final String classifier = a.getClassifier();
if (classifier == null || classifier.isEmpty()) {
final IncludedBuild includedBuild = ToolingUtils.includedBuild(project.getRootProject(), a.getName());
if (includedBuild != null) {
final PathList.Builder pathBuilder = PathList.builder();
addSubstitutedProject(pathBuilder, includedBuild.getProjectDir());
paths = pathBuilder.build();
} else if (sourceSets != null) {
final PathList.Builder pathBuilder = PathList.builder();
projectModule = initProjectModuleAndBuildPaths(projectDep, a, modelBuilder, depBuilder,
pathBuilder, SourceSet.MAIN_SOURCE_SET_NAME, false);
paths = pathBuilder.build();
}
} else if (sourceSets != null) {
if ("test".equals(classifier)) {
final PathList.Builder pathBuilder = PathList.builder();
projectModule = initProjectModuleAndBuildPaths(projectDep, a, modelBuilder, depBuilder,
pathBuilder, SourceSet.TEST_SOURCE_SET_NAME, true);
paths = pathBuilder.build();
} else if ("test-fixtures".equals(classifier)) {
final PathList.Builder pathBuilder = PathList.builder();
projectModule = initProjectModuleAndBuildPaths(projectDep, a, modelBuilder, depBuilder,
pathBuilder, "testFixtures", true);
paths = pathBuilder.build();
}
}
}
depBuilder.setResolvedPaths(paths == null ? PathList.of(a.getFile().toPath()) : paths)
.setWorkspaceModule(projectModule);
if (processQuarkusDependency(depBuilder, modelBuilder)) {
if (isFlagOn(flags, COLLECT_TOP_EXTENSION_RUNTIME_NODES)) {
depBuilder.setFlags(DependencyFlags.TOP_LEVEL_RUNTIME_EXTENSION_ARTIFACT);
flags = clearFlag(flags, COLLECT_TOP_EXTENSION_RUNTIME_NODES);
}
flags = clearFlag(flags, COLLECT_RELOADABLE_MODULES);
}
if (!isFlagOn(flags, COLLECT_RELOADABLE_MODULES)) {
depBuilder.clearFlag(DependencyFlags.RELOADABLE);
}
modelBuilder.addDependency(depBuilder);
if (artifactFiles != null) {
artifactFiles.add(a.getFile());
}
}
processedModules.add(new GACT(resolvedDep.getModuleGroup(), resolvedDep.getModuleName()));
for (org.gradle.api.artifacts.ResolvedDependency child : resolvedDep.getChildren()) {
if (!processedModules.contains(new GACT(child.getModuleGroup(), child.getModuleName()))) {
collectDependencies(child, workspaceDiscovery, project, artifactFiles, processedModules,
modelBuilder, projectModule, flags);
}
}
}
private static String toNonNullClassifier(String resolvedClassifier) {
return resolvedClassifier == null ? "" : resolvedClassifier;
}
private WorkspaceModule.Mutable initProjectModuleAndBuildPaths(final Project project,
ResolvedArtifact resolvedArtifact, ApplicationModelBuilder appModel, final ResolvedDependencyBuilder appDep,
PathList.Builder buildPaths, String sourceName, boolean test) {
appDep.setWorkspaceModule().setReloadable();
final WorkspaceModule.Mutable projectModule = appModel.getOrCreateProjectModule(
new GAV(resolvedArtifact.getModuleVersion().getId().getGroup(), resolvedArtifact.getName(),
resolvedArtifact.getModuleVersion().getId().getVersion()),
project.getProjectDir(),
project.getBuildDir())
.setBuildFile(project.getBuildFile().toPath());
final String classifier = toNonNullClassifier(resolvedArtifact.getClassifier());
SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class);
initProjectModule(project, projectModule, sourceSets.findByName(sourceName), sourceName, classifier);
collectDestinationDirs(projectModule.getSources(classifier).getSourceDirs(), buildPaths);
collectDestinationDirs(projectModule.getSources(classifier).getResourceDirs(), buildPaths);
appModel.addReloadableWorkspaceModule(
new GACT(resolvedArtifact.getModuleVersion().getId().getGroup(), resolvedArtifact.getName(), classifier,
ArtifactCoords.TYPE_JAR));
return projectModule;
}
private boolean processQuarkusDependency(ResolvedDependencyBuilder artifactBuilder, ApplicationModelBuilder modelBuilder) {
for (Path artifactPath : artifactBuilder.getResolvedPaths()) {
if (!Files.exists(artifactPath) || !artifactBuilder.getType().equals(ArtifactCoords.TYPE_JAR)) {
break;
}
if (Files.isDirectory(artifactPath)) {
return processQuarkusDir(artifactBuilder, artifactPath.resolve(BootstrapConstants.META_INF), modelBuilder);
} else {
try (FileSystem artifactFs = ZipUtils.newFileSystem(artifactPath)) {
return processQuarkusDir(artifactBuilder, artifactFs.getPath(BootstrapConstants.META_INF), modelBuilder);
} catch (IOException e) {
throw new RuntimeException("Failed to process " + artifactPath, e);
}
}
}
return false;
}
private static boolean processQuarkusDir(ResolvedDependencyBuilder artifactBuilder, Path quarkusDir,
ApplicationModelBuilder modelBuilder) {
if (!Files.exists(quarkusDir)) {
return false;
}
final Path quarkusDescr = quarkusDir.resolve(BootstrapConstants.DESCRIPTOR_FILE_NAME);
if (!Files.exists(quarkusDescr)) {
return false;
}
final Properties extProps = readDescriptor(quarkusDescr);
if (extProps == null) {
return false;
}
artifactBuilder.setRuntimeExtensionArtifact();
final String extensionCoords = artifactBuilder.toGACTVString();
modelBuilder.handleExtensionProperties(extProps, extensionCoords);
final String providesCapabilities = extProps.getProperty(BootstrapConstants.PROP_PROVIDES_CAPABILITIES);
if (providesCapabilities != null) {
modelBuilder
.addExtensionCapabilities(CapabilityContract.of(extensionCoords, providesCapabilities, null));
}
return true;
}
private static Properties readDescriptor(final Path path) {
final Properties rtProps;
if (!Files.exists(path)) {
// not a platform artifact
return null;
}
rtProps = new Properties();
try (BufferedReader reader = Files.newBufferedReader(path)) {
rtProps.load(reader);
} catch (IOException e) {
throw new UncheckedIOException("Failed to load extension description " + path, e);
}
return rtProps;
}
private static void initProjectModule(Project project, WorkspaceModule.Mutable module, SourceSet sourceSet,
String sourceName, String classifier) {
if (sourceSet == null) {
return;
}
final FileCollection allClassesDirs = sourceSet.getOutput().getClassesDirs();
// some plugins do not add source directories to source sets and they may be missing from sourceSet.getAllJava()
// see https://github.com/quarkusio/quarkus/issues/20755
final List<SourceDir> sourceDirs = new ArrayList<>(1);
final List<SourceDir> resourceDirs = new ArrayList<>(1);
project.getTasks().withType(AbstractCompile.class, t -> {
if (!t.getEnabled()) {
return;
}
final FileTree source = t.getSource();
if (source.isEmpty()) {
return;
}
final File destDir = t.getDestinationDirectory().getAsFile().get();
if (!allClassesDirs.contains(destDir)) {
return;
}
source.visit(a -> {
// we are looking for the root dirs containing sources
if (a.getRelativePath().getSegments().length == 1) {
final File srcDir = a.getFile().getParentFile();
DefaultSourceDir sources = new DefaultSourceDir(srcDir.toPath(), destDir.toPath(),
Collections.singletonMap("compiler", t.getName()));
sourceDirs.add(sources);
}
});
});
// This "try/catch" is needed because of the way the "quarkus-cli" Gradle tests work. Without it, the tests fail.
try {
Class.forName("org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile");
project.getTasks().withType(KotlinJvmCompile.class, t -> {
if (!t.getEnabled()) {
return;
}
final FileTree source = t.getSources().getAsFileTree();
if (source.isEmpty()) {
return;
}
final File destDir = t.getDestinationDirectory().getAsFile().get();
if (!allClassesDirs.contains(destDir)) {
return;
}
source.visit(a -> {
// we are looking for the root dirs containing sources
if (a.getRelativePath().getSegments().length == 1) {
final File srcDir = a.getFile().getParentFile();
DefaultSourceDir sources = new DefaultSourceDir(srcDir.toPath(), destDir.toPath(),
Collections.singletonMap("compiler", t.getName()));
sourceDirs.add(sources);
}
});
});
} catch (ClassNotFoundException e) {
// ignore
}
final File resourcesOutputDir = sourceSet.getOutput().getResourcesDir();
project.getTasks().withType(ProcessResources.class, t -> {
if (!t.getEnabled()) {
return;
}
final FileCollection source = t.getSource();
if (source.isEmpty()) {
return;
}
if (!t.getDestinationDir().equals(resourcesOutputDir)) {
return;
}
final Path destDir = t.getDestinationDir().toPath();
source.getAsFileTree().visit(a -> {
// we are looking for the root dirs containing sources
if (a.getRelativePath().getSegments().length == 1) {
final File srcDir = a.getFile().getParentFile();
resourceDirs.add(new DefaultSourceDir(srcDir.toPath(), destDir));
}
});
});
// there could be a task generating resources
if (resourcesOutputDir.exists() && resourceDirs.isEmpty()) {
sourceSet.getResources().getSrcDirs()
.forEach(srcDir -> resourceDirs.add(new DefaultSourceDir(srcDir.toPath(), resourcesOutputDir.toPath())));
}
module.addArtifactSources(new DefaultArtifactSources(classifier, sourceDirs, resourceDirs));
}
private void addSubstitutedProject(PathList.Builder paths, File projectFile) {
File mainResourceDirectory = new File(projectFile, MAIN_RESOURCES_OUTPUT);
if (mainResourceDirectory.exists()) {
paths.add(mainResourceDirectory.toPath());
}
File classesOutput = new File(projectFile, CLASSES_OUTPUT);
File[] languageDirectories = classesOutput.listFiles();
if (languageDirectories == null) {
throw new GradleException(
"The project does not contain a class output directory. " + classesOutput.getPath() + " must exist.");
}
for (File languageDirectory : languageDirectories) {
if (languageDirectory.isDirectory()) {
for (File sourceSet : languageDirectory.listFiles()) {
if (sourceSet.isDirectory() && sourceSet.getName().equals(SourceSet.MAIN_SOURCE_SET_NAME)) {
paths.add(sourceSet.toPath());
}
}
}
}
}
private static boolean isFlagOn(byte walkingFlags, byte flag) {
return (walkingFlags & flag) > 0;
}
private static byte clearFlag(byte flags, byte flag) {
if ((flags & flag) > 0) {
flags ^= flag;
}
return flags;
}
private static boolean isDependency(ResolvedArtifact a) {
return ArtifactCoords.TYPE_JAR.equalsIgnoreCase(a.getExtension()) || "exe".equalsIgnoreCase(a.getExtension()) ||
a.getFile().isDirectory();
}
/**
* Creates an instance of Dependency and associates it with the ResolvedArtifact's path
*/
static ResolvedDependencyBuilder toDependency(ResolvedArtifact a, int... flags) {
return toDependency(a, PathList.of(a.getFile().toPath()), null, flags);
}
static ResolvedDependencyBuilder toDependency(ResolvedArtifact a, SourceSet s) {
PathList.Builder resolvedPathBuilder = PathList.builder();
for (File classesDir : s.getOutput().getClassesDirs()) {
if (classesDir.exists()) {
resolvedPathBuilder.add(classesDir.toPath());
}
}
File resourceDir = s.getOutput().getResourcesDir();
if (resourceDir != null && resourceDir.exists()) {
resolvedPathBuilder.add(resourceDir.toPath());
}
return ResolvedDependencyBuilder
.newInstance()
.setResolvedPaths(resolvedPathBuilder.build())
.setCoords(toArtifactCoords(a));
}
static ResolvedDependencyBuilder toDependency(ResolvedArtifact a, PathCollection paths, DefaultWorkspaceModule module,
int... flags) {
int allFlags = 0;
for (int f : flags) {
allFlags |= f;
}
return ResolvedDependencyBuilder.newInstance()
.setCoords(toArtifactCoords(a))
.setResolvedPaths(paths)
.setWorkspaceModule(module)
.setFlags(allFlags);
}
private static ArtifactCoords toArtifactCoords(ResolvedArtifact a) {
final String[] split = a.getModuleVersion().toString().split(":");
return new GACTV(split[0], split[1], a.getClassifier(), a.getType(), split.length > 2 ? split[2] : null);
}
private static ArtifactKey toAppDependenciesKey(String groupId, String artifactId, String classifier) {
return new GACT(groupId, artifactId, classifier, ArtifactCoords.TYPE_JAR);
}
}