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.writers.rad;
20  
21  import java.io.File;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.io.OutputStreamWriter;
25  import java.io.Writer;
26  
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugin.eclipse.Constants;
29  import org.apache.maven.plugin.eclipse.Messages;
30  import org.apache.maven.plugin.eclipse.writers.AbstractEclipseWriter;
31  import org.apache.maven.plugin.ide.IdeDependency;
32  import org.apache.maven.plugin.ide.IdeUtils;
33  import org.apache.maven.plugin.ide.JeeUtils;
34  import org.codehaus.plexus.util.IOUtil;
35  import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
36  import org.codehaus.plexus.util.xml.XMLWriter;
37  
38  /**
39   * Creates a .settings folder for Eclipse WTP 1.x release and writes out the configuration under it.
40   * 
41   * @author <a href="mailto:nir@cfc.at">Richard van Nieuwenhoven </a>
42   */
43  public class RadWebSettingsWriter
44      extends AbstractEclipseWriter
45  {
46  
47      private static final String COM_IBM_ETOOLS_SITEEDIT_WIZARDS_PROJECTFEATURE_WEB_SITE_FEATURE =
48          "com.ibm.etools.siteedit.wizards.projectfeature.WebSiteFeature";
49  
50      private static final String WEBSETTINGS_CONTEXT_ROOT = "context-root";
51  
52      private static final String WEBSETTINGS_FEATURE = "feature";
53  
54      private static final String WEBSETTINGS_FEATURE_ID = "feature-id";
55  
56      private static final String WEBSETTINGS_FEATURES = "features";
57  
58      private static final String WEBSETTINGS_FILENAME = ".websettings";
59  
60      private static final String WEBSETTINGS_JSP_LEVEL = "jsp-level";
61  
62      private static final String WEBSETTINGS_PROJECT_TYPE = "project-type";
63  
64      private static final String WEBSETTINGS_TEMPLATEFEATURE = "templatefeature";
65  
66      private static final String WEBSETTINGS_VERSION = "version";
67  
68      private static final String WEBSETTINGS_WEBCONTENT = "webcontent";
69  
70      private static final String WEBSETTINGS_WEBSETTINGS = "websettings";
71  
72      private static final String WEBSETTINGS_LIBMODULES = "lib-modules";
73  
74      private static final String WEBSETTINGS_LIBMODULE = "lib-module";
75  
76      private static final String WEBSETTINGS_LM_JAR = "jar";
77  
78      private static final String WEBSETTINGS_LM_PROJECT = "project";
79  
80      /**
81       * the context root to use for this project
82       */
83      private String warContextRoot;
84  
85      /**
86       * required default constructor.
87       * 
88       * @param warContextRoot the context root to use for this project
89       */
90      public RadWebSettingsWriter( String warContextRoot )
91      {
92          this.warContextRoot = warContextRoot;
93      }
94  
95      /**
96       * write the websettings file for RAD6 if needed.
97       * 
98       * @throws MojoExecutionException when writing the config files was not possible
99       */
100     public void write()
101         throws MojoExecutionException
102     {
103         Writer w;
104         if ( Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase( config.getPackaging() ) )
105         {
106             try
107             {
108                 w =
109                     new OutputStreamWriter( new FileOutputStream( new File( config.getEclipseProjectDirectory(),
110                                                                             WEBSETTINGS_FILENAME ) ), "UTF-8" );
111             }
112             catch ( IOException ex )
113             {
114                 throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$
115             }
116 
117             XMLWriter writer = new PrettyPrintXMLWriter( w, "UTF-8", null );
118             writeModuleTypeFacetCore( writer );
119             IOUtil.close( w );
120         }
121     }
122 
123     /**
124      * write the websettings file for RAD6.
125      * 
126      * @param writer where to write to
127      * @throws MojoExecutionException
128      */
129     private void writeModuleTypeFacetCore( XMLWriter writer )
130         throws MojoExecutionException
131     {
132         writer.startElement( WEBSETTINGS_WEBSETTINGS );
133         writer.addAttribute( WEBSETTINGS_VERSION, "600" );
134         writer.startElement( WEBSETTINGS_WEBCONTENT );
135 
136         // Generating web content settings based on war plug-in warSourceDirectory property
137         File warSourceDirectory =
138             new File( IdeUtils.getPluginSetting( config.getProject(), JeeUtils.ARTIFACT_MAVEN_WAR_PLUGIN,
139                                                  "warSourceDirectory", //$NON-NLS-1$
140                                                  config.getProject().getBasedir() + "/src/main/webapp" ) ); //$NON-NLS-1$
141         String webContentDir =
142             IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(), warSourceDirectory, false );
143 
144         writer.writeText( webContentDir );
145 
146         writer.endElement();
147         writer.startElement( WEBSETTINGS_CONTEXT_ROOT );
148         writer.writeText( getContextRoot( warContextRoot ) );
149         writer.endElement();
150         writer.startElement( WEBSETTINGS_PROJECT_TYPE );
151         writer.writeText( "J2EE" );
152         writer.endElement();
153         writer.startElement( WEBSETTINGS_JSP_LEVEL );
154         writer.writeText( JeeUtils.resolveJspVersion( config.getProject() ) );
155         writer.endElement();
156         writer.startElement( WEBSETTINGS_FEATURES );
157         writer.startElement( WEBSETTINGS_FEATURE );
158         writer.startElement( WEBSETTINGS_FEATURE_ID );
159         writer.writeText( WEBSETTINGS_TEMPLATEFEATURE );
160         writer.endElement();
161         writer.endElement();
162         writer.startElement( WEBSETTINGS_FEATURE );
163         writer.startElement( WEBSETTINGS_FEATURE_ID );
164         writer.writeText( COM_IBM_ETOOLS_SITEEDIT_WIZARDS_PROJECTFEATURE_WEB_SITE_FEATURE );
165         writer.endElement();
166         writer.endElement();
167         writer.endElement();
168 
169         // library modules
170         writer.startElement( WEBSETTINGS_LIBMODULES );
171 
172         // iterate relevant dependencies (non-test, non-provided, project)
173         IdeDependency[] deps = config.getDepsOrdered();
174         if ( deps != null )
175         {
176             for ( int i = 0; i < deps.length; i++ )
177             {
178                 final IdeDependency dependency = deps[i];
179                 log.debug( "RadWebSettingsWriter: checking dependency " + dependency.toString() );
180 
181                 if ( dependency.isReferencedProject() && !dependency.isTestDependency() && !dependency.isProvided() )
182                 {
183                     log.debug( "RadWebSettingsWriter: dependency " + dependency.toString()
184                         + " selected for inclusion as lib-module" );
185 
186                     String depName = dependency.getEclipseProjectName();
187                     String depJar = dependency.getArtifactId() + ".jar";
188 
189                     writer.startElement( WEBSETTINGS_LIBMODULE );
190 
191                     writer.startElement( WEBSETTINGS_LM_JAR );
192                     writer.writeText( depJar );
193                     writer.endElement(); // jar
194 
195                     writer.startElement( WEBSETTINGS_LM_PROJECT );
196                     writer.writeText( depName );
197                     writer.endElement(); // project
198 
199                     writer.endElement(); // libmodule
200                 }
201             }
202         }
203 
204         writer.endElement(); // libmodules
205         writer.endElement(); // websettings
206 
207     }
208 
209     /**
210      * Create the ContextRoot for this project, the default is the artifact id
211      * 
212      * @param warContextRoot set as a configuration property.
213      * @return the context root to use
214      */
215     private String getContextRoot( String warContextRoot )
216     {
217         if ( warContextRoot == null || warContextRoot.length() == 0 )
218         {
219             return config.getProject().getArtifactId();
220         }
221         else
222         {
223             return warContextRoot;
224         }
225     }
226 
227 }