1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.model.path;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.io.File;
26 import java.nio.file.Path;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Objects;
30 import java.util.function.Function;
31
32 import org.apache.maven.api.model.Build;
33 import org.apache.maven.api.model.Model;
34 import org.apache.maven.api.model.Reporting;
35 import org.apache.maven.api.model.Resource;
36 import org.apache.maven.model.building.ModelBuildingRequest;
37
38
39
40
41
42 @Named
43 @Singleton
44 public class DefaultModelPathTranslator implements ModelPathTranslator {
45
46 private final PathTranslator pathTranslator;
47
48 @Inject
49 public DefaultModelPathTranslator(PathTranslator pathTranslator) {
50 this.pathTranslator = pathTranslator;
51 }
52
53 @Deprecated
54 @Override
55 public void alignToBaseDirectory(org.apache.maven.model.Model modelV3, File basedir, ModelBuildingRequest request) {
56 if (modelV3 == null || basedir == null) {
57 return;
58 }
59 alignToBaseDirectory(modelV3, basedir.toPath(), request);
60 }
61
62 @Override
63 public void alignToBaseDirectory(org.apache.maven.model.Model modelV3, Path basedir, ModelBuildingRequest request) {
64 if (modelV3 == null || basedir == null) {
65 return;
66 }
67
68 Model model = modelV3.getDelegate();
69 Build build = model.getBuild();
70 Build newBuild = null;
71 if (build != null) {
72 newBuild = Build.newBuilder(build)
73 .directory(alignToBaseDirectory(build.getDirectory(), basedir))
74 .sourceDirectory(alignToBaseDirectory(build.getSourceDirectory(), basedir))
75 .testSourceDirectory(alignToBaseDirectory(build.getTestSourceDirectory(), basedir))
76 .scriptSourceDirectory(alignToBaseDirectory(build.getScriptSourceDirectory(), basedir))
77 .resources(map(build.getResources(), r -> alignToBaseDirectory(r, basedir)))
78 .testResources(map(build.getTestResources(), r -> alignToBaseDirectory(r, basedir)))
79 .filters(map(build.getFilters(), s -> alignToBaseDirectory(s, basedir)))
80 .outputDirectory(alignToBaseDirectory(build.getOutputDirectory(), basedir))
81 .testOutputDirectory(alignToBaseDirectory(build.getTestOutputDirectory(), basedir))
82 .build();
83 }
84
85 Reporting reporting = model.getReporting();
86 Reporting newReporting = null;
87 if (reporting != null) {
88 newReporting = Reporting.newBuilder(reporting)
89 .outputDirectory(alignToBaseDirectory(reporting.getOutputDirectory(), basedir))
90 .build();
91 }
92 if (newBuild != build || newReporting != reporting) {
93 modelV3.update(Model.newBuilder(model)
94 .build(newBuild)
95 .reporting(newReporting)
96 .build());
97 }
98 }
99
100 private <T> List<T> map(List<T> resources, Function<T, T> mapper) {
101 List<T> newResources = null;
102 if (resources != null) {
103 for (int i = 0; i < resources.size(); i++) {
104 T resource = resources.get(i);
105 T newResource = mapper.apply(resource);
106 if (newResource != null) {
107 if (newResources == null) {
108 newResources = new ArrayList<>(resources);
109 }
110 newResources.set(i, newResource);
111 }
112 }
113 }
114 return newResources;
115 }
116
117 private Resource alignToBaseDirectory(Resource resource, Path basedir) {
118 if (resource != null) {
119 String newDir = alignToBaseDirectory(resource.getDirectory(), basedir);
120 if (newDir != null) {
121 return resource.withDirectory(newDir);
122 }
123 }
124 return resource;
125 }
126
127 private String alignToBaseDirectory(String path, Path basedir) {
128 String newPath = pathTranslator.alignToBaseDirectory(path, basedir);
129 return Objects.equals(path, newPath) ? null : newPath;
130 }
131 }