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