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.List;
32  
33  /**
34   * @version $Id: AddDirectoryTask.java 1478985 2013-05-03 21:33:10Z krosenvold $
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 ( String include : includes )
113                         {
114                             String value = AssemblyFormatUtils.fixRelativeRefs( include );
115 
116                             if ( value.startsWith( "/" ) || value.startsWith( "\\" ) )
117                             {
118                                 value = value.substring( 1 );
119                             }
120 
121                             includesArray[i] = value;
122 
123                             i++;
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                         String value = AssemblyFormatUtils.fixRelativeRefs( directoryExclude );
134 
135                         if ( value.startsWith( "/" ) || value.startsWith( "\\" ) )
136                         {
137                             value = value.substring( 1 );
138                         }
139 
140                         excludesArray[i] = value;
141 
142                         i++;
143                     }
144 
145                     final DefaultFileSet fs = new DefaultFileSet();
146                     fs.setUsingDefaultExcludes( useDefaultExcludes );
147                     fs.setPrefix( outputDirectory );
148                     fs.setDirectory( directory );
149                     fs.setIncludes( includesArray );
150                     fs.setExcludes( excludesArray );
151 
152                     archiver.addFileSet( fs );
153                 }
154                 catch ( final ArchiverException e )
155                 {
156                     throw new ArchiveCreationException( "Error adding directory to archive: " + e.getMessage(), e );
157                 }
158             }
159         }
160         finally
161         {
162             if ( dirModeSet )
163             {
164                 archiver.setDirectoryMode( oldDirMode );
165             }
166 
167             if ( fileModeSet )
168             {
169                 archiver.setFileMode( oldFileMode );
170             }
171         }
172     }
173 
174     public void setExcludes( final List<String> excludes )
175     {
176         this.excludes = excludes;
177     }
178 
179     public void setIncludes( final List<String> includes )
180     {
181         this.includes = includes;
182     }
183 
184     public void setOutputDirectory( final String outputDirectory )
185     {
186         this.outputDirectory = outputDirectory;
187     }
188 
189     public void setDirectoryMode( final int directoryMode )
190     {
191         this.directoryMode = directoryMode;
192     }
193 
194     public void setFileMode( final int fileMode )
195     {
196         this.fileMode = fileMode;
197     }
198 
199     public void setUseDefaultExcludes( final boolean useDefaultExcludes )
200     {
201         this.useDefaultExcludes = useDefaultExcludes;
202     }
203 
204 }