View Javadoc
1   package org.apache.maven.plugin.eclipse.writers.wtp;
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  import java.io.FileOutputStream;
24  import java.io.IOException;
25  import java.io.OutputStreamWriter;
26  import java.io.Writer;
27  
28  import org.apache.maven.artifact.repository.ArtifactRepository;
29  import org.apache.maven.plugin.MojoExecutionException;
30  import org.apache.maven.plugin.eclipse.Constants;
31  import org.apache.maven.plugin.eclipse.EclipseSourceDir;
32  import org.apache.maven.plugin.eclipse.Messages;
33  import org.apache.maven.plugin.ide.IdeUtils;
34  import org.apache.maven.plugin.ide.JeeUtils;
35  import org.codehaus.plexus.util.IOUtil;
36  import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
37  import org.codehaus.plexus.util.xml.XMLWriter;
38  import org.codehaus.plexus.util.xml.Xpp3Dom;
39  
40  /**
41   * Creates a .settings folder for Eclipse WTP 1.x release and writes out the configuration under it.
42   * 
43   * @author <a href="mailto:rahul.thakur.xdev@gmail.com">Rahul Thakur</a>
44   * @author <a href="mailto:fgiust@apache.org">Fabrizio Giustina</a>
45   * @version $Id$
46   */
47  public class EclipseWtpComponentWriter
48      extends AbstractWtpResourceWriter
49  {
50  
51      /**
52       * Context root attribute.
53       */
54      public static final String ATTR_CONTEXT_ROOT = "context-root"; //$NON-NLS-1$
55  
56      /**
57       * The .settings folder for Web Tools Project 1.x release.
58       */
59      public static final String DIR_WTP_SETTINGS = ".settings"; //$NON-NLS-1$
60  
61      /**
62       * File name where the WTP component settings will be stored for our Eclipse Project.
63       * 
64       * @return <code>.component</code>
65       */
66      protected String getComponentFileName()
67      {
68          return ".component"; //$NON-NLS-1$
69      }
70  
71      /**
72       * Version number added to component configuration.
73       * 
74       * @return <code>1.0</code>
75       */
76      protected String getProjectVersion()
77      {
78          return null;
79      }
80  
81      /**
82       * @see org.apache.maven.plugin.eclipse.writers.EclipseWriter#write()
83       */
84      public void write()
85          throws MojoExecutionException
86      {
87  
88          // create a .settings directory (if not existing)
89          File settingsDir = new File( config.getEclipseProjectDirectory(), DIR_WTP_SETTINGS );
90          settingsDir.mkdirs();
91  
92          Writer w;
93          try
94          {
95              w =
96                  new OutputStreamWriter( new FileOutputStream( new File( settingsDir, getComponentFileName() ) ),
97                                          "UTF-8" );
98          }
99          catch ( IOException ex )
100         {
101             throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex );
102         }
103 
104         // create a .component file and write out to it
105         XMLWriter writer = new PrettyPrintXMLWriter( w, "UTF-8", null );
106 
107         writeModuleTypeComponent( writer, config.getPackaging(), config.getBuildOutputDirectory(),
108                                   config.getSourceDirs(), config.getLocalRepository() );
109 
110         IOUtil.close( w );
111     }
112 
113     /**
114      * Writes out the module type settings for a Web Tools Project to a component file.
115      * 
116      * @param writer
117      * @param packaging
118      * @param buildOutputDirectory
119      * @param sourceDirs
120      * @param localRepository
121      * @throws MojoExecutionException
122      */
123     private void writeModuleTypeComponent( XMLWriter writer, String packaging, File buildOutputDirectory,
124                                            EclipseSourceDir[] sourceDirs, ArtifactRepository localRepository )
125         throws MojoExecutionException
126     {
127         writer.startElement( ELT_PROJECT_MODULES );
128         writer.addAttribute( ATTR_MODULE_ID, "moduleCoreId" ); //$NON-NLS-1$
129         if ( getProjectVersion() != null )
130         {
131             writer.addAttribute( ATTR_PROJECT_VERSION, getProjectVersion() );
132         }
133         writer.startElement( ELT_WB_MODULE );
134 
135         // we should use the eclipse project name as the deploy name.
136         writer.addAttribute( ATTR_DEPLOY_NAME, this.config.getEclipseProjectName() );
137 
138         // deploy-path is "/" for utility and ejb projects, "/WEB-INF/classes" for webapps
139         String target = "/"; //$NON-NLS-1$
140 
141         if ( Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase( packaging ) ) //$NON-NLS-1$
142         {
143             target = "/WEB-INF/classes"; //$NON-NLS-1$
144 
145             File warSourceDirectory =
146                 new File( IdeUtils.getPluginSetting( config.getProject(), JeeUtils.ARTIFACT_MAVEN_WAR_PLUGIN,
147                                                      "warSourceDirectory", //$NON-NLS-1$
148                                                      config.getProject().getBasedir() + "/src/main/webapp" ) );
149 
150             writeContextRoot( writer );
151 
152             writer.startElement( ELT_WB_RESOURCE );
153             writer.addAttribute( ATTR_DEPLOY_PATH, "/" ); //$NON-NLS-1$
154             writer.addAttribute( ATTR_SOURCE_PATH,
155                                  IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(),
156                                                                      warSourceDirectory, false ) );
157             writer.endElement();
158 
159             // add web resources over the top of the war source directory
160             Xpp3Dom[] webResources =
161                 IdeUtils.getPluginConfigurationDom( config.getProject(), JeeUtils.ARTIFACT_MAVEN_WAR_PLUGIN,
162                                                     new String[] { "webResources", "resource" } );
163             for ( Xpp3Dom webResource : webResources )
164             {
165                 File webResourceDirectory = new File( webResource.getChild( "directory" ).getValue() );
166                 writer.startElement( ELT_WB_RESOURCE );
167                 writer.addAttribute( ATTR_DEPLOY_PATH, "/" ); //$NON-NLS-1$
168                 writer.addAttribute( ATTR_SOURCE_PATH,
169                                      IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(),
170                                                                          webResourceDirectory, false ) );
171                 writer.endElement();
172             }
173 
174             // @todo is this really needed?
175             writer.startElement( ELT_PROPERTY );
176             writer.addAttribute( ATTR_NAME, "java-output-path" ); //$NON-NLS-1$
177             writer.addAttribute( ATTR_VALUE, "/" //$NON-NLS-1$
178                 + IdeUtils.toRelativeAndFixSeparator( config.getProject().getBasedir(), buildOutputDirectory, false ) );
179             writer.endElement(); // property
180 
181         }
182         else if ( Constants.PROJECT_PACKAGING_EAR.equalsIgnoreCase( packaging ) ) //$NON-NLS-1$
183         {
184 
185             String defaultApplicationXML =
186                 config.getWtpapplicationxml() ? "/target/eclipseEar" : "/src/main/application";
187 
188             String earSourceDirectory =
189                 IdeUtils.getPluginSetting( config.getProject(), JeeUtils.ARTIFACT_MAVEN_EAR_PLUGIN,
190                                            "earSourceDirectory", //$NON-NLS-1$
191                                            config.getProject().getBasedir() + defaultApplicationXML );
192             writer.startElement( ELT_WB_RESOURCE );
193             writer.addAttribute( ATTR_DEPLOY_PATH, "/" ); //$NON-NLS-1$
194             writer.addAttribute( ATTR_SOURCE_PATH,
195                                  IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(),
196                                                                      new File( earSourceDirectory ), false ) );
197             writer.endElement();
198         }
199 
200         if ( Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase( packaging )
201             || Constants.PROJECT_PACKAGING_EAR.equalsIgnoreCase( packaging ) )
202         {
203             // write out the dependencies.
204             writeWarOrEarResources( writer, config.getProject(), localRepository );
205 
206         }
207 
208         for ( EclipseSourceDir dir : sourceDirs )
209         {
210             // test src/resources are not added to wtpmodules
211             if ( !dir.isTest() )
212             {
213                 // <wb-resource deploy-path="/" source-path="/src/java" />
214                 writer.startElement( ELT_WB_RESOURCE );
215                 writer.addAttribute( ATTR_DEPLOY_PATH, target );
216                 writer.addAttribute( ATTR_SOURCE_PATH, dir.getPath() );
217                 writer.endElement();
218             }
219         }
220 
221         writer.endElement(); // wb-module
222         writer.endElement(); // project-modules
223     }
224 
225     /**
226      * @param writer
227      */
228     protected void writeContextRoot( XMLWriter writer )
229     {
230         writer.startElement( ELT_PROPERTY );
231         writer.addAttribute( ATTR_CONTEXT_ROOT, config.getContextName() );
232         writer.endElement(); // property
233     }
234 
235 }