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 | 2 | private static final String FS = File.separator; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | 2 | public static final String LS = System.getProperty( "line.separator" ); |
52 | |
|
53 | |
private ReleaseUtil() |
54 | 0 | { |
55 | |
|
56 | 0 | } |
57 | |
|
58 | |
public static MavenProject getRootProject( List<MavenProject> reactorProjects ) |
59 | |
{ |
60 | 598 | MavenProject project = reactorProjects.get( 0 ); |
61 | 598 | for ( MavenProject currentProject : reactorProjects ) |
62 | |
{ |
63 | 1340 | if ( currentProject.isExecutionRoot() ) |
64 | |
{ |
65 | 0 | project = currentProject; |
66 | 0 | break; |
67 | |
} |
68 | |
} |
69 | |
|
70 | 598 | return project; |
71 | |
} |
72 | |
|
73 | |
public static File getStandardPom( MavenProject project ) |
74 | |
{ |
75 | 1182 | if ( project == null ) |
76 | |
{ |
77 | 2 | return null; |
78 | |
} |
79 | |
|
80 | 1180 | File pom = project.getFile(); |
81 | |
|
82 | 1180 | if ( pom == null ) |
83 | |
{ |
84 | 0 | return null; |
85 | |
} |
86 | |
|
87 | 1180 | File releasePom = getReleasePom( project ); |
88 | 1180 | if ( pom.equals( releasePom ) ) |
89 | |
{ |
90 | 0 | pom = new File( pom.getParent(), POMv4 ); |
91 | |
} |
92 | |
|
93 | 1180 | return pom; |
94 | |
} |
95 | |
|
96 | |
public static File getReleasePom( MavenProject project ) |
97 | |
{ |
98 | 1692 | if ( project == null ) |
99 | |
{ |
100 | 2 | return null; |
101 | |
} |
102 | |
|
103 | 1690 | File pom = project.getFile(); |
104 | |
|
105 | 1690 | if ( pom == null ) |
106 | |
{ |
107 | 0 | return null; |
108 | |
} |
109 | |
|
110 | 1690 | 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 | 80 | return readXmlFile( file, LS ); |
126 | |
} |
127 | |
|
128 | |
public static String readXmlFile( File file, String ls ) |
129 | |
throws IOException |
130 | |
{ |
131 | 632 | Reader reader = null; |
132 | |
try |
133 | |
{ |
134 | 632 | reader = ReaderFactory.newXmlReader( file ); |
135 | 632 | return normalizeLineEndings( IOUtil.toString( reader ), ls ); |
136 | |
} |
137 | |
finally |
138 | |
{ |
139 | 632 | 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 | 1532 | String norm = text; |
155 | 1532 | if ( text != null ) |
156 | |
{ |
157 | 1532 | norm = text.replaceAll( "(\r\n)|(\n)|(\r)", separator ); |
158 | |
} |
159 | 1532 | 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 | 18 | basedir = getCommonBasedir( reactorProjects ); |
170 | |
} |
171 | 0 | catch ( IOException e ) |
172 | |
{ |
173 | 0 | throw new ReleaseExecutionException( "Exception occurred while calculating common basedir: " |
174 | |
+ e.getMessage(), e ); |
175 | 18 | } |
176 | |
|
177 | 18 | int parentLevels = |
178 | |
getBaseWorkingDirectoryParentCount( basedir, |
179 | |
FileUtils.normalize( releaseDescriptor.getWorkingDirectory() ) ); |
180 | |
|
181 | 18 | String url = releaseDescriptor.getScmSourceUrl(); |
182 | 18 | url = realignScmUrl( parentLevels, url ); |
183 | |
|
184 | 18 | ReleaseDescriptor descriptor = new ReleaseDescriptor(); |
185 | 18 | descriptor.setWorkingDirectory( basedir ); |
186 | 18 | descriptor.setScmSourceUrl( url ); |
187 | 18 | return descriptor; |
188 | |
} |
189 | |
|
190 | |
public static String getCommonBasedir( List<MavenProject> reactorProjects ) |
191 | |
throws IOException |
192 | |
{ |
193 | 542 | return getCommonBasedir( reactorProjects, FS ); |
194 | |
} |
195 | |
|
196 | |
public static String getCommonBasedir( List<MavenProject> reactorProjects, String separator ) |
197 | |
throws IOException |
198 | |
{ |
199 | 566 | String[] baseDirs = new String[reactorProjects.size()]; |
200 | 566 | int idx = 0; |
201 | 566 | for ( MavenProject p : reactorProjects ) |
202 | |
{ |
203 | 1772 | String dir = p.getBasedir().getCanonicalPath(); |
204 | |
|
205 | |
|
206 | |
|
207 | 1772 | if ( !dir.endsWith( separator ) ) |
208 | |
{ |
209 | 1772 | dir = dir + separator; |
210 | |
} |
211 | 1772 | baseDirs[idx++] = dir; |
212 | 1772 | } |
213 | |
|
214 | 566 | String basedir = StringUtils.getCommonPrefix( baseDirs ); |
215 | |
|
216 | 566 | int separatorPos = basedir.lastIndexOf( separator ); |
217 | 566 | if ( !basedir.endsWith( separator ) && separatorPos >= 0 ) |
218 | |
{ |
219 | 4 | basedir = basedir.substring( 0, separatorPos ); |
220 | |
} |
221 | |
|
222 | 566 | if ( basedir.endsWith( separator ) && basedir.length() > 1 ) |
223 | |
{ |
224 | 562 | basedir = basedir.substring( 0, basedir.length() - 1 ); |
225 | |
} |
226 | |
|
227 | 566 | return basedir; |
228 | |
} |
229 | |
|
230 | |
public static int getBaseWorkingDirectoryParentCount( String basedir, String workingDirectory ) |
231 | |
{ |
232 | 96 | int num = 0; |
233 | |
|
234 | |
|
235 | |
|
236 | 96 | workingDirectory = workingDirectory.toLowerCase( Locale.ENGLISH ); |
237 | 96 | basedir = basedir.toLowerCase( Locale.ENGLISH ); |
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | 96 | File workingDirectoryFile = new File( workingDirectory, ".tmp" ).getParentFile(); |
243 | 96 | File basedirFile = new File( basedir, ".tmp" ).getParentFile(); |
244 | |
|
245 | 96 | if ( !workingDirectoryFile.equals( basedirFile ) && workingDirectory.startsWith( basedir ) ) |
246 | |
{ |
247 | |
do |
248 | |
{ |
249 | 42 | workingDirectoryFile = workingDirectoryFile.getParentFile(); |
250 | 42 | num++; |
251 | |
} |
252 | 42 | while ( !workingDirectoryFile.equals( basedirFile ) ); |
253 | |
} |
254 | 96 | return num; |
255 | |
} |
256 | |
|
257 | |
public static String realignScmUrl( int parentLevels, String url ) |
258 | |
{ |
259 | 204 | if ( !StringUtils.isEmpty( url ) ) |
260 | |
{ |
261 | 204 | int index = url.length(); |
262 | 204 | String suffix = ""; |
263 | 204 | if ( url.endsWith( "/" ) ) |
264 | |
{ |
265 | 20 | index--; |
266 | 20 | suffix = "/"; |
267 | |
} |
268 | 256 | for ( int i = 0; i < parentLevels && index > 0; i++ ) |
269 | |
{ |
270 | 52 | index = url.lastIndexOf( '/', index - 1 ); |
271 | |
} |
272 | |
|
273 | 204 | if ( index > 0 ) |
274 | |
{ |
275 | 200 | url = url.substring( 0, index ) + suffix; |
276 | |
} |
277 | |
} |
278 | 204 | return url; |
279 | |
} |
280 | |
|
281 | |
public static boolean isSymlink( File file ) |
282 | |
throws IOException |
283 | |
{ |
284 | 192 | return !file.getAbsolutePath().equals( file.getCanonicalPath() ); |
285 | |
} |
286 | |
} |