View Javadoc
1   package org.apache.maven.plugins.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.plugins.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.html 1016737 2017-08-13 12:01:54Z khmarbaise $
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      @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       * Unpacks the archive file.
95       *
96       * @param source  File to be unpacked.
97       * @param destDir Location where to put the unpacked files.
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, '/' ); // How can this be anything but a no-op
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 }