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
43 @Named
44 @Singleton
45 @Deprecated(since = "4.0.0")
46 public class DefaultModelPathTranslator implements ModelPathTranslator {
47
48 private final PathTranslator pathTranslator;
49
50 @Inject
51 public DefaultModelPathTranslator(PathTranslator pathTranslator) {
52 this.pathTranslator = pathTranslator;
53 }
54
55 @Deprecated
56 @Override
57 public void alignToBaseDirectory(org.apache.maven.model.Model modelV3, File basedir, ModelBuildingRequest request) {
58 if (modelV3 == null || basedir == null) {
59 return;
60 }
61 alignToBaseDirectory(modelV3, basedir.toPath(), request);
62 }
63
64 @Override
65 public void alignToBaseDirectory(org.apache.maven.model.Model modelV3, Path basedir, ModelBuildingRequest request) {
66 if (modelV3 == null || basedir == null) {
67 return;
68 }
69
70 Model model = modelV3.getDelegate();
71 Build build = model.getBuild();
72 Build newBuild = null;
73 if (build != null) {
74 newBuild = Build.newBuilder(build)
75 .directory(alignToBaseDirectory(build.getDirectory(), basedir))
76 .sourceDirectory(alignToBaseDirectory(build.getSourceDirectory(), basedir))
77 .testSourceDirectory(alignToBaseDirectory(build.getTestSourceDirectory(), basedir))
78 .scriptSourceDirectory(alignToBaseDirectory(build.getScriptSourceDirectory(), basedir))
79 .resources(map(build.getResources(), r -> alignToBaseDirectory(r, basedir)))
80 .testResources(map(build.getTestResources(), r -> alignToBaseDirectory(r, basedir)))
81 .filters(map(build.getFilters(), s -> alignToBaseDirectory(s, basedir)))
82 .outputDirectory(alignToBaseDirectory(build.getOutputDirectory(), basedir))
83 .testOutputDirectory(alignToBaseDirectory(build.getTestOutputDirectory(), basedir))
84 .build();
85 }
86
87 Reporting reporting = model.getReporting();
88 Reporting newReporting = null;
89 if (reporting != null) {
90 newReporting = Reporting.newBuilder(reporting)
91 .outputDirectory(alignToBaseDirectory(reporting.getOutputDirectory(), basedir))
92 .build();
93 }
94 if (newBuild != build || newReporting != reporting) {
95 modelV3.update(Model.newBuilder(model)
96 .build(newBuild)
97 .reporting(newReporting)
98 .build());
99 }
100 }
101
102 private <T> List<T> map(List<T> resources, Function<T, T> mapper) {
103 List<T> newResources = null;
104 if (resources != null) {
105 for (int i = 0; i < resources.size(); i++) {
106 T resource = resources.get(i);
107 T newResource = mapper.apply(resource);
108 if (newResource != null) {
109 if (newResources == null) {
110 newResources = new ArrayList<>(resources);
111 }
112 newResources.set(i, newResource);
113 }
114 }
115 }
116 return newResources;
117 }
118
119 private Resource alignToBaseDirectory(Resource resource, Path basedir) {
120 if (resource != null) {
121 String newDir = mayAlignToBaseDirectoryOrNull(resource.getDirectory(), basedir);
122 if (newDir != null) {
123 return resource.withDirectory(newDir);
124 }
125 }
126 return resource;
127 }
128
129 private String alignToBaseDirectory(String path, Path basedir) {
130 String newPath = mayAlignToBaseDirectoryOrNull(path, basedir);
131 if (newPath != null) {
132 return newPath;
133 }
134 return path;
135 }
136
137
138
139
140 private String mayAlignToBaseDirectoryOrNull(String path, Path basedir) {
141 String newPath = pathTranslator.alignToBaseDirectory(path, basedir);
142 return Objects.equals(path, newPath) ? null : newPath;
143 }
144 }