View Javadoc

1   /*
2    * Copyright 2010 Felix Roethenbacher
3    */
4   package ch.syabru.maven.timemachine;
5   
6   import org.apache.commons.lang.SystemUtils;
7   import org.apache.maven.plugin.AbstractMojo;
8   import org.apache.maven.plugin.MojoExecutionException;
9   
10  import java.io.File;
11  import java.io.IOException;
12  
13  /**
14   * Goal which excludes ${project.build.directory} from TimeMachine backup.
15   * 
16   * This plugin adds an extended attribute called
17   * <i>com.apple.metadata:com_apple_backup_excludeItem = "com.apple.backupd"</i>
18   * to the output directory which excludes the directory from being
19   * backed up by TimeMachine.
20   *
21   * The plugin only runs when run on MAC OS X.
22   *
23   * @goal exclude
24   * 
25   * @phase initialize
26   */
27  public class TimeMachineMojo extends AbstractMojo
28  {
29      /**
30       * Location of the build directory.
31       * @parameter expression="${project.build.directory}"
32       * @required
33       */
34      private File outputDirectory;
35  
36      public void execute() throws MojoExecutionException
37      {
38          if (SystemUtils.IS_OS_MAC_OSX) {
39              File f = outputDirectory;
40      
41              if (!f.exists())
42              {
43                  f.mkdirs();
44              }
45              String cmd = "xattr -w com.apple.metadata:" +
46              		"com_apple_backup_excludeItem com.apple.backupd " +
47                      outputDirectory.getAbsolutePath();
48              try {
49                  Runtime.getRuntime().exec(cmd);
50              } catch (IOException e) {
51                  throw new MojoExecutionException("Error executing " + cmd, e);
52              }
53          }
54      }
55  }