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