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