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.project.MavenProject;
27 import org.codehaus.plexus.util.FileUtils;
28
29 /**
30 * Deletes the config files used by Rad-6. the files .j2ee and the file .websettings
31 *
32 * @author <a href="mailto:nir@cfc.at">Richard van Nieuwenhoven</a>
33 * @goal rad-clean
34 */
35 public class RadCleanMojo
36 extends EclipseCleanMojo
37 {
38 /**
39 * The project whose project files to clean.
40 *
41 * @parameter expression="${project}"
42 * @required
43 * @readonly
44 */
45 private MavenProject project;
46
47 protected void cleanExtras()
48 throws MojoExecutionException
49 {
50 delete( new File( getBasedir(), ".j2ee" ) );
51 delete( new File( getBasedir(), ".websettings" ) );
52 delete( new File( getBasedir(), ".website-config" ) );
53
54 handleLibs();
55 }
56
57 /**
58 * getter for the instancevarriable project.
59 *
60 * @return the maven project decriptor
61 */
62 public MavenProject getProject()
63 {
64 return this.project;
65 }
66
67 /**
68 * getter for the instancevarriable project.
69 *
70 * @param project the maven project decriptor
71 */
72 public void setProject( MavenProject project )
73 {
74 this.project = project;
75 }
76
77 /**
78 * Delete all jars in the EAR project root directory.
79 *
80 * @throws MojoExecutionException only if a file exists and can't be deleted
81 */
82 private void handleEarLibs()
83 throws MojoExecutionException
84 {
85 File targetDir = this.project.getBasedir();
86 deleteJarArtifactsInDirectory( targetDir );
87 deleteWarArtifactsInDirectory( targetDir );
88 }
89
90 /**
91 * Delete all jars in the project that were required by rad6.
92 *
93 * @throws MojoExecutionException only if a file exists and can't be deleted
94 */
95 private void handleLibs()
96 throws MojoExecutionException
97 {
98
99 if ( Constants.PROJECT_PACKAGING_EAR.equals( getPackaging() ) )
100 {
101 handleEarLibs();
102 }
103 else if ( Constants.PROJECT_PACKAGING_WAR.equals( getPackaging() ) )
104 {
105 handleWarLibs();
106 }
107 }
108
109 /**
110 * Delete all jars in the WAR project WEB-INF/lib directory.
111 *
112 * @throws MojoExecutionException only if a file exists and can't be deleted
113 */
114 private void handleWarLibs()
115 throws MojoExecutionException
116 {
117 File basedir = this.project.getBasedir();
118
119 File warSourceDirectory =
120 new File( IdeUtils.getPluginSetting( this.project, JeeUtils.ARTIFACT_MAVEN_WAR_PLUGIN,
121 "warSourceDirectory", //$NON-NLS-1$
122 "src/main/webapp" ) ); //$NON-NLS-1$
123
124 String webContentDir = IdeUtils.toRelativeAndFixSeparator( basedir, warSourceDirectory, false );
125
126 String srcMainWebappWebInfLibDirname =
127 basedir.getAbsolutePath() + File.separatorChar + webContentDir + File.separatorChar + "WEB-INF"
128 + File.separatorChar + "lib";
129
130 File srcMainWebappWebInfLibDir = new File( srcMainWebappWebInfLibDirname );
131 srcMainWebappWebInfLibDir.mkdirs();
132
133 deleteJarArtifactsInDirectory( srcMainWebappWebInfLibDir );
134 }
135
136 /**
137 * delete all Jar artifacts in the specified directory.
138 *
139 * @param directory to delete the jars from
140 * @throws MojoExecutionException only if a file exists and can't be deleted
141 */
142 protected void deleteJarArtifactsInDirectory( File directory )
143 throws MojoExecutionException
144 {
145 deleteArtifactsInDirectory( directory, Constants.PROJECT_PACKAGING_JAR );
146 }
147
148 /**
149 * delete all War artifacts in the specified directory (cleaning up EAR's for example).
150 *
151 * @param directory to delete the wars from
152 * @throws MojoExecutionException only if a file exists and can't be deleted
153 */
154 protected void deleteWarArtifactsInDirectory( File directory )
155 throws MojoExecutionException
156 {
157 deleteArtifactsInDirectory( directory, Constants.PROJECT_PACKAGING_WAR );
158 }
159
160 /**
161 * Deletes all artifacts of specified packaging type in the specified directory
162 *
163 * @param directory - to delete the jars from
164 * @param packagingType - packaging type (file extensions in fact - can be dangerous)
165 * @see Constants#PROJECT_PACKAGING_JAR
166 * @see Constants#PROJECT_PACKAGING_WAR
167 * @throws MojoExecutionException if a file exists and can't be deleted
168 */
169 private void deleteArtifactsInDirectory( File directory, String packagingType )
170 throws MojoExecutionException
171 {
172
173 // sanity check, only support cleanup of 2 types - jar and war
174 if ( Constants.PROJECT_PACKAGING_JAR.equalsIgnoreCase( packagingType )
175 || Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase( packagingType ) )
176 {
177 String[] oldFiles =
178 FileUtils.getFilesFromExtension( directory.getAbsolutePath(), new String[] { packagingType } );
179 for ( int index = 0; index < oldFiles.length; index++ )
180 {
181 File f = new File( oldFiles[index] );
182
183 delete( f );
184 }
185 }
186 }
187 }