View Javadoc
1   package org.codehaus.plexus.archiver;
2   
3   import java.io.BufferedReader;
4   import java.io.File;
5   import java.io.IOException;
6   import java.nio.charset.StandardCharsets;
7   import java.nio.file.Files;
8   import java.util.Collections;
9   import java.util.List;
10  
11  import org.codehaus.plexus.archiver.util.DefaultFileSet;
12  import org.codehaus.plexus.util.FileUtils;
13  import org.codehaus.plexus.util.StringUtils;
14  
15  /**
16   * An {@link ArchiveFinalizer} that process dot files with archiver directives
17   * contained within. This basically means you can communicate archive creation
18   * instructions between processes using dot files.
19   *
20   * @author Jason van Zyl
21   */
22  public class DotDirectiveArchiveFinalizer extends AbstractArchiveFinalizer {
23  
24      private static final String DEFAULT_DOT_FILE_PREFIX = ".plxarc";
25  
26      private final File dotFileDirectory;
27  
28      private final String dotFilePrefix;
29  
30      public DotDirectiveArchiveFinalizer(File dotFileDirectory) {
31          this(dotFileDirectory, DEFAULT_DOT_FILE_PREFIX);
32      }
33  
34      public DotDirectiveArchiveFinalizer(File dotFileDirectory, String dotFilePrefix) {
35          this.dotFileDirectory = dotFileDirectory;
36  
37          this.dotFilePrefix = dotFilePrefix;
38      }
39  
40      @Override
41      public void finalizeArchiveCreation(Archiver archiver) throws ArchiverException {
42          try {
43              List<File> dotFiles = FileUtils.getFiles(dotFileDirectory, dotFilePrefix + "*", null);
44  
45              for (File dotFile : dotFiles) {
46                  try (BufferedReader in = Files.newBufferedReader(dotFile.toPath(), StandardCharsets.UTF_8)) {
47  
48                      for (String line = in.readLine(); line != null; line = in.readLine()) {
49                          String[] s = StringUtils.split(line, ":");
50  
51                          if (s.length == 1) {
52                              File directory = new File(dotFileDirectory, s[0]);
53  
54                              System.out.println("adding directory = " + directory);
55  
56                              archiver.addFileSet(new DefaultFileSet(directory));
57                          } else {
58                              File directory = new File(dotFileDirectory, s[0]);
59  
60                              System.out.println("adding directory = " + directory + " to: " + s[1]);
61  
62                              if (s[1].endsWith("/")) {
63  
64                                  archiver.addFileSet(new DefaultFileSet(directory).prefixed(s[1]));
65                              } else {
66                                  archiver.addFileSet(new DefaultFileSet(directory).prefixed(s[1] + "/"));
67                              }
68                          }
69                      }
70                  }
71              }
72          } catch (IOException e) {
73              throw new ArchiverException("Error processing dot files.", e);
74          }
75      }
76  
77      @Override
78      public List getVirtualFiles() {
79          return Collections.EMPTY_LIST;
80      }
81  }