This repository has been archived by the owner on Oct 14, 2020. It is now read-only.
forked from develar/flexmojos
/
MavenUtils.java
367 lines (311 loc) · 11.1 KB
/
MavenUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/**
* Copyright 2008 Marvin Herman Froeder
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*
*/
package org.sonatype.flexmojos.plugin.utilities;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Build;
import org.apache.maven.model.Resource;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.FileUtils;
import scala.actors.threadpool.Arrays;
/**
* Utility class to help get information from Maven objects like files, source paths, resolve dependencies, etc.
*
* @author velo.br
*/
public class MavenUtils
{
private static final String FREE_BSD = "freebsd";
private static final String WINDOWS_OS = "windows";
private static final String MAC_OS = "mac os x";
private static final String MAC_OS_DARWIN = "darwin";
private static final String LINUX_OS = "linux";
private static final String SOLARIS_OS = "sunos";
private static final String VISTA = "vista";
private static final Properties flexmojosProperties;
static
{
flexmojosProperties = new Properties();
try
{
flexmojosProperties.load( MavenUtils.class.getResourceAsStream( "/flexmojos.properties" ) );
}
catch ( IOException e )
{
throw new RuntimeException( "Unable to load flexmojos.properties", e );
}
}
private MavenUtils()
{
}
/**
* Resolve a resource file in a maven project resources folders
*
* @param project maven project
* @param fileName sugested name on pom
* @return source file or null if source not found
* @throws MojoFailureException
*/
public static File resolveResourceFile( MavenProject project, String fileName )
throws MojoFailureException
{
File file = new File( fileName );
if ( file.exists() )
{
return file;
}
if ( file.isAbsolute() )
{
throw new MojoFailureException( "File " + fileName + " not found" );
}
List<Resource> resources = project.getBuild().getResources();
for ( Resource resourceFolder : resources )
{
File resource = new File( resourceFolder.getDirectory(), fileName );
if ( resource.exists() )
{
return resource;
}
}
throw new MojoFailureException( "File " + fileName + " not found" );
}
/**
* Returns the file reference to the fonts file. Depending on the os, the correct fonts.ser file is used. The fonts
* file is copied to the build directory.
*
* @param build Build for which to get the fonts file
* @return file reference to fonts file
* @throws MojoExecutionException thrown if the config file could not be copied to the build directory
*/
public static File getFontsFile( Build build )
throws MojoExecutionException
{
URL url;
if ( MavenUtils.isMac() )
{
url = MavenUtils.class.getResource( "/fonts/macFonts.ser" );
}
else
{
// TODO And linux?!
// if(os.contains("windows")) {
url = MavenUtils.class.getResource( "/fonts/winFonts.ser" );
}
File fontsSer = new File( build.getOutputDirectory(), "fonts.ser" );
try
{
FileUtils.copyURLToFile( url, fontsSer );
}
catch ( IOException e )
{
throw new MojoExecutionException( "Error copying fonts file.", e );
}
return fontsSer;
}
/**
* Returns the file reference to a localize resourceBundlePath. Replaces the {locale} variable in the given
* resourceBundlePath with given locale.
*
* @param resourceBundlePath Path to resource bundle.
* @param locale Locale
* @return File reference to the resourceBundlePath for given locale
*/
public static File getLocaleResourcePath( String resourceBundlePath, String locale )
{
String path = resourceBundlePath.replace( "{locale}", locale );
File localePath = new File( path );
return localePath.exists() ? localePath : null;
}
public static String osString()
{
return System.getProperty( "os.name" ).toLowerCase();
}
/**
* Return a boolean to show if we are running on Windows.
*
* @return true if we are running on Windows.
*/
public static boolean isWindows()
{
return osString().startsWith( WINDOWS_OS );
}
/**
* Return a boolean to show if we are running on Linux.
*
* @return true if we are running on Linux.
*/
public static boolean isLinux()
{
return osString().startsWith( LINUX_OS ) ||
// I know, but people said that workds...
osString().startsWith( FREE_BSD );
}
/**
* Return a boolean to show if we are running on Solaris.
*
* @return true if we are running on Solaris.
*/
public static boolean isSolaris()
{
return osString().startsWith( SOLARIS_OS );
}
/**
* Return a boolean to show if we are running on a unix-based OS.
*
* @return true if we are running on a unix-based OS.
*/
public static boolean isUnixBased()
{
return isLinux() || isSolaris();
}
/**
* Return a boolean to show if we are running on Mac OS X.
*
* @return true if we are running on Mac OS X.
*/
public static boolean isMac()
{
return osString().startsWith( MAC_OS ) || osString().startsWith( MAC_OS_DARWIN );
}
/**
* Return a boolean to show if we are running on Windows Vista.
*
* @return true if we are running on Windows Vista.
*/
public static boolean isWindowsVista()
{
return osString().startsWith( WINDOWS_OS ) && osString().contains( VISTA );
}
public static Artifact searchFor( Collection<Artifact> artifacts, String groupId, String artifactId,
String version, String type, String classifier )
{
for ( Artifact artifact : artifacts )
{
if ( equals( artifact.getGroupId(), groupId ) && equals( artifact.getArtifactId(), artifactId )
&& equals( artifact.getVersion(), version ) && equals( artifact.getType(), type )
&& equals( artifact.getClassifier(), classifier ) )
{
return artifact;
}
}
return null;
}
private static boolean equals( String str1, String str2 )
{
// If is null is not relevant
if ( str1 == null || str2 == null )
{
return true;
}
return str1.equals( str2 );
}
public static String getFlexMojosVersion()
{
return flexmojosProperties.getProperty( "version" );
}
public static String replaceArtifactCoordinatesTokens( String sample, Artifact artifact )
{
sample = sample.replace( "{groupId}", artifact.getGroupId() );
sample = sample.replace( "{artifactId}", artifact.getArtifactId() );
sample = sample.replace( "{version}", artifact.getBaseVersion() );
if ( artifact.getClassifier() != null )
{
sample = sample.replace( "{classifier}", artifact.getClassifier() );
}
sample = sample.replace( "{hard-version}", artifact.getVersion() );
return sample;
}
public static String interpolateRslUrl( String baseUrl, Artifact artifact, String extension, String contextRoot )
{
if ( baseUrl == null )
{
return null;
}
if ( contextRoot == null || "".equals( contextRoot ) )
{
baseUrl = baseUrl.replace( "{contextRoot}/", "" );
}
else
{
baseUrl = baseUrl.replace( "{contextRoot}", contextRoot );
}
baseUrl = replaceArtifactCoordinatesTokens( baseUrl, artifact );
if ( extension != null )
{
baseUrl = baseUrl.replace( "{extension}", extension );
}
return baseUrl;
}
public static String getRuntimeLocaleOutputPath( String sample, Artifact artifact, String locale, String extension )
{
String path = replaceArtifactCoordinatesTokens( sample, artifact );
path = path.replace( "{locale}", locale );
path = path.replace( "{extension}", extension );
return path;
}
public static String getRuntimeLocaleOutputName( String sample, Artifact artifact, String locale )
{
String path = replaceArtifactCoordinatesTokens( sample, artifact );
path = path.replace( "{locale}", locale );
return path;
}
public static Set<File> getFilesSet( Collection<Artifact>... dependenciesSet )
{
if ( dependenciesSet == null )
{
return null;
}
Set<File> files = new LinkedHashSet<File>();
for ( Collection<Artifact> dependencies : dependenciesSet )
{
if ( dependencies == null )
{
continue;
}
for ( Artifact artifact : dependencies )
{
if ( !artifact.isResolved() )
{
throw new IllegalArgumentException( "Unable to handle unresolved artifact: " + artifact );
}
if ( artifact.getFile() == null )
{
throw new NullPointerException( "Artifact file not defined: " + artifact );
}
files.add( artifact.getFile() );
}
}
return files;
}
@SuppressWarnings( "unchecked" )
public static File[] getFiles( Artifact... dependencies )
{
if ( dependencies == null )
{
return null;
}
return getFilesSet( Arrays.asList( dependencies ) ).toArray( new File[0] );
}
public static File[] getFiles( Collection<Artifact>... dependencies )
{
if ( dependencies == null )
{
return null;
}
return getFilesSet( dependencies ).toArray( new File[0] );
}
}