1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.project.path;
20
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.List;
24 import org.apache.maven.model.Build;
25 import org.apache.maven.model.Model;
26 import org.apache.maven.model.Reporting;
27 import org.apache.maven.model.Resource;
28 import org.codehaus.plexus.component.annotations.Component;
29
30
31
32
33 @Deprecated
34 @Component(role = PathTranslator.class)
35 public class DefaultPathTranslator implements PathTranslator {
36 private static final String[] BASEDIR_EXPRESSIONS = {"${basedir}", "${pom.basedir}", "${project.basedir}"};
37
38 public void alignToBaseDirectory(Model model, File basedir) {
39 if (basedir == null) {
40 return;
41 }
42
43 Build build = model.getBuild();
44
45 if (build != null) {
46 build.setDirectory(alignToBaseDirectory(build.getDirectory(), basedir));
47
48 build.setSourceDirectory(alignToBaseDirectory(build.getSourceDirectory(), basedir));
49
50 build.setTestSourceDirectory(alignToBaseDirectory(build.getTestSourceDirectory(), basedir));
51
52 for (Resource resource : build.getResources()) {
53 resource.setDirectory(alignToBaseDirectory(resource.getDirectory(), basedir));
54 }
55
56 for (Resource resource : build.getTestResources()) {
57 resource.setDirectory(alignToBaseDirectory(resource.getDirectory(), basedir));
58 }
59
60 if (build.getFilters() != null) {
61 List<String> filters = new ArrayList<>();
62 for (String filter : build.getFilters()) {
63 filters.add(alignToBaseDirectory(filter, basedir));
64 }
65 build.setFilters(filters);
66 }
67
68 build.setOutputDirectory(alignToBaseDirectory(build.getOutputDirectory(), basedir));
69
70 build.setTestOutputDirectory(alignToBaseDirectory(build.getTestOutputDirectory(), basedir));
71 }
72
73 Reporting reporting = model.getReporting();
74
75 if (reporting != null) {
76 reporting.setOutputDirectory(alignToBaseDirectory(reporting.getOutputDirectory(), basedir));
77 }
78 }
79
80 public String alignToBaseDirectory(String path, File basedir) {
81 if (basedir == null) {
82 return path;
83 }
84
85 if (path == null) {
86 return null;
87 }
88
89 String s = stripBasedirToken(path);
90
91 File file = new File(s);
92 if (file.isAbsolute()) {
93
94 s = file.getPath();
95 } else if (file.getPath().startsWith(File.separator)) {
96
97 s = file.getAbsolutePath();
98 } else {
99
100 s = new File(new File(basedir, s).toURI().normalize()).getAbsolutePath();
101 }
102
103 return s;
104 }
105
106 private String stripBasedirToken(String s) {
107 if (s != null) {
108 String basedirExpr = null;
109 for (String expression : BASEDIR_EXPRESSIONS) {
110 if (s.startsWith(expression)) {
111 basedirExpr = expression;
112 break;
113 }
114 }
115
116 if (basedirExpr != null) {
117 if (s.length() > basedirExpr.length()) {
118
119 s = chopLeadingFileSeparator(s.substring(basedirExpr.length()));
120 } else {
121 s = ".";
122 }
123 }
124 }
125
126 return s;
127 }
128
129
130
131
132
133
134
135
136 private String chopLeadingFileSeparator(String path) {
137 if (path != null) {
138 if (path.startsWith("/") || path.startsWith("\\")) {
139 path = path.substring(1);
140 }
141 }
142 return path;
143 }
144
145 public void unalignFromBaseDirectory(Model model, File basedir) {
146 if (basedir == null) {
147 return;
148 }
149
150 Build build = model.getBuild();
151
152 if (build != null) {
153 build.setDirectory(unalignFromBaseDirectory(build.getDirectory(), basedir));
154
155 build.setSourceDirectory(unalignFromBaseDirectory(build.getSourceDirectory(), basedir));
156
157 build.setTestSourceDirectory(unalignFromBaseDirectory(build.getTestSourceDirectory(), basedir));
158
159 for (Resource resource : build.getResources()) {
160 resource.setDirectory(unalignFromBaseDirectory(resource.getDirectory(), basedir));
161 }
162
163 for (Resource resource : build.getTestResources()) {
164 resource.setDirectory(unalignFromBaseDirectory(resource.getDirectory(), basedir));
165 }
166
167 if (build.getFilters() != null) {
168 List<String> filters = new ArrayList<>();
169 for (String filter : build.getFilters()) {
170 filters.add(unalignFromBaseDirectory(filter, basedir));
171 }
172 build.setFilters(filters);
173 }
174
175 build.setOutputDirectory(unalignFromBaseDirectory(build.getOutputDirectory(), basedir));
176
177 build.setTestOutputDirectory(unalignFromBaseDirectory(build.getTestOutputDirectory(), basedir));
178 }
179
180 Reporting reporting = model.getReporting();
181
182 if (reporting != null) {
183 reporting.setOutputDirectory(unalignFromBaseDirectory(reporting.getOutputDirectory(), basedir));
184 }
185 }
186
187 public String unalignFromBaseDirectory(String path, File basedir) {
188 if (basedir == null) {
189 return path;
190 }
191
192 if (path == null) {
193 return null;
194 }
195
196 path = path.trim();
197
198 String base = basedir.getAbsolutePath();
199 if (path.startsWith(base)) {
200 path = chopLeadingFileSeparator(path.substring(base.length()));
201 }
202
203 if (path.length() <= 0) {
204 path = ".";
205 }
206
207 if (!new File(path).isAbsolute()) {
208 path = path.replace('\\', '/');
209 }
210
211 return path;
212 }
213 }