View Javadoc

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