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.util.ArrayList;
27 import java.util.List;
28 import java.util.Objects;
29 import java.util.function.Function;
30
31 import org.apache.maven.api.model.Build;
32 import org.apache.maven.api.model.Model;
33 import org.apache.maven.api.model.Reporting;
34 import org.apache.maven.api.model.Resource;
35 import org.apache.maven.model.building.ModelBuildingRequest;
36
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 @Override
54 public void alignToBaseDirectory(org.apache.maven.model.Model modelV3, File basedir, ModelBuildingRequest request) {
55 if (modelV3 == null || basedir == null) {
56 return;
57 }
58
59 Model model = modelV3.getDelegate();
60 Build build = model.getBuild();
61 Build newBuild = null;
62 if (build != null) {
63 newBuild = Build.newBuilder(build)
64 .directory(alignToBaseDirectory(build.getDirectory(), basedir))
65 .sourceDirectory(alignToBaseDirectory(build.getSourceDirectory(), basedir))
66 .testSourceDirectory(alignToBaseDirectory(build.getTestSourceDirectory(), basedir))
67 .scriptSourceDirectory(alignToBaseDirectory(build.getScriptSourceDirectory(), basedir))
68 .resources(map(build.getResources(), r -> alignToBaseDirectory(r, basedir)))
69 .testResources(map(build.getTestResources(), r -> alignToBaseDirectory(r, basedir)))
70 .filters(map(build.getFilters(), s -> alignToBaseDirectory(s, basedir)))
71 .outputDirectory(alignToBaseDirectory(build.getOutputDirectory(), basedir))
72 .testOutputDirectory(alignToBaseDirectory(build.getTestOutputDirectory(), basedir))
73 .build();
74 }
75
76 Reporting reporting = model.getReporting();
77 Reporting newReporting = null;
78 if (reporting != null) {
79 newReporting = Reporting.newBuilder(reporting)
80 .outputDirectory(alignToBaseDirectory(reporting.getOutputDirectory(), basedir))
81 .build();
82 }
83 if (newBuild != build || newReporting != reporting) {
84 modelV3.update(Model.newBuilder(model)
85 .build(newBuild)
86 .reporting(newReporting)
87 .build());
88 }
89 }
90
91 private <T> List<T> map(List<T> resources, Function<T, T> mapper) {
92 List<T> newResources = null;
93 if (resources != null) {
94 for (int i = 0; i < resources.size(); i++) {
95 T resource = resources.get(i);
96 T newResource = mapper.apply(resource);
97 if (newResource != null) {
98 if (newResources == null) {
99 newResources = new ArrayList<>(resources);
100 }
101 newResources.set(i, newResource);
102 }
103 }
104 }
105 return newResources;
106 }
107
108 private Resource alignToBaseDirectory(Resource resource, File basedir) {
109 if (resource != null) {
110 String newDir = alignToBaseDirectory(resource.getDirectory(), basedir);
111 if (newDir != null) {
112 return resource.withDirectory(newDir);
113 }
114 }
115 return resource;
116 }
117
118 private String alignToBaseDirectory(String path, File basedir) {
119 String newPath = pathTranslator.alignToBaseDirectory(path, basedir);
120 return Objects.equals(path, newPath) ? null : newPath;
121 }
122 }