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.wtp;
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.EclipseSourceDir;
30  import org.apache.maven.plugin.eclipse.Messages;
31  import org.apache.maven.plugin.ide.IdeUtils;
32  import org.apache.maven.plugin.ide.JeeUtils;
33  import org.codehaus.plexus.util.IOUtil;
34  import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
35  import org.codehaus.plexus.util.xml.XMLWriter;
36  
37  /**
38   * Writes eclipse .wtpmodules file.
39   * 
40   * @author <a href="mailto:fgiust@users.sourceforge.net">Fabrizio Giustina</a>
41   * @version $Id: EclipseWtpmodulesWriter.java 595639 2007-11-16 12:16:25Z aheritier $
42   */
43  public class EclipseWtpmodulesWriter
44      extends AbstractWtpResourceWriter
45  {
46  
47      protected static final String FILE_DOT_WTPMODULES = ".wtpmodules"; //$NON-NLS-1$
48  
49      /**
50       * @see org.apache.maven.plugin.eclipse.writers.EclipseWriter#write()
51       */
52      public void write()
53          throws MojoExecutionException
54      {
55          Writer w;
56  
57          try
58          {
59              w =
60                  new OutputStreamWriter( new FileOutputStream( new File( config.getEclipseProjectDirectory(),
61                                                                          FILE_DOT_WTPMODULES ) ), "UTF-8" );
62          }
63          catch ( IOException ex )
64          {
65              throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$
66          }
67  
68          XMLWriter writer = new PrettyPrintXMLWriter( w );
69          writer.startElement( ELT_PROJECT_MODULES );
70          writer.addAttribute( ATTR_MODULE_ID, "moduleCoreId" ); //$NON-NLS-1$
71  
72          writer.startElement( ELT_WB_MODULE );
73          // we should use the configured eclipse project name.
74          writer.addAttribute( ATTR_DEPLOY_NAME, this.config.getEclipseProjectName() );
75  
76          writer.startElement( ELT_MODULE_TYPE );
77          writeModuleTypeAccordingToPackaging( config.getProject(), writer, config.getBuildOutputDirectory() );
78          writer.endElement(); // module-type
79  
80          // source and resource paths.
81          // deploy-path is "/" for utility and ejb projects, "/WEB-INF/classes" for webapps
82  
83          String target = "/"; //$NON-NLS-1$
84          if ( Constants.PROJECT_PACKAGING_WAR.equals( config.getPackaging() ) ) //$NON-NLS-1$
85          {
86              String warSourceDirectory =
87                  IdeUtils.getPluginSetting( config.getProject(), JeeUtils.ARTIFACT_MAVEN_WAR_PLUGIN,
88                                             "warSourceDirectory", //$NON-NLS-1$
89                                             config.getProject().getBasedir() + "/src/main/webapp" ); //$NON-NLS-1$
90  
91              writer.startElement( ELT_WB_RESOURCE );
92              writer.addAttribute( ATTR_DEPLOY_PATH, "/" ); //$NON-NLS-1$
93              writer.addAttribute( ATTR_SOURCE_PATH, "/" //$NON-NLS-1$
94                  +
95                  IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(),
96                                                      new File( warSourceDirectory ), false ) );
97              writer.endElement();
98  
99              writeWarOrEarResources( writer, config.getProject(), config.getLocalRepository() );
100 
101             target = "/WEB-INF/classes"; //$NON-NLS-1$
102         }
103         else if ( Constants.PROJECT_PACKAGING_EAR.equals( config.getPackaging() ) ) //$NON-NLS-1$
104         {
105             writer.startElement( ELT_WB_RESOURCE );
106             writer.addAttribute( ATTR_DEPLOY_PATH, "/" ); //$NON-NLS-1$
107             writer.addAttribute( ATTR_SOURCE_PATH, "/" ); //$NON-NLS-1$
108             writer.endElement();
109 
110             writeWarOrEarResources( writer, config.getProject(), config.getLocalRepository() );
111         }
112 
113         for ( int j = 0; j < config.getSourceDirs().length; j++ )
114         {
115             EclipseSourceDir dir = config.getSourceDirs()[j];
116             // test src/resources are not added to wtpmodules
117             if ( !dir.isTest() )
118             {
119                 // <wb-resource deploy-path="/" source-path="/src/java" />
120                 writer.startElement( ELT_WB_RESOURCE );
121                 writer.addAttribute( ATTR_DEPLOY_PATH, target );
122                 writer.addAttribute( ATTR_SOURCE_PATH, dir.getPath() );
123                 writer.endElement();
124             }
125         }
126 
127         writer.endElement(); // wb-module
128         writer.endElement(); // project-modules
129 
130         IOUtil.close( w );
131     }
132 
133 }