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 1152335 2011-07-29 18:40:03Z rfscholte $
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, "UTF-8", null );
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                  + IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(),
95                                                        new File( warSourceDirectory ), false ) );
96              writer.endElement();
97  
98              writeWarOrEarResources( writer, config.getProject(), config.getLocalRepository() );
99  
100             target = "/WEB-INF/classes"; //$NON-NLS-1$
101         }
102         else if ( Constants.PROJECT_PACKAGING_EAR.equals( config.getPackaging() ) ) //$NON-NLS-1$
103         {
104             writer.startElement( ELT_WB_RESOURCE );
105             writer.addAttribute( ATTR_DEPLOY_PATH, "/" ); //$NON-NLS-1$
106             writer.addAttribute( ATTR_SOURCE_PATH, "/" ); //$NON-NLS-1$
107             writer.endElement();
108 
109             writeWarOrEarResources( writer, config.getProject(), config.getLocalRepository() );
110         }
111 
112         for ( int j = 0; j < config.getSourceDirs().length; j++ )
113         {
114             EclipseSourceDir dir = config.getSourceDirs()[j];
115             // test src/resources are not added to wtpmodules
116             if ( !dir.isTest() )
117             {
118                 // <wb-resource deploy-path="/" source-path="/src/java" />
119                 writer.startElement( ELT_WB_RESOURCE );
120                 writer.addAttribute( ATTR_DEPLOY_PATH, target );
121                 writer.addAttribute( ATTR_SOURCE_PATH, dir.getPath() );
122                 writer.endElement();
123             }
124         }
125 
126         writer.endElement(); // wb-module
127         writer.endElement(); // project-modules
128 
129         IOUtil.close( w );
130     }
131 
132 }