View Javadoc
1   package org.apache.maven.plugin.assembly.archive.task;
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.ArchiveCreationException;
23  import org.apache.maven.plugin.assembly.utils.AssemblyFormatUtils;
24  import org.codehaus.plexus.archiver.Archiver;
25  import org.codehaus.plexus.archiver.ArchiverException;
26  import org.codehaus.plexus.archiver.util.DefaultFileSet;
27  import org.codehaus.plexus.components.io.functions.InputStreamTransformer;
28  
29  import java.io.File;
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  /**
34   * @version $Id: AddDirectoryTask.java 1639422 2014-11-13 18:08:07Z krosenvold $
35   */
36  public class AddDirectoryTask
37  {
38  
39      private final File directory;
40  
41      private final InputStreamTransformer transformer;
42  
43      private List<String> includes;
44  
45      private List<String> excludes;
46  
47      private String outputDirectory;
48  
49      private boolean useDefaultExcludes = true;
50  
51      private int directoryMode = -1;
52  
53      private int fileMode = -1;
54  
55      public AddDirectoryTask( final File directory, InputStreamTransformer transformers )
56      {
57          this.directory = directory;
58  
59          this.transformer = transformers;
60      }
61  
62      public AddDirectoryTask( final File directory )
63      {
64          this( directory, null );
65      }
66  
67      public void execute( final Archiver archiver )
68          throws ArchiveCreationException
69      {
70          if ( ".".equals( outputDirectory ) )
71          {
72              outputDirectory = "";
73          }
74          else if ( "..".equals( outputDirectory ) )
75          {
76              throw new ArchiveCreationException(
77                  "Cannot add source directory: " + directory + " to archive-path: " + outputDirectory
78                      + ". All paths must be within the archive root directory." );
79          }
80  
81          final int oldDirMode = archiver.getOverrideDirectoryMode();
82          final int oldFileMode = archiver.getOverrideFileMode();
83  
84          boolean fileModeSet = false;
85          boolean dirModeSet = false;
86  
87          try
88          {
89              if ( directoryMode != -1 )
90              {
91                  archiver.setDirectoryMode( directoryMode );
92                  dirModeSet = true;
93              }
94  
95              if ( fileMode != -1 )
96              {
97                  archiver.setFileMode( fileMode );
98                  fileModeSet = true;
99              }
100 
101             if ( directory.exists() )
102             {
103                 List<String> directoryExcludes;
104                 if ( excludes != null && !excludes.isEmpty() )
105                 {
106                     directoryExcludes = new ArrayList<String>( excludes );
107                 }
108                 else
109                 {
110                     directoryExcludes = new ArrayList<String>();
111                 }
112 
113                 try
114                 {
115                     String[] includesArray = null;
116                     if ( includes != null && !includes.isEmpty() )
117                     {
118                         includesArray = new String[includes.size()];
119 
120                         int i = 0;
121                         for ( String include : includes )
122                         {
123                             includesArray[i++] = normalize( include );
124                         }
125                     }
126 
127                     // this one is guaranteed to be non-null by code above.
128                     final String[] excludesArray = new String[directoryExcludes.size()];
129 
130                     int i = 0;
131                     for ( String directoryExclude : directoryExcludes )
132                     {
133                         excludesArray[i++] = normalize( directoryExclude );
134                     }
135 
136                     final DefaultFileSet fs = new DefaultFileSet();
137                     fs.setUsingDefaultExcludes( useDefaultExcludes );
138                     fs.setPrefix( outputDirectory );
139                     fs.setDirectory( directory );
140                     fs.setIncludes( includesArray );
141                     fs.setExcludes( excludesArray );
142                     if ( transformer != null )
143                     {
144                         fs.setStreamTransformer( transformer );
145                     }
146 
147                     archiver.addFileSet( fs );
148                 }
149                 catch ( final ArchiverException e )
150                 {
151                     throw new ArchiveCreationException( "Error adding directory to archive: " + e.getMessage(), e );
152                 }
153             }
154         }
155         finally
156         {
157             if ( dirModeSet )
158             {
159                 archiver.setDirectoryMode( oldDirMode );
160             }
161 
162             if ( fileModeSet )
163             {
164                 archiver.setFileMode( oldFileMode );
165             }
166         }
167     }
168 
169     private String normalize( String include )
170     {
171         String value = AssemblyFormatUtils.fixRelativeRefs( include );
172 
173         if ( value.startsWith( "/" ) || value.startsWith( "\\" ) )
174         {
175             value = value.substring( 1 );
176         }
177         return value;
178     }
179 
180     public void setExcludes( final List<String> excludes )
181     {
182         this.excludes = excludes;
183     }
184 
185     public void setIncludes( final List<String> includes )
186     {
187         this.includes = includes;
188     }
189 
190     public void setOutputDirectory( final String outputDirectory )
191     {
192         this.outputDirectory = outputDirectory;
193     }
194 
195     public void setDirectoryMode( final int directoryMode )
196     {
197         this.directoryMode = directoryMode;
198     }
199 
200     public void setFileMode( final int fileMode )
201     {
202         this.fileMode = fileMode;
203     }
204 
205     public void setUseDefaultExcludes( final boolean useDefaultExcludes )
206     {
207         this.useDefaultExcludes = useDefaultExcludes;
208     }
209 
210 }