View Javadoc

1   package org.codehaus.mojo.pde;
2   
3   /*
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.File;
20  import java.io.FileInputStream;
21  import java.io.IOException;
22  import java.util.Properties;
23  
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.MojoFailureException;
26  import org.codehaus.mojo.pde.descriptor.DescriptorUtil;
27  import org.codehaus.plexus.util.FileUtils;
28  import org.codehaus.plexus.util.cli.Commandline;
29  
30  /**
31   * Clean Eclipse PDE generated files. Supports cleaning of features and products only.
32   * 
33   * TODO: Add support for cleaning of fragments, plugins.
34   * 
35   * @goal clean
36   * @phase clean
37   * @requiresProject false
38   * @version $Id:$ *
39   * @author dtran@gmail.com
40   */
41  
42  public class EclipsePDECleanMojo
43      extends AbstractEclipsePDEMojo
44  {
45  
46      /**
47       * {@inheritDoc}
48       */
49      public void execute()
50          throws MojoExecutionException, MojoFailureException
51      {
52          File buildXml = new File( this.pdeDirectory, "build.xml" );
53  
54          if ( buildXml.exists() )
55          {
56              super.execute();
57  
58              try
59              {
60                  clean();
61              }
62              catch ( IOException e )
63              {
64                  throw new MojoExecutionException( "Failed to clean", e );
65              }
66  
67              clean( buildXml );
68  
69              // remove build.xml if it is a generated one
70              File buildPropertiesFile = new File( this.pdeDirectory, "build.properties" );
71              if ( buildPropertiesFile.exists() )
72              {
73                  Properties properties = new Properties();
74                  try
75                  {
76                      properties.load( new FileInputStream( buildPropertiesFile ) );
77                  }
78                  catch ( IOException e )
79                  {
80                      throw new MojoExecutionException( "Error loading: " + buildPropertiesFile );
81                  }
82                  if ( properties.getProperty( "custom", "false" ).equals( "false" ) )
83                  {
84                      buildXml.delete();
85                  }
86              }
87          }
88      }
89  
90      /**
91       * Clean the pde project artifacts that an ant clean does not.
92       * 
93       * @throws MojoExecutionException build failures.
94       * @throws IOException build failures.
95       * 
96       */
97      private void clean()
98          throws MojoExecutionException, IOException
99      {
100         String id = this.descriptor.getId();
101         File pdeBuildDirectory = this.getPDEBuildDirectory();
102 
103         if ( "feature".equals( DescriptorUtil.getPDEType( descriptor ) ) )
104         {
105             File file = new File( pdeBuildDirectory, "package." + id + ".all.xml" );
106             file.delete();
107 
108             file = new File( pdeBuildDirectory, "package." + id + ".xml" );
109             file.delete();
110 
111             file = new File( pdeBuildDirectory, "assemble." + id + ".all.xml" );
112             file.delete();
113 
114             file = new File( pdeBuildDirectory, "assemble." + id + ".xml" );
115             file.delete();
116         }
117         else if ( "product".equals( DescriptorUtil.getPDEType( descriptor ) ) )
118         {
119             String buildLabel = buildConfigurationProperties.getString( "buildLabel" );
120             String config = convertPdeConfigsToFilenameSuffix( buildConfigurationProperties.getString( "configs" ) );
121             String configWithUnderscores = config.replace( '.', '_' );
122 
123             File file = new File( pdeBuildDirectory, "assemble.org.eclipse.pde.build.container.feature.all.xml" );
124             file.delete();
125 
126             file = new File( pdeBuildDirectory, "assemble.org.eclipse.pde.build.container.feature." + config + ".xml" );
127             file.delete();
128 
129             file = new File( pdeBuildDirectory, "finalFeaturesVersions.properties" );
130             file.delete();
131 
132             file = new File( pdeBuildDirectory, "finalFeaturesVersions." + configWithUnderscores + ".properties" );
133             file.delete();
134 
135             file = new File( pdeBuildDirectory, "finalPluginsVersions.properties" );
136             file.delete();
137 
138             file = new File( pdeBuildDirectory, "finalPluginsVersions." + configWithUnderscores + ".properties" );
139             file.delete();
140 
141             file = new File( pdeBuildDirectory, "package.org.eclipse.pde.build.container.feature.all.xml" );
142             file.delete();
143 
144             file = new File( pdeBuildDirectory, "package.org.eclipse.pde.build.container.feature." + config + ".xml" );
145             file.delete();
146 
147             file = new File( pdeBuildDirectory, buildLabel );
148             FileUtils.deleteDirectory( file );
149             
150             file = new File( pdeDirectory, "temp.folder" );
151             FileUtils.deleteDirectory( file );
152 
153             file = new File( pdeDirectory, "javaCompiler...args" );
154             file.delete();            
155         }
156     }
157 
158     /**
159      * Run the clean target for the specified ant file
160      * 
161      * @param antFile the build file to use
162      * @throws MojoExecutionException build failures.
163      */
164     private void clean( File antFile )
165         throws MojoExecutionException
166     {
167         Commandline cl = this.createCommandLine( antFile, "clean" );
168         this.executeCommandLine( cl );
169     }
170 
171 }