1 package org.apache.maven.plugin.assembly.utils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.plugin.assembly.archive.ArchiveExpansionException;
23 import org.codehaus.plexus.archiver.ArchiverException;
24 import org.codehaus.plexus.archiver.UnArchiver;
25 import org.codehaus.plexus.archiver.manager.ArchiverManager;
26 import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
27 import org.codehaus.plexus.components.io.fileselectors.FileInfo;
28
29 import javax.annotation.Nonnull;
30 import java.io.File;
31 import java.util.Locale;
32
33
34
35
36 public final class AssemblyFileUtils
37 {
38
39 private AssemblyFileUtils()
40 {
41
42 }
43
44 public static String makePathRelativeTo( String path, final File basedir )
45 {
46 if ( basedir == null )
47 {
48 return path;
49 }
50
51 if ( path == null )
52 {
53 return null;
54 }
55
56 path = path.trim();
57
58 String base = basedir.getAbsolutePath();
59 if ( path.startsWith( base ) )
60 {
61 path = path.substring( base.length() );
62 if ( path.length() > 0 )
63 {
64 if ( path.startsWith( "/" ) || path.startsWith( "\\" ) )
65 {
66 path = path.substring( 1 );
67 }
68 }
69
70 if ( path.length() == 0 )
71 {
72 path = ".";
73 }
74 }
75
76 if ( !new File( path ).isAbsolute() )
77 {
78 path = path.replace( '\\', '/' );
79 }
80
81 return path;
82 }
83
84 @SuppressWarnings( "ResultOfMethodCallIgnored" )
85 public static void verifyTempDirectoryAvailability( @Nonnull final File tempDir )
86 {
87 if ( !tempDir.exists() )
88 {
89 tempDir.mkdirs();
90 }
91 }
92
93
94
95
96
97
98
99 public static void unpack( File source, File destDir, ArchiverManager archiverManager )
100 throws ArchiveExpansionException, NoSuchArchiverException
101 {
102 try
103 {
104 UnArchiver unArchiver = archiverManager.getUnArchiver( source );
105
106 unArchiver.setSourceFile( source );
107
108 unArchiver.setDestDirectory( destDir );
109
110 unArchiver.extract();
111 }
112 catch ( ArchiverException e )
113 {
114 throw new ArchiveExpansionException( "Error unpacking file: " + source + "to: " + destDir, e );
115 }
116 }
117
118 @Nonnull
119 private static String normalizePath( @Nonnull String path )
120 {
121 return path.replace( '\\', '/' );
122 }
123
124 @Nonnull
125 public static String normalizeFileInfo( @Nonnull FileInfo fileInfo )
126 {
127 String name = fileInfo.getName();
128 name = normalizePath( name );
129 return name.replace( File.separatorChar, '/' );
130 }
131
132 public static boolean isPropertyFile( String sourceName )
133 {
134 return sourceName.toLowerCase( Locale.ENGLISH ).endsWith( ".properties" );
135 }
136
137 public static boolean isPropertyFile( File file )
138 {
139 return isPropertyFile( file.getName() );
140 }
141 }