View Javadoc
1   package org.apache.maven.plugin.eclipse.writers.myeclipse;
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  
23  import java.io.File;
24  import java.io.FileWriter;
25  import java.io.IOException;
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.IdeUtils;
32  import org.apache.maven.plugin.ide.JeeDescriptor;
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   * MyEclipse .mymetadata configuration file writer
40   * 
41   * @author Olivier Jacob
42   */
43  public class MyEclipseMetadataWriter
44      extends AbstractEclipseWriter
45  {
46  
47      private static final String MYECLIPSE_MYMETADATA_FILENAME = ".mymetadata";
48  
49      private static final String MYECLIPSE_METADATA_PROJECT = "project-module";
50  
51      private static final String MYECLIPSE_METADATA_PROJECT_TYPE = "type";
52  
53      private static final String MYECLIPSE_METADATA_PROJECT_NAME = "name";
54  
55      private static final String MYECLIPSE_METADATA_PROJECT_ID = "id";
56  
57      private static final String MYECLIPSE_METADATA_PROJECT_CONTEXT_ROOT = "context-root";
58  
59      private static final String MYECLIPSE_METADATA_PROJECT_J2EE_SPEC = "j2ee-spec";
60  
61      private static final String MYECLIPSE_METADATA_PROJECT_ARCHIVE = "archive";
62  
63      private static final String MYECLIPSE_METADATA_PROJECT_TYPE_WAR = "WEB";
64  
65      private static final String MYECLIPSE_METADATA_PROJECT_TYPE_EAR = "EAR";
66  
67      private static final String MYECLIPSE_METADATA_PROJECT_TYPE_EJB = "EJB";
68  
69      private static final String MYECLIPSE_METADATA_PROJECT_ATTRIBUTES = "attributes";
70  
71      private static final String MYECLIPSE_METADATA_PROJECT_ATTRIBUTE = "attribute";
72  
73      /**
74       * Writer entry point
75       * 
76       * @throws MojoExecutionException
77       */
78      public void write()
79          throws MojoExecutionException
80      {
81          String packaging = config.getProject().getPackaging();
82  
83          if ( !Constants.PROJECT_PACKAGING_EAR.equalsIgnoreCase( packaging )
84              && !Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase( packaging )
85              && !Constants.PROJECT_PACKAGING_EJB.equalsIgnoreCase( packaging ) )
86          {
87              return;
88          }
89  
90          FileWriter w;
91  
92          try
93          {
94              w = new FileWriter( new File( config.getEclipseProjectDirectory(), MYECLIPSE_MYMETADATA_FILENAME ) );
95          }
96          catch ( IOException ex )
97          {
98              throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$
99          }
100 
101         XMLWriter writer = new PrettyPrintXMLWriter( w, "UTF-8", null );
102 
103         writer.startElement( MYECLIPSE_METADATA_PROJECT );
104         writer.addAttribute( MYECLIPSE_METADATA_PROJECT_TYPE, getMyEclipseProjectType( packaging ) );
105         writer.addAttribute( MYECLIPSE_METADATA_PROJECT_NAME, config.getEclipseProjectName() );
106         writer.addAttribute( MYECLIPSE_METADATA_PROJECT_ID, config.getEclipseProjectName() );
107 
108         if ( Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase( packaging ) )
109         {
110             // Find web application context root from maven-war-plugin configuration.
111             // ArtifactId is used as the default value
112             String warContextRoot =
113                 IdeUtils.getPluginSetting( config.getProject(), JeeUtils.ARTIFACT_MAVEN_WAR_PLUGIN, "warContextRoot",//$NON-NLS-1$
114                                            "/" + config.getProject().getArtifactId() );
115 
116             writer.addAttribute( MYECLIPSE_METADATA_PROJECT_CONTEXT_ROOT, warContextRoot );
117 
118             writer.addAttribute( MYECLIPSE_METADATA_PROJECT_J2EE_SPEC, getJeeVersion() );
119             // TODO : use maven final name
120             writer.addAttribute( MYECLIPSE_METADATA_PROJECT_ARCHIVE, config.getEclipseProjectName() + ".war" );
121         }
122 
123         if ( Constants.PROJECT_PACKAGING_EAR.equalsIgnoreCase( packaging ) )
124         {
125             // TODO : use maven final name
126             writer.addAttribute( MYECLIPSE_METADATA_PROJECT_ARCHIVE, config.getEclipseProjectName() + ".ear" );
127         }
128 
129         writer.startElement( MYECLIPSE_METADATA_PROJECT_ATTRIBUTES );
130         if ( Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase( packaging ) )
131         {
132             writer.startElement( MYECLIPSE_METADATA_PROJECT_ATTRIBUTE );
133             writer.addAttribute( "name", "webrootdir" );
134             // TODO : retrieve this from project configuration
135             writer.addAttribute( "value", "src/main/webapp" );
136             writer.endElement();
137         }
138         // Close <attributes>
139         writer.endElement();
140 
141         // Close <project-module>
142         writer.endElement();
143 
144         IOUtil.close( w );
145     }
146 
147     /**
148      * @param packaging maven project packaging
149      * @return MyEclipse project type (EAR, WAR, EJB)
150      */
151     private String getMyEclipseProjectType( String packaging )
152     {
153         if ( Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase( packaging ) )
154         {
155             return MYECLIPSE_METADATA_PROJECT_TYPE_WAR;
156         }
157         if ( Constants.PROJECT_PACKAGING_EAR.equalsIgnoreCase( packaging ) )
158         {
159             return MYECLIPSE_METADATA_PROJECT_TYPE_EAR;
160         }
161         if ( Constants.PROJECT_PACKAGING_EJB.equalsIgnoreCase( packaging ) )
162         {
163             return MYECLIPSE_METADATA_PROJECT_TYPE_EJB;
164         }
165         // Should never be reached
166         return null;
167     }
168 
169     /**
170      * Find JEE version from the project dependencies : find version from 'j2ee.jar' artifact or from 'servlet-api'
171      * 
172      * @return the JEE version for the project (1.2, 1.3, 1.4, 1.5)
173      * @see org.apache.maven.plugin.ide.JeeUtils#resolveJeeVersion(org.apache.maven.project.MavenProject)
174      */
175     private String getJeeVersion()
176     {
177         String jeeVersion;
178         if ( config.getJeeVersion() != null )
179         {
180             jeeVersion = JeeUtils.getJeeDescriptorFromJeeVersion( config.getJeeVersion() ).getJeeVersion();
181         }
182         else
183         {
184             jeeVersion =
185                 JeeUtils.getJeeDescriptorFromServletVersion( JeeUtils.resolveServletVersion( config.getProject() ) ).getJeeVersion();
186         }
187 
188         if ( jeeVersion == null )
189         {
190             return JeeDescriptor.JEE_1_4;
191         }
192 
193         return jeeVersion;
194     }
195 }