View Javadoc
1   package org.apache.maven.project.path;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.maven.model.Build;
27  import org.apache.maven.model.Model;
28  import org.apache.maven.model.Reporting;
29  import org.apache.maven.model.Resource;
30  import org.codehaus.plexus.component.annotations.Component;
31  
32  @Deprecated
33  @Component( role = PathTranslator.class )
34  public class DefaultPathTranslator
35      implements PathTranslator
36  {
37      private static final String[] BASEDIR_EXPRESSIONS = {"${basedir}", "${pom.basedir}", "${project.basedir}"};
38  
39      public void alignToBaseDirectory( Model model, File basedir )
40      {
41          if ( basedir == null )
42          {
43              return;
44          }
45  
46          Build build = model.getBuild();
47  
48          if ( build != null )
49          {
50              build.setDirectory( alignToBaseDirectory( build.getDirectory(), basedir ) );
51  
52              build.setSourceDirectory( alignToBaseDirectory( build.getSourceDirectory(), basedir ) );
53  
54              build.setTestSourceDirectory( alignToBaseDirectory( build.getTestSourceDirectory(), basedir ) );
55  
56              for ( Resource resource : build.getResources() )
57              {
58                  resource.setDirectory( alignToBaseDirectory( resource.getDirectory(), basedir ) );
59              }
60  
61              for ( Resource resource : build.getTestResources() )
62              {
63                  resource.setDirectory( alignToBaseDirectory( resource.getDirectory(), basedir ) );
64              }
65  
66              if ( build.getFilters() != null )
67              {
68                  List<String> filters = new ArrayList<>();
69                  for ( String filter : build.getFilters() )
70                  {
71                      filters.add( alignToBaseDirectory( filter, basedir ) );
72                  }
73                  build.setFilters( filters );
74              }
75  
76              build.setOutputDirectory( alignToBaseDirectory( build.getOutputDirectory(), basedir ) );
77  
78              build.setTestOutputDirectory( alignToBaseDirectory( build.getTestOutputDirectory(), basedir ) );
79          }
80  
81          Reporting reporting = model.getReporting();
82  
83          if ( reporting != null )
84          {
85              reporting.setOutputDirectory( alignToBaseDirectory( reporting.getOutputDirectory(), basedir ) );
86          }
87      }
88  
89      public String alignToBaseDirectory( String path, File basedir )
90      {
91          if ( basedir == null )
92          {
93              return path;
94          }
95  
96          if ( path == null )
97          {
98              return null;
99          }
100 
101         String s = stripBasedirToken( path );
102 
103         File file = new File( s );
104         if ( file.isAbsolute() )
105         {
106             // path was already absolute, just normalize file separator and we're done
107             s = file.getPath();
108         }
109         else if ( file.getPath().startsWith( File.separator ) )
110         {
111             // drive-relative Windows path, don't align with project directory but with drive root
112             s = file.getAbsolutePath();
113         }
114         else
115         {
116             // an ordinary relative path, align with project directory
117             s = new File( new File( basedir, s ).toURI().normalize() ).getAbsolutePath();
118         }
119 
120         return s;
121     }
122 
123     private String stripBasedirToken( String s )
124     {
125         if ( s != null )
126         {
127             String basedirExpr = null;
128             for ( String expression : BASEDIR_EXPRESSIONS )
129             {
130                 if ( s.startsWith( expression ) )
131                 {
132                     basedirExpr = expression;
133                     break;
134                 }
135             }
136 
137             if ( basedirExpr != null )
138             {
139                 if ( s.length() > basedirExpr.length() )
140                 {
141                     // Take out basedir expression and the leading slash
142                     s = chopLeadingFileSeparator( s.substring( basedirExpr.length() ) );
143                 }
144                 else
145                 {
146                     s = ".";
147                 }
148             }
149         }
150 
151         return s;
152     }
153 
154     /**
155      * Removes the leading directory separator from the specified filesystem path (if any). For platform-independent
156      * behavior, this method accepts both the forward slash and the backward slash as separator.
157      *
158      * @param path The filesystem path, may be <code>null</code>.
159      * @return The altered filesystem path or <code>null</code> if the input path was <code>null</code>.
160      */
161     private String chopLeadingFileSeparator( String path )
162     {
163         if ( path != null )
164         {
165             if ( path.startsWith( "/" ) || path.startsWith( "\\" ) )
166             {
167                 path = path.substring( 1 );
168             }
169         }
170         return path;
171     }
172 
173     public void unalignFromBaseDirectory( Model model, File basedir )
174     {
175         if ( basedir == null )
176         {
177             return;
178         }
179 
180         Build build = model.getBuild();
181 
182         if ( build != null )
183         {
184             build.setDirectory( unalignFromBaseDirectory( build.getDirectory(), basedir ) );
185 
186             build.setSourceDirectory( unalignFromBaseDirectory( build.getSourceDirectory(), basedir ) );
187 
188             build.setTestSourceDirectory( unalignFromBaseDirectory( build.getTestSourceDirectory(), basedir ) );
189 
190             for ( Resource resource : build.getResources() )
191             {
192                 resource.setDirectory( unalignFromBaseDirectory( resource.getDirectory(), basedir ) );
193             }
194 
195             for ( Resource resource : build.getTestResources() )
196             {
197                 resource.setDirectory( unalignFromBaseDirectory( resource.getDirectory(), basedir ) );
198             }
199 
200             if ( build.getFilters() != null )
201             {
202                 List<String> filters = new ArrayList<>();
203                 for ( String filter : build.getFilters() )
204                 {
205                     filters.add( unalignFromBaseDirectory( filter, basedir ) );
206                 }
207                 build.setFilters( filters );
208             }
209 
210             build.setOutputDirectory( unalignFromBaseDirectory( build.getOutputDirectory(), basedir ) );
211 
212             build.setTestOutputDirectory( unalignFromBaseDirectory( build.getTestOutputDirectory(), basedir ) );
213         }
214 
215         Reporting reporting = model.getReporting();
216 
217         if ( reporting != null )
218         {
219             reporting.setOutputDirectory( unalignFromBaseDirectory( reporting.getOutputDirectory(), basedir ) );
220         }
221     }
222 
223     public String unalignFromBaseDirectory( String path, File basedir )
224     {
225         if ( basedir == null )
226         {
227             return path;
228         }
229 
230         if ( path == null )
231         {
232             return null;
233         }
234 
235         path = path.trim();
236 
237         String base = basedir.getAbsolutePath();
238         if ( path.startsWith( base ) )
239         {
240             path = chopLeadingFileSeparator( path.substring( base.length() ) );
241         }
242 
243         if ( path.length() <= 0 )
244         {
245             path = ".";
246         }
247 
248         if ( !new File( path ).isAbsolute() )
249         {
250             path = path.replace( '\\', '/' );
251         }
252 
253         return path;
254     }
255 
256 }
257