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   */
39  public class ScmFileSet implements Serializable {
40      private static final long serialVersionUID = -5978597349974797556L;
41  
42      private static final String DELIMITER = ",";
43  
44      /** @see DirectoryScanner#DEFAULTEXCLUDES */
45      private static final String DEFAULT_EXCLUDES = StringUtils.join(DirectoryScanner.DEFAULTEXCLUDES, DELIMITER);
46  
47      private final File basedir;
48  
49      private String includes;
50  
51      private String excludes;
52  
53      /**
54       * List of File objects, all relative to the basedir.
55       */
56      private final List<File> files;
57  
58      /**
59       * Create a file set with no files, only the base directory.
60       *
61       * @param basedir directory files in the set are relative to
62       */
63      public ScmFileSet(File basedir) {
64          this(basedir, new ArrayList<>(0));
65      }
66  
67      /**
68       * Create a file set with only the file provided, relative to basedir.
69       *
70       * @param basedir directory file is relative to
71       * @param file    file that the set will contain, has to be relative to basedir
72       */
73      public ScmFileSet(File basedir, File file) {
74          this(basedir, new File[] {file});
75      }
76  
77      /**
78       * Create a file set with only files (not directories) from basefile,
79       * using includes and excludes provided.
80       *
81       * @param basedir  directory files are relative to
82       * @param includes Ant pattern for files to include
83       * @param excludes Ant pattern for files to exclude,
84       *                 if null DEFAULT_EXCLUDES is used, else DEFAULT_EXCLUDES is added.
85       * @throws IOException if any
86       */
87      public ScmFileSet(File basedir, String includes, String excludes) throws IOException {
88          this.basedir = basedir;
89  
90          if (excludes != null && excludes.length() > 0) {
91              excludes += DELIMITER + DEFAULT_EXCLUDES;
92          } else {
93              excludes = DEFAULT_EXCLUDES;
94          }
95          this.files = FileUtils.getFiles(basedir, includes, excludes, false);
96          this.includes = includes;
97          this.excludes = excludes;
98      }
99  
100     /**
101      * Create a file set with files from basefile, using includes provided and default excludes.
102      *
103      * @param basedir  directory files are relative to
104      * @param includes Ant pattern for files to include
105      * @throws IOException if any
106      * @since 1.0
107      */
108     public ScmFileSet(File basedir, String includes) throws IOException {
109         this(basedir, includes, null);
110     }
111 
112     /**
113      * Create a file set with the files provided, relative to basedir.
114      *
115      * @param basedir directory files are relative to
116      * @param files   files that the set will contain, have to be relative to basedir
117      * @deprecated use ScmFileSet( File, List )
118      */
119     public ScmFileSet(File basedir, File[] files) {
120         this(basedir, Arrays.asList(files));
121     }
122 
123     /**
124      * Create a file set with the files provided, relative to basedir.
125      *
126      * @param basedir directory files are relative to
127      * @param files   list of File objects, files that the set will contain, have to be relative to basedir
128      */
129     public ScmFileSet(File basedir, List<File> files) {
130         if (basedir == null) {
131             throw new NullPointerException("basedir must not be null");
132         }
133 
134         if (files == null) {
135             throw new NullPointerException("files must not be null");
136         }
137 
138         this.basedir = basedir;
139         this.files = files;
140     }
141 
142     /**
143      * Get the base directory of the file set. It's the directory files in the set are relative to.
144      *
145      * @return base directory
146      */
147     public File getBasedir() {
148         return basedir;
149     }
150 
151     /**
152      * Get the list of files in the set, relative to basedir
153      *
154      * @return files in this set
155      * @deprecated use getFileList() instead
156      */
157     public File[] getFiles() {
158         return this.files.toArray(new File[this.files.size()]);
159     }
160 
161     /**
162      * Get the list of files in the set, relative to basedir
163      *
164      * @return List of File objects
165      */
166     public List<File> getFileList() {
167         return this.files;
168     }
169 
170     /**
171      * @return the includes files as a comma separated string
172      */
173     public String getIncludes() {
174         return this.includes;
175     }
176 
177     /**
178      * @return the excludes files as a comma separated string
179      */
180     public String getExcludes() {
181         return this.excludes;
182     }
183 
184     /** {@inheritDoc} */
185     public String toString() {
186         return "basedir = " + basedir + "; files = " + files;
187     }
188 }