View Javadoc
1   package org.apache.maven.plugin.assembly.utils;
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 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   * @version $Id: AssemblyFileUtils.java 1633832 2014-10-23 15:03:47Z krosenvold $
35   */
36  public final class AssemblyFileUtils
37  {
38  
39      private AssemblyFileUtils()
40      {
41          // no op
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      public static void verifyTempDirectoryAvailability( @Nonnull
85      final File tempDir )
86      {
87          if ( !tempDir.exists() )
88          {
89              // noinspection ResultOfMethodCallIgnored
90              tempDir.mkdirs();
91          }
92      }
93  
94      /**
95       * Unpacks the archive file.
96       *
97       * @param source File to be unpacked.
98       * @param destDir Location where to put the unpacked files.
99       */
100     public static void unpack( File source, File destDir, ArchiverManager archiverManager )
101         throws ArchiveExpansionException, NoSuchArchiverException
102     {
103         try
104         {
105             UnArchiver unArchiver = archiverManager.getUnArchiver( source );
106 
107             unArchiver.setSourceFile( source );
108 
109             unArchiver.setDestDirectory( destDir );
110 
111             unArchiver.extract();
112         }
113         catch ( ArchiverException e )
114         {
115             throw new ArchiveExpansionException( "Error unpacking file: " + source + "to: " + destDir, e );
116         }
117     }
118 
119     @Nonnull
120     private static String normalizePath( @Nonnull String path )
121     {
122         return path.replace( '\\', '/' );
123     }
124 
125     @Nonnull
126     public static String normalizeFileInfo( @Nonnull FileInfo fileInfo )
127     {
128         String name = fileInfo.getName();
129         name = normalizePath( name );
130         return name.replace( File.separatorChar, '/' ); // How can this be anything but a no-op
131     }
132 
133     public static boolean isPropertyFile( String sourceName )
134     {
135         return sourceName.toLowerCase( Locale.ENGLISH ).endsWith( ".properties" );
136     }
137 
138     public static boolean isPropertyFile( File file )
139     {
140         return isPropertyFile( file.getName());
141     }
142 }