View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin.eclipse;
20  
21  import java.io.File;
22  
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.plugin.ide.IdeUtils;
25  import org.apache.maven.plugin.ide.JeeUtils;
26  import org.apache.maven.plugins.annotations.Mojo;
27  import org.apache.maven.plugins.annotations.Parameter;
28  import org.apache.maven.project.MavenProject;
29  import org.codehaus.plexus.util.FileUtils;
30  
31  /**
32   * Deletes the config files used by Rad-6. the files .j2ee and the file .websettings
33   *
34   * @author <a href="mailto:nir@cfc.at">Richard van Nieuwenhoven</a>
35   */
36  @Mojo( name = "rad-clean" )
37  public class RadCleanMojo
38      extends EclipseCleanMojo
39  {
40      /**
41       * The project whose project files to clean.
42       */
43      @Parameter( property = "project", required = true, readonly = true )
44      private MavenProject project;
45  
46      protected void cleanExtras()
47          throws MojoExecutionException
48      {
49          delete( new File( getBasedir(), ".j2ee" ) );
50          delete( new File( getBasedir(), ".websettings" ) );
51          delete( new File( getBasedir(), ".website-config" ) );
52  
53          handleLibs();
54      }
55  
56      /**
57       * getter for the instancevarriable project.
58       * 
59       * @return the maven project decriptor
60       */
61      public MavenProject getProject()
62      {
63          return this.project;
64      }
65  
66      /**
67       * getter for the instancevarriable project.
68       * 
69       * @param project the maven project decriptor
70       */
71      public void setProject( MavenProject project )
72      {
73          this.project = project;
74      }
75  
76      /**
77       * Delete all jars in the EAR project root directory.
78       * 
79       * @throws MojoExecutionException only if a file exists and can't be deleted
80       */
81      private void handleEarLibs()
82          throws MojoExecutionException
83      {
84          File targetDir = this.project.getBasedir();
85          deleteJarArtifactsInDirectory( targetDir );
86          deleteWarArtifactsInDirectory( targetDir );
87      }
88  
89      /**
90       * Delete all jars in the project that were required by rad6.
91       * 
92       * @throws MojoExecutionException only if a file exists and can't be deleted
93       */
94      private void handleLibs()
95          throws MojoExecutionException
96      {
97  
98          if ( Constants.PROJECT_PACKAGING_EAR.equals( getPackaging() ) )
99          {
100             handleEarLibs();
101         }
102         else if ( Constants.PROJECT_PACKAGING_WAR.equals( getPackaging() ) )
103         {
104             handleWarLibs();
105         }
106     }
107 
108     /**
109      * Delete all jars in the WAR project WEB-INF/lib directory.
110      * 
111      * @throws MojoExecutionException only if a file exists and can't be deleted
112      */
113     private void handleWarLibs()
114         throws MojoExecutionException
115     {
116         File basedir = this.project.getBasedir();
117 
118         File warSourceDirectory =
119             new File( IdeUtils.getPluginSetting( this.project, JeeUtils.ARTIFACT_MAVEN_WAR_PLUGIN,
120                                                  "warSourceDirectory", //$NON-NLS-1$
121                                                  "src/main/webapp" ) ); //$NON-NLS-1$
122 
123         String webContentDir = IdeUtils.toRelativeAndFixSeparator( basedir, warSourceDirectory, false );
124 
125         String srcMainWebappWebInfLibDirname =
126             basedir.getAbsolutePath() + File.separatorChar + webContentDir + File.separatorChar + "WEB-INF"
127                 + File.separatorChar + "lib";
128 
129         File srcMainWebappWebInfLibDir = new File( srcMainWebappWebInfLibDirname );
130         srcMainWebappWebInfLibDir.mkdirs();
131 
132         deleteJarArtifactsInDirectory( srcMainWebappWebInfLibDir );
133     }
134 
135     /**
136      * delete all Jar artifacts in the specified directory.
137      * 
138      * @param directory to delete the jars from
139      * @throws MojoExecutionException only if a file exists and can't be deleted
140      */
141     protected void deleteJarArtifactsInDirectory( File directory )
142         throws MojoExecutionException
143     {
144         deleteArtifactsInDirectory( directory, Constants.PROJECT_PACKAGING_JAR );
145     }
146 
147     /**
148      * delete all War artifacts in the specified directory (cleaning up EAR's for example).
149      * 
150      * @param directory to delete the wars from
151      * @throws MojoExecutionException only if a file exists and can't be deleted
152      */
153     protected void deleteWarArtifactsInDirectory( File directory )
154         throws MojoExecutionException
155     {
156         deleteArtifactsInDirectory( directory, Constants.PROJECT_PACKAGING_WAR );
157     }
158 
159     /**
160      * Deletes all artifacts of specified packaging type in the specified directory
161      * 
162      * @param directory - to delete the jars from
163      * @param packagingType - packaging type (file extensions in fact - can be dangerous)
164      * @see Constants#PROJECT_PACKAGING_JAR
165      * @see Constants#PROJECT_PACKAGING_WAR
166      * @throws MojoExecutionException if a file exists and can't be deleted
167      */
168     private void deleteArtifactsInDirectory( File directory, String packagingType )
169         throws MojoExecutionException
170     {
171 
172         // sanity check, only support cleanup of 2 types - jar and war
173         if ( Constants.PROJECT_PACKAGING_JAR.equalsIgnoreCase( packagingType )
174             || Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase( packagingType ) )
175         {
176             String[] oldFiles =
177                 FileUtils.getFilesFromExtension( directory.getAbsolutePath(), new String[] { packagingType } );
178             for (String oldFile : oldFiles) {
179                 File f = new File(oldFile);
180 
181                 delete(f);
182             }
183         }
184     }
185 }