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 1633925 2014-10-23 19:08:20Z 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      public AddDirectoryTask( final File directory )
62      {
63          this( directory, null);
64      }
65  
66      public void execute( final Archiver archiver )
67          throws ArchiveCreationException
68      {
69          if ( ".".equals( outputDirectory ) )
70          {
71              outputDirectory = "";
72          }
73          else if ( "..".equals( outputDirectory ) )
74          {
75              throw new ArchiveCreationException( "Cannot add source directory: " + directory + " to archive-path: "
76                  + outputDirectory + ". All paths must be within the archive root directory." );
77          }
78  
79          final int oldDirMode = archiver.getOverrideDirectoryMode();
80          final int oldFileMode = archiver.getOverrideFileMode();
81  
82          boolean fileModeSet = false;
83          boolean dirModeSet = false;
84  
85          try
86          {
87              if ( directoryMode != -1 )
88              {
89                  archiver.setDirectoryMode( directoryMode );
90                  dirModeSet = true;
91              }
92  
93              if ( fileMode != -1 )
94              {
95                  archiver.setFileMode( fileMode );
96                  fileModeSet = true;
97              }
98  
99              if ( directory.exists() )
100             {
101                 List<String> directoryExcludes;
102                 if ( excludes != null && !excludes.isEmpty() )
103                 {
104                     directoryExcludes = new ArrayList<String>( excludes );
105                 }
106                 else
107                 {
108                     directoryExcludes = new ArrayList<String>();
109                 }
110 
111                 try
112                 {
113                     String[] includesArray = null;
114                     if ( includes != null && !includes.isEmpty() )
115                     {
116                         includesArray = new String[includes.size()];
117 
118                         int i = 0;
119                         for ( String include : includes )
120                         {
121                             includesArray[i++] = normalize( include );
122                         }
123                     }
124 
125                     // this one is guaranteed to be non-null by code above.
126                     final String[] excludesArray = new String[directoryExcludes.size()];
127 
128                     int i = 0;
129                     for ( String directoryExclude : directoryExcludes )
130                     {
131                         excludesArray[i++] = normalize( directoryExclude );
132                     }
133 
134                     final DefaultFileSet fs = new DefaultFileSet();
135                     fs.setUsingDefaultExcludes( useDefaultExcludes );
136                     fs.setPrefix( outputDirectory );
137                     fs.setDirectory( directory );
138                     fs.setIncludes( includesArray );
139                     fs.setExcludes( excludesArray );
140                     if (transformer != null){
141                             fs.setStreamTransformer( transformer );
142                     }
143 
144                     archiver.addFileSet( fs );
145                 }
146                 catch ( final ArchiverException e )
147                 {
148                     throw new ArchiveCreationException( "Error adding directory to archive: " + e.getMessage(), e );
149                 }
150             }
151         }
152         finally
153         {
154             if ( dirModeSet )
155             {
156                 archiver.setDirectoryMode( oldDirMode );
157             }
158 
159             if ( fileModeSet )
160             {
161                 archiver.setFileMode( oldFileMode );
162             }
163         }
164     }
165 
166     private String normalize( String include )
167     {
168         String value = AssemblyFormatUtils.fixRelativeRefs( include );
169 
170         if ( value.startsWith( "/" ) || value.startsWith( "\\" ) )
171         {
172             value = value.substring( 1 );
173         }
174         return value;
175     }
176 
177     public void setExcludes( final List<String> excludes )
178     {
179         this.excludes = excludes;
180     }
181 
182     public void setIncludes( final List<String> includes )
183     {
184         this.includes = includes;
185     }
186 
187     public void setOutputDirectory( final String outputDirectory )
188     {
189         this.outputDirectory = outputDirectory;
190     }
191 
192     public void setDirectoryMode( final int directoryMode )
193     {
194         this.directoryMode = directoryMode;
195     }
196 
197     public void setFileMode( final int fileMode )
198     {
199         this.fileMode = fileMode;
200     }
201 
202     public void setUseDefaultExcludes( final boolean useDefaultExcludes )
203     {
204         this.useDefaultExcludes = useDefaultExcludes;
205     }
206 
207 }