View Javadoc
1   package org.apache.maven.plugin.ant;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  
24  import org.apache.maven.plugin.AbstractMojo;
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugins.annotations.Mojo;
27  import org.apache.maven.plugins.annotations.Parameter;
28  import org.apache.maven.project.MavenProject;
29  
30  /**
31   * Clean all Ant build files.
32   *
33   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
34   * @version $Id: AntCleanMojo.java 1640228 2014-11-17 21:20:42Z hboutemy $
35   */
36  @Mojo( name = "clean" )
37  public class AntCleanMojo
38      extends AbstractMojo
39  {
40      // ----------------------------------------------------------------------
41      // Mojo components
42      // ----------------------------------------------------------------------
43  
44      // ----------------------------------------------------------------------
45      // Mojo parameters
46      // ----------------------------------------------------------------------
47  
48      /**
49       * The working project.
50       */
51      @Parameter( defaultValue = "${project}", readonly = true, required = true )
52      private MavenProject project;
53  
54      /**
55       * Forcing the deletion of the custom <code>build.xml</code>.
56       *
57       * @since 2.2
58       */
59      @Parameter( property = "deleteCustomFiles", defaultValue = "false" )
60      private boolean deleteCustomFiles;
61  
62      /**
63       * {@inheritDoc}
64       */
65      public void execute()
66          throws MojoExecutionException
67      {
68          File buildXml = new File( project.getBasedir(), AntBuildWriter.DEFAULT_BUILD_FILENAME );
69          if ( buildXml.exists() )
70          {
71              if ( deleteCustomFiles )
72              {
73                  if ( !buildXml.delete() )
74                  {
75                      throw new MojoExecutionException( "Cannot delete " + buildXml.getAbsolutePath() );
76                  }
77              }
78              else if ( getLog().isInfoEnabled() )
79              {
80                  getLog().info( "Not deleting custom " + buildXml.getName()
81                                     + ", use -DdeleteCustomFiles=true to force its deletion" );
82              }
83          }
84  
85          File mavenBuildXml = new File( project.getBasedir(), AntBuildWriter.DEFAULT_MAVEN_BUILD_FILENAME );
86          if ( mavenBuildXml.exists() && !mavenBuildXml.delete() )
87          {
88              throw new MojoExecutionException( "Cannot delete " + mavenBuildXml.getAbsolutePath() );
89          }
90  
91          File mavenBuildProperties = new File( project.getBasedir(), AntBuildWriter.DEFAULT_MAVEN_PROPERTIES_FILENAME );
92          if ( mavenBuildProperties.exists() && !mavenBuildProperties.delete() )
93          {
94              throw new MojoExecutionException( "Cannot delete " + mavenBuildProperties.getAbsolutePath() );
95          }
96  
97          getLog().info( "Deleted Ant build files for project " + project.getArtifactId() + " in "
98                             + project.getBasedir().getAbsolutePath() );
99      }
100 
101 }