View Javadoc

1   package org.apache.maven.shared.release.util;
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.io.IOException;
24  import java.io.Reader;
25  import java.util.List;
26  import java.util.Locale;
27  
28  import org.apache.commons.lang.StringUtils;
29  import org.apache.maven.project.MavenProject;
30  import org.apache.maven.shared.release.ReleaseExecutionException;
31  import org.apache.maven.shared.release.config.ReleaseDescriptor;
32  import org.codehaus.plexus.util.FileUtils;
33  import org.codehaus.plexus.util.IOUtil;
34  import org.codehaus.plexus.util.ReaderFactory;
35  
36  /**
37   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
38   * @version $Id: ReleaseUtil.java 1348252 2012-06-08 21:34:01Z rfscholte $
39   */
40  public class ReleaseUtil
41  {
42      public static final String RELEASE_POMv4 = "release-pom.xml";
43  
44      public static final String POMv4 = "pom.xml";
45  
46      private static final String FS = File.separator;
47  
48      /**
49       * The line separator to use.
50       */
51      public static final String LS = System.getProperty( "line.separator" );
52  
53      private ReleaseUtil()
54      {
55          // noop
56      }
57  
58      public static MavenProject getRootProject( List<MavenProject> reactorProjects )
59      {
60          MavenProject project = reactorProjects.get( 0 );
61          for ( MavenProject currentProject : reactorProjects )
62          {
63              if ( currentProject.isExecutionRoot() )
64              {
65                  project = currentProject;
66                  break;
67              }
68          }
69  
70          return project;
71      }
72  
73      public static File getStandardPom( MavenProject project )
74      {
75          if ( project == null )
76          {
77              return null;
78          }
79  
80          File pom = project.getFile();
81  
82          if ( pom == null )
83          {
84              return null;
85          }
86  
87          File releasePom = getReleasePom( project );
88          if ( pom.equals( releasePom ) )
89          {
90              pom = new File( pom.getParent(), POMv4 );
91          }
92  
93          return pom;
94      }
95  
96      public static File getReleasePom( MavenProject project )
97      {
98          if ( project == null )
99          {
100             return null;
101         }
102 
103         File pom = project.getFile();
104 
105         if ( pom == null )
106         {
107             return null;
108         }
109 
110         return new File( pom.getParent(), RELEASE_POMv4 );
111     }
112 
113     /**
114      * Gets the string contents of the specified XML file. Note: In contrast to an XML processor, the line separators in
115      * the returned string will be normalized to use the platform's native line separator. This is basically to save
116      * another normalization step when writing the string contents back to an XML file.
117      *
118      * @param file The path to the XML file to read in, must not be <code>null</code>.
119      * @return The string contents of the XML file.
120      * @throws IOException If the file could not be opened/read.
121      */
122     public static String readXmlFile( File file )
123         throws IOException
124     {
125         return readXmlFile( file, LS );
126     }
127 
128     public static String readXmlFile( File file, String ls )
129         throws IOException
130     {
131         Reader reader = null;
132         try
133         {
134             reader = ReaderFactory.newXmlReader( file );
135             return normalizeLineEndings( IOUtil.toString( reader ), ls );
136         }
137         finally
138         {
139             IOUtil.close( reader );
140         }
141     }
142 
143     /**
144      * Normalizes the line separators in the specified string.
145      *
146      * @param text      The string to normalize, may be <code>null</code>.
147      * @param separator The line separator to use for normalization, typically "\n" or "\r\n", must not be
148      *                  <code>null</code>.
149      * @return The input string with normalized line separators or <code>null</code> if the string was <code>null</code>
150      *         .
151      */
152     public static String normalizeLineEndings( String text, String separator )
153     {
154         String norm = text;
155         if ( text != null )
156         {
157             norm = text.replaceAll( "(\r\n)|(\n)|(\r)", separator );
158         }
159         return norm;
160     }
161 
162     public static ReleaseDescriptor createBasedirAlignedReleaseDescriptor( ReleaseDescriptor releaseDescriptor,
163                                                                            List<MavenProject> reactorProjects )
164         throws ReleaseExecutionException
165     {
166         String basedir;
167         try
168         {
169             basedir = getCommonBasedir( reactorProjects );
170         }
171         catch ( IOException e )
172         {
173             throw new ReleaseExecutionException( "Exception occurred while calculating common basedir: "
174                 + e.getMessage(), e );
175         }
176 
177         int parentLevels =
178             getBaseWorkingDirectoryParentCount( basedir,
179                                                 FileUtils.normalize( releaseDescriptor.getWorkingDirectory() ) );
180 
181         String url = releaseDescriptor.getScmSourceUrl();
182         url = realignScmUrl( parentLevels, url );
183 
184         ReleaseDescriptor descriptor = new ReleaseDescriptor();
185         descriptor.setWorkingDirectory( basedir );
186         descriptor.setScmSourceUrl( url );
187         return descriptor;
188     }
189 
190     public static String getCommonBasedir( List<MavenProject> reactorProjects )
191         throws IOException
192     {
193         return getCommonBasedir( reactorProjects, FS );
194     }
195 
196     public static String getCommonBasedir( List<MavenProject> reactorProjects, String separator )
197         throws IOException
198     {
199         String[] baseDirs = new String[reactorProjects.size()];
200         int idx = 0;
201         for ( MavenProject p : reactorProjects )
202         {
203             String dir = p.getBasedir().getCanonicalPath();
204 
205             // always end with separator so that we know what is a path and what is a partial directory name in the
206             // next call
207             if ( !dir.endsWith( separator ) )
208             {
209                 dir = dir + separator;
210             }
211             baseDirs[idx++] = dir;
212         }
213 
214         String basedir = StringUtils.getCommonPrefix( baseDirs );
215 
216         int separatorPos = basedir.lastIndexOf( separator );
217         if ( !basedir.endsWith( separator ) && separatorPos >= 0 )
218         {
219             basedir = basedir.substring( 0, separatorPos );
220         }
221 
222         if ( basedir.endsWith( separator ) && basedir.length() > 1 )
223         {
224             basedir = basedir.substring( 0, basedir.length() - 1 );
225         }
226 
227         return basedir;
228     }
229 
230     public static int getBaseWorkingDirectoryParentCount( String basedir, String workingDirectory )
231     {
232         int num = 0;
233 
234         // we can safely assume case-insensitivity as we are just backtracking, not comparing. This helps with issues
235         // on Windows with C: vs c:
236         workingDirectory = workingDirectory.toLowerCase( Locale.ENGLISH );
237         basedir = basedir.toLowerCase( Locale.ENGLISH );
238 
239         // MRELEASE-663
240         // For Windows is does matter if basedir ends with a file-separator or not to be able to compare.
241         // Using the parent of a dummy file makes it possible to compare them OS-independent
242         File workingDirectoryFile = new File( workingDirectory, ".tmp" ).getParentFile();
243         File basedirFile = new File( basedir, ".tmp" ).getParentFile();
244 
245         if ( !workingDirectoryFile.equals( basedirFile ) && workingDirectory.startsWith( basedir ) )
246         {
247             do
248             {
249                 workingDirectoryFile = workingDirectoryFile.getParentFile();
250                 num++;
251             }
252             while ( !workingDirectoryFile.equals( basedirFile ) );
253         }
254         return num;
255     }
256 
257     public static String realignScmUrl( int parentLevels, String url )
258     {
259         if ( !StringUtils.isEmpty( url ) )
260         {
261             int index = url.length();
262             String suffix = "";
263             if ( url.endsWith( "/" ) )
264             {
265                 index--;
266                 suffix = "/";
267             }
268             for ( int i = 0; i < parentLevels && index > 0; i++ )
269             {
270                 index = url.lastIndexOf( '/', index - 1 );
271             }
272 
273             if ( index > 0 )
274             {
275                 url = url.substring( 0, index ) + suffix;
276             }
277         }
278         return url;
279     }
280 
281     public static boolean isSymlink( File file )
282         throws IOException
283     {
284         return !file.getAbsolutePath().equals( file.getCanonicalPath() );
285     }
286 }