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.rad;
20  
21  import java.io.File;
22  
23  import org.apache.maven.model.Resource;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.eclipse.Constants;
26  import org.apache.maven.plugin.eclipse.EclipseSourceDir;
27  import org.apache.maven.plugin.eclipse.writers.AbstractEclipseManifestWriter;
28  import org.apache.maven.plugin.ide.IdeUtils;
29  import org.apache.maven.plugin.ide.JeeUtils;
30  import org.apache.maven.project.MavenProject;
31  
32  /**
33   * Create or adapt the manifest files for the RAD6 runtime dependencys. attention these will not be used for the real
34   * ear these are just to get the runtime enviorment using the maven dependencies. WARNING: The manifest resources added
35   * here will not have the benefit of the dependencies of the project, since that's not provided in the setup() apis, one
36   * of the locations from which this writer is used in the RadPlugin.
37   * 
38   * @author <a href="mailto:nir@cfc.at">Richard van Nieuwenhoven </a>
39   */
40  public class RadManifestWriter
41      extends AbstractEclipseManifestWriter
42  {
43  
44      private static final String DEFAULT_WEBAPP_RESOURCE_DIR =
45          "src" + File.separatorChar + "main" + File.separatorChar + "webapp";
46  
47      /**
48       * Search the project for the existing META-INF directory where the manifest should be located.
49       * 
50       * @return the apsolute path to the META-INF directory
51       * @throws MojoExecutionException
52       */
53      protected String getMetaInfBaseDirectory( MavenProject project )
54          throws MojoExecutionException
55      {
56          String metaInfBaseDirectory = null;
57  
58          if ( config.getProject().getPackaging().equals( Constants.PROJECT_PACKAGING_WAR ) )
59          {
60              // Generating web content settings based on war plug-in warSourceDirectory property
61              File warSourceDirectory =
62                  new File( IdeUtils.getPluginSetting( config.getProject(), JeeUtils.ARTIFACT_MAVEN_WAR_PLUGIN,
63                                                       "warSourceDirectory", //$NON-NLS-1$
64                                                       DEFAULT_WEBAPP_RESOURCE_DIR ) );
65  
66              String webContentDir =
67                  IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(), warSourceDirectory, false );
68  
69              metaInfBaseDirectory =
70                  config.getProject().getBasedir().getAbsolutePath() + File.separatorChar + webContentDir;
71  
72              log.debug( "Attempting to use: " + metaInfBaseDirectory + " for location of META-INF in war project." );
73  
74              File metaInfDirectoryFile = new File( metaInfBaseDirectory + File.separatorChar + META_INF_DIRECTORY );
75  
76              if ( metaInfDirectoryFile.exists() && !metaInfDirectoryFile.isDirectory() )
77              {
78                  metaInfBaseDirectory = null;
79              }
80          }
81  
82          if ( metaInfBaseDirectory == null )
83          {
84              for (Object o : project.getResources()) {
85                  metaInfBaseDirectory = ((Resource) o).getDirectory();
86  
87                  File metaInfDirectoryFile = new File(metaInfBaseDirectory + File.separatorChar + META_INF_DIRECTORY);
88  
89                  log.debug("Checking for existence of META-INF directory: " + metaInfDirectoryFile);
90  
91                  if (metaInfDirectoryFile.exists() && !metaInfDirectoryFile.isDirectory()) {
92                      metaInfBaseDirectory = null;
93                  }
94              }
95          }
96  
97          return metaInfBaseDirectory;
98      }
99  
100     /**
101      * {@inheritDoc}
102      */
103     public void write()
104         throws MojoExecutionException
105     {
106         super.write();
107         verifyManifestBasedirInSourceDirs( getMetaInfBaseDirectory( config.getProject() ) );
108     }
109 
110     // NOTE: This could change the config!
111     private void verifyManifestBasedirInSourceDirs( String metaInfBaseDirectory )
112     {
113         EclipseSourceDir[] sourceDirs = config.getSourceDirs();
114 
115         if ( sourceDirs != null )
116         {
117             boolean foundMetaInfBaseDirectory = false;
118 
119             for (EclipseSourceDir esd : sourceDirs) {
120                 if (esd.getPath().equals(metaInfBaseDirectory)) {
121                     foundMetaInfBaseDirectory = true;
122                     break;
123                 }
124             }
125 
126             if ( !foundMetaInfBaseDirectory )
127             {
128                 EclipseSourceDir dir =
129                     new EclipseSourceDir( metaInfBaseDirectory, null, true, false, null, null, false );
130 
131                 EclipseSourceDir[] newSourceDirs = new EclipseSourceDir[sourceDirs.length + 1];
132                 newSourceDirs[sourceDirs.length] = dir;
133 
134                 System.arraycopy( sourceDirs, 0, newSourceDirs, 0, sourceDirs.length );
135 
136                 config.setSourceDirs( newSourceDirs );
137             }
138         }
139     }
140 }