View Javadoc

1   package org.apache.maven.plugin.ear.util;
2   
3   import org.apache.maven.archiver.MavenArchiveConfiguration;
4   import org.apache.maven.archiver.MavenArchiver;
5   import org.apache.maven.artifact.DependencyResolutionRequiredException;
6   import org.apache.maven.plugin.ear.EarModule;
7   import org.apache.maven.project.MavenProject;
8   import org.codehaus.plexus.archiver.jar.Manifest;
9   import org.codehaus.plexus.archiver.jar.ManifestException;
10  
11  import java.util.Iterator;
12  import java.util.List;
13  import java.util.Set;
14  
15  /*
16   * Licensed to the Apache Software Foundation (ASF) under one
17   * or more contributor license agreements.  See the NOTICE file
18   * distributed with this work for additional information
19   * regarding copyright ownership.  The ASF licenses this file
20   * to you under the Apache License, Version 2.0 (the
21   * "License"); you may not use this file except in compliance
22   * with the License.  You may obtain a copy of the License at
23   *
24   *  http://www.apache.org/licenses/LICENSE-2.0
25   *
26   * Unless required by applicable law or agreed to in writing,
27   * software distributed under the License is distributed on an
28   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
29   * KIND, either express or implied.  See the License for the
30   * specific language governing permissions and limitations
31   * under the License.
32   */
33  
34  /**
35   * A custom {@link MavenArchiver} implementation that takes care
36   * of setting the right classpath value according to the actual
37   * path of bundled files.
38   *
39   * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
40   */
41  public class EarMavenArchiver
42      extends MavenArchiver
43  {
44      public static final String CLASS_PATH_KEY = "Class-Path";
45  
46      private final List earModules;
47  
48  
49      /**
50       * Creates an instance with the ear modules that will be packaged
51       * in the EAR archive.
52       *
53       * @param earModules the intitialized list of ear modules
54       */
55      public EarMavenArchiver( List earModules )
56      {
57          this.earModules = earModules;
58      }
59  
60      public Manifest getManifest( MavenProject project, MavenArchiveConfiguration config )
61          throws ManifestException, DependencyResolutionRequiredException
62      {
63          final Manifest manifest = super.getManifest( project, config );
64          if ( config.getManifest().isAddClasspath() )
65          {
66              String earManifestClassPathEntry = generateClassPathEntry( config.getManifest().getClasspathPrefix() );
67              // Class-path can be customized. Let's make sure we don't overwrite this
68              // with our custom change!
69              final String userSuppliedClassPathEntry = getUserSuppliedClassPathEntry( config );
70              if ( userSuppliedClassPathEntry != null )
71              {
72                  earManifestClassPathEntry = userSuppliedClassPathEntry + " " + earManifestClassPathEntry;
73              }
74  
75              // Overwrite the existing one, if any
76              final Manifest.Attribute classPathAttr = manifest.getMainSection().getAttribute( CLASS_PATH_KEY );
77              if ( classPathAttr != null )
78              {
79                  classPathAttr.setValue( earManifestClassPathEntry );
80              }
81              else
82              {
83                  final Manifest.Attribute attr = new Manifest.Attribute( CLASS_PATH_KEY, earManifestClassPathEntry );
84                  manifest.addConfiguredAttribute( attr );
85              }
86          }
87          return manifest;
88      }
89  
90      /**
91       * Generates the <tt>Class-Path</tt> entry of the manifest according to
92       * the list of ear modules.
93       *
94       * @param classPathPrefix the classpath prefix to use
95       * @return the <tt>Class-Path</tt> entry
96       */
97      protected String generateClassPathEntry( String classPathPrefix )
98      {
99          final StringBuffer classpath = new StringBuffer();
100         final Iterator it = earModules.iterator();
101         while ( it.hasNext() )
102         {
103             final EarModule earModule = (EarModule) it.next();
104             if ( !earModule.isExcluded() )
105             {
106                 classpath.append( classPathPrefix ).append( earModule.getUri() ).append( " " );
107             }
108         }
109         return classpath.toString().trim();
110     }
111 
112     protected String getUserSuppliedClassPathEntry( MavenArchiveConfiguration config )
113     {
114         if ( config.getManifestEntries() != null )
115         {
116             final Set keys = config.getManifestEntries().keySet();
117             for ( Iterator iter = keys.iterator(); iter.hasNext(); )
118             {
119                 String key = (String) iter.next();
120                 String value = (String) config.getManifestEntries().get( key );
121                 if ( "Class-Path".equals( key ) && value != null )
122                 {
123                     return value;
124 
125                 }
126 
127             }
128         }
129         return null;
130     }
131 }