View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.Serializable;
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.List;
27  
28  import org.apache.commons.lang3.StringUtils;
29  import org.codehaus.plexus.util.DirectoryScanner;
30  import org.codehaus.plexus.util.FileUtils;
31  
32  /**
33   * Set of files used for SCM operations.
34   * Consists of the base directory of the files and a list of files relative to that directory.
35   *
36   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
37   */
38  public class ScmFileSet implements Serializable {
39      private static final long serialVersionUID = -5978597349974797556L;
40  
41      private static final String DELIMITER = ",";
42  
43      /**
44       * @see DirectoryScanner#DEFAULTEXCLUDES
45       */
46      private static final String DEFAULT_EXCLUDES = StringUtils.join(DirectoryScanner.DEFAULTEXCLUDES, DELIMITER);
47  
48      private final File basedir;
49  
50      private String includes;
51  
52      private String excludes;
53  
54      /**
55       * List of File objects, all relative to the basedir.
56       */
57      private final List<File> files;
58  
59      /**
60       * Create a file set with no files, only the base directory.
61       *
62       * @param basedir directory files in the set are relative to
63       */
64      public ScmFileSet(File basedir) {
65          this(basedir, new ArrayList<>(0));
66      }
67  
68      /**
69       * Create a file set with only the file provided, relative to basedir.
70       *
71       * @param basedir directory file is relative to
72       * @param file    file that the set will contain, has to be relative to basedir
73       */
74      public ScmFileSet(File basedir, File file) {
75          this(basedir, new File[] {file});
76      }
77  
78      /**
79       * Create a file set with only files (not directories) from basefile,
80       * using includes and excludes provided.
81       *
82       * @param basedir  directory files are relative to
83       * @param includes ant pattern for files to include
84       * @param excludes ant pattern for files to exclude,
85       *                 if null DEFAULT_EXCLUDES is used, else DEFAULT_EXCLUDES is added
86       * @throws IOException if any
87       */
88      public ScmFileSet(File basedir, String includes, String excludes) throws IOException {
89          this.basedir = basedir;
90  
91          if (excludes != null && excludes.length() > 0) {
92              excludes += DELIMITER + DEFAULT_EXCLUDES;
93          } else {
94              excludes = DEFAULT_EXCLUDES;
95          }
96          this.files = FileUtils.getFiles(basedir, includes, excludes, false);
97          this.includes = includes;
98          this.excludes = excludes;
99      }
100 
101     /**
102      * Create a file set with files from basefile, using includes provided and default excludes.
103      *
104      * @param basedir  directory files are relative to
105      * @param includes ant pattern for files to include
106      * @throws IOException if any
107      * @since 1.0
108      */
109     public ScmFileSet(File basedir, String includes) throws IOException {
110         this(basedir, includes, null);
111     }
112 
113     /**
114      * Create a file set with the files provided, relative to basedir.
115      *
116      * @param basedir directory files are relative to
117      * @param files   files that the set will contain, have to be relative to basedir
118      * @deprecated use ScmFileSet( File, List )
119      */
120     public ScmFileSet(File basedir, File[] files) {
121         this(basedir, Arrays.asList(files));
122     }
123 
124     /**
125      * Create a file set with the files provided, relative to basedir.
126      *
127      * @param basedir directory files are relative to
128      * @param files   list of File objects, files that the set will contain, have to be relative to basedir
129      */
130     public ScmFileSet(File basedir, List<File> files) {
131         if (basedir == null) {
132             throw new NullPointerException("basedir must not be null");
133         }
134 
135         if (files == null) {
136             throw new NullPointerException("files must not be null");
137         }
138 
139         this.basedir = basedir;
140         this.files = files;
141     }
142 
143     /**
144      * Get the base directory of the file set. It's the directory files in the set are relative to.
145      *
146      * @return base directory
147      */
148     public File getBasedir() {
149         return basedir;
150     }
151 
152     /**
153      * Get the list of files in the set, relative to basedir.
154      *
155      * @return files in this set
156      * @deprecated use getFileList() instead
157      */
158     public File[] getFiles() {
159         return this.files.toArray(new File[this.files.size()]);
160     }
161 
162     /**
163      * Get the list of files in the set, relative to basedir.
164      *
165      * @return list of File objects
166      */
167     public List<File> getFileList() {
168         return this.files;
169     }
170 
171     /**
172      * @return the includes files as a comma separated string
173      */
174     public String getIncludes() {
175         return this.includes;
176     }
177 
178     /**
179      * @return the excludes files as a comma separated string
180      */
181     public String getExcludes() {
182         return this.excludes;
183     }
184 
185     /**
186      * {@inheritDoc}
187      */
188     public String toString() {
189         return "basedir = " + basedir + "; files = " + files;
190     }
191 }