View Javadoc

1   /*
2    * Copyright 2012 akquinet
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *   http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package de.akquinet.innovation.play.maven;
17  
18  import org.apache.commons.exec.CommandLine;
19  import org.apache.commons.exec.DefaultExecutor;
20  import org.apache.commons.exec.ExecuteWatchdog;
21  import org.apache.commons.io.FileUtils;
22  import org.apache.maven.plugin.MojoExecutionException;
23  
24  import java.io.File;
25  import java.io.IOException;
26  
27  /**
28   * Clean the project.
29   *
30   * @goal clean
31   * @phase clean
32   */
33  public class Play2CleanMojo
34          extends AbstractPlay2Mojo {
35  
36      /**
37       * Where are the dependencies copied.
38       *
39       * @parameter default-value="lib"
40       */
41      private File lib;
42  
43      /**
44       * Set to false to avoid to clean the lib folder..
45       *
46       * @parameter default-value="true"
47       */
48      private boolean cleanLibFolder = true;
49  
50      public void execute()
51              throws MojoExecutionException {
52  
53          String line = getPlay2().getAbsolutePath();
54  
55          CommandLine cmdLine = CommandLine.parse(line);
56          cmdLine.addArgument("clean");
57          DefaultExecutor executor = new DefaultExecutor();
58  
59          if (timeout > 0) {
60              ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
61              executor.setWatchdog(watchdog);
62          }
63  
64          executor.setWorkingDirectory(project.getBasedir());
65          executor.setExitValue(0);
66          try {
67              executor.execute(cmdLine, getEnvironment());
68          } catch (IOException e) {
69              throw new MojoExecutionException("Error during cleanup", e);
70          }
71          
72          // Also delete the dist directory
73          File dist = new File(project.getBasedir(), "dist");
74          if (dist.exists()) {
75              getLog().debug("Deleting " + dist.getAbsolutePath());
76              try {
77                  FileUtils.deleteDirectory(dist);
78              } catch (IOException e) {
79                  throw new MojoExecutionException("Can't delete the dist folder", e);
80              }
81          } else {
82              getLog().debug("'dist' directory not found");
83          }
84  
85          // Delete the log folder
86          File logs = new File(project.getBasedir(), "logs");
87          if (logs.exists()) {
88              getLog().debug("Deleting " + logs.getAbsolutePath());
89              try {
90                  FileUtils.deleteDirectory(logs);
91              } catch (IOException e) {
92                  throw new MojoExecutionException("Can't delete the logs folder", e);
93              }
94          } else {
95              getLog().debug("'logs' directory not found");
96          }
97  
98          // Also delete the lib directory if set
99          if (cleanLibFolder) {
100             File lib = new File(project.getBasedir(), "lib");
101             if (lib.exists()) {
102                 getLog().debug("Deleting " + lib.getAbsolutePath());
103                 try {
104                     FileUtils.deleteDirectory(lib);
105                 } catch (IOException e) {
106                     throw new MojoExecutionException("Can't delete the " + lib + " folder", e);
107                 }
108             } else {
109                 getLog().debug("'" + lib + "' directory not found");
110             }
111         }
112     }
113 }