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