1 package org.apache.maven.shared.release.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
38
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
50
51 public static final String LS = System.getProperty( "line.separator" );
52
53 private ReleaseUtil()
54 {
55
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
115
116
117
118
119
120
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
145
146
147
148
149
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
206
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
235
236 workingDirectory = workingDirectory.toLowerCase( Locale.ENGLISH );
237 basedir = basedir.toLowerCase( Locale.ENGLISH );
238
239
240
241
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 }