View Javadoc
1   package org.apache.maven.plugin.eclipse.writers;
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.IOException;
24  
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugin.eclipse.Constants;
27  import org.apache.maven.plugin.eclipse.EclipseSourceDir;
28  import org.apache.maven.plugin.eclipse.Messages;
29  import org.apache.maven.plugin.ide.IdeUtils;
30  import org.apache.maven.plugin.ide.JeeUtils;
31  import org.apache.maven.plugin.logging.Log;
32  import org.apache.maven.project.MavenProject;
33  
34  /**
35   * Create or adapt the manifest files for the RAD6 runtime dependencys. attention these will not be used for the real
36   * ear these are just to get the runtime enviorment using the maven dependencies. WARNING: The manifest resources added
37   * here will not have the benefit of the dependencies of the project, since that's not provided in the setup() apis, one
38   * of the locations from which this writer is used in the RadPlugin.
39   * 
40   * @author <a href="mailto:nir@cfc.at">Richard van Nieuwenhoven </a>
41   */
42  public class EclipseManifestWriter
43      extends AbstractEclipseManifestWriter
44  {
45  
46      private static final String GENERATED_RESOURCE_DIRNAME = "target" + File.separatorChar + "generated-resources"
47          + File.separatorChar + "eclipse";
48  
49      private static final String WEBAPP_RESOURCE_DIR = "src" + File.separatorChar + "main" + File.separatorChar
50          + "webapp";
51  
52      /**
53       * Returns absolute path to the web content directory based on configuration of the war plugin or default one
54       * otherwise.
55       * 
56       * @param project
57       * @return absolute directory path as String
58       * @throws MojoExecutionException
59       */
60      private static String getWebContentBaseDirectory( EclipseWriterConfig config )
61          throws MojoExecutionException
62      {
63          // getting true location of web source dir from config
64          File warSourceDirectory =
65              new File( IdeUtils.getPluginSetting( config.getProject(), JeeUtils.ARTIFACT_MAVEN_WAR_PLUGIN,
66                                                   "warSourceDirectory", WEBAPP_RESOURCE_DIR ) );
67          // getting real and correct path to the web source dir
68          String webContentDir =
69              IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(), warSourceDirectory, false );
70  
71          // getting the path to meta-inf base dir
72          String result = config.getProject().getBasedir().getAbsolutePath() + File.separatorChar + webContentDir;
73  
74          return result;
75      }
76  
77      /**
78       * Search the project for the existing META-INF directory where the manifest should be located.
79       * 
80       * @return the absolute path to the META-INF directory
81       * @throws MojoExecutionException
82       */
83      protected String getMetaInfBaseDirectory( MavenProject project )
84          throws MojoExecutionException
85      {
86          String metaInfBaseDirectory = null;
87  
88          if ( this.config.getProject().getPackaging().equals( Constants.PROJECT_PACKAGING_WAR ) )
89          {
90  
91              // getting the path to meta-inf base dir
92              metaInfBaseDirectory = getWebContentBaseDirectory( this.config );
93  
94              log.debug( "Attempting to use: " + metaInfBaseDirectory + " for location of META-INF in war project." );
95  
96              File metaInfDirectoryFile = new File( metaInfBaseDirectory + File.separatorChar 
97                                                    + AbstractEclipseManifestWriter.META_INF_DIRECTORY );
98  
99              if ( !metaInfDirectoryFile.exists()
100                 || ( metaInfDirectoryFile.exists() && !metaInfDirectoryFile.isDirectory() ) )
101             {
102                 metaInfBaseDirectory = null;
103             }
104         }
105 
106         for ( int index = this.config.getSourceDirs().length - 1; metaInfBaseDirectory == null && index >= 0; index-- )
107         {
108 
109             File manifestFile =
110                 new File( this.config.getEclipseProjectDirectory(), this.config.getSourceDirs()[index].getPath()
111                     + File.separatorChar + AbstractEclipseManifestWriter.META_INF_DIRECTORY + File.separatorChar
112                     + AbstractEclipseManifestWriter.MANIFEST_MF_FILENAME );
113 
114             this.log.debug( "Checking for existence of META-INF/MANIFEST.MF file: " + manifestFile );
115 
116             if ( manifestFile.exists() )
117             {
118                 metaInfBaseDirectory = manifestFile.getParentFile().getParent();
119             }
120         }
121 
122         return metaInfBaseDirectory;
123     }
124 
125     /**
126      * make room for a Manifest file. use a generated resource for JARS and for WARS use the manifest in the
127      * webapp/META-INF directory.
128      * 
129      * @throws MojoExecutionException
130      */
131     public static void addManifestResource( Log log, EclipseWriterConfig config )
132         throws MojoExecutionException
133     {
134 
135         AbstractEclipseManifestWriter manifestWriter = new EclipseManifestWriter();
136         manifestWriter.init( log, config );
137 
138         String packaging = config.getProject().getPackaging();
139 
140         String manifestDirectory = manifestWriter.getMetaInfBaseDirectory( config.getProject() );
141 
142         if ( !Constants.PROJECT_PACKAGING_EAR.equals( packaging )
143             && !Constants.PROJECT_PACKAGING_WAR.equals( packaging ) && manifestDirectory == null )
144         {
145 
146             String generatedResourceDir =
147                 config.getProject().getBasedir().getAbsolutePath() + File.separatorChar
148                     + EclipseManifestWriter.GENERATED_RESOURCE_DIRNAME;
149 
150             manifestDirectory = generatedResourceDir + File.separatorChar + "META-INF";
151 
152             try
153             {
154                 new File( manifestDirectory ).mkdirs();
155                 File manifestFile = new File( manifestDirectory + File.separatorChar + "MANIFEST.MF" );
156                 if ( manifestFile.exists() )
157                 {
158                     manifestFile.delete();
159                 }
160                 manifestFile.createNewFile();
161             }
162             catch ( IOException e )
163             {
164                 log.error( Messages.getString( "EclipsePlugin.cantwritetofile", new Object[] { manifestDirectory
165                     + File.separatorChar + "META-INF" + File.separatorChar + "MANIFEST.MF" } ) );
166             }
167 
168             log.debug( "Adding " + EclipseManifestWriter.GENERATED_RESOURCE_DIRNAME + " to eclipse sources " );
169 
170             EclipseSourceDir[] sourceDirs = config.getSourceDirs();
171             EclipseSourceDir[] newSourceDirs = new EclipseSourceDir[sourceDirs.length + 1];
172             System.arraycopy( sourceDirs, 0, newSourceDirs, 0, sourceDirs.length );
173             newSourceDirs[sourceDirs.length] =
174                 new EclipseSourceDir( EclipseManifestWriter.GENERATED_RESOURCE_DIRNAME, null, true, false, null, null,
175                                       false );
176             config.setSourceDirs( newSourceDirs );
177         }
178 
179         if ( Constants.PROJECT_PACKAGING_WAR.equals( packaging ) )
180         {
181             new File( getWebContentBaseDirectory( config ) + File.separatorChar + "META-INF" ).mkdirs();
182         }
183 
184         // special case must be done first because it can add stuff to the
185         // classpath that will be
186         // written by the superclass
187         manifestWriter.write();
188     }
189 }