View Javadoc

1   package org.apache.maven.plugin.ear.util;
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.util.List;
23  import java.util.Set;
24  
25  import org.apache.maven.archiver.MavenArchiveConfiguration;
26  import org.apache.maven.archiver.MavenArchiver;
27  import org.apache.maven.artifact.DependencyResolutionRequiredException;
28  import org.apache.maven.execution.MavenSession;
29  import org.apache.maven.plugin.ear.EarModule;
30  import org.apache.maven.project.MavenProject;
31  import org.codehaus.plexus.archiver.jar.Manifest;
32  import org.codehaus.plexus.archiver.jar.ManifestException;
33  
34  /**
35   * A custom {@link MavenArchiver} implementation that takes care of setting the right classpath value according to the
36   * actual path of bundled files.
37   * 
38   * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
39   */
40  public class EarMavenArchiver
41      extends MavenArchiver
42  {
43      public static final String CLASS_PATH_KEY = "Class-Path";
44  
45      private final List<EarModule> earModules;
46  
47      /**
48       * Creates an instance with the ear modules that will be packaged in the EAR archive.
49       * 
50       * @param earModules the intitialized list of ear modules
51       */
52      public EarMavenArchiver( List<EarModule> earModules )
53      {
54          this.earModules = earModules;
55      }
56  
57      /** @deprecated */
58      public Manifest getManifest( MavenProject project, MavenArchiveConfiguration config )
59          throws ManifestException, DependencyResolutionRequiredException
60      {
61          return this.getManifest( null, project, config );
62      }
63  
64      public Manifest getManifest( MavenSession session, MavenProject project, MavenArchiveConfiguration config )
65          throws ManifestException, DependencyResolutionRequiredException
66      {
67          final Manifest manifest = super.getManifest( session, project, config );
68          if ( config.getManifest().isAddClasspath() )
69          {
70              String earManifestClassPathEntry = generateClassPathEntry( config.getManifest().getClasspathPrefix() );
71              // Class-path can be customized. Let's make sure we don't overwrite this
72              // with our custom change!
73              final String userSuppliedClassPathEntry = getUserSuppliedClassPathEntry( config );
74              if ( userSuppliedClassPathEntry != null )
75              {
76                  earManifestClassPathEntry = userSuppliedClassPathEntry + " " + earManifestClassPathEntry;
77              }
78  
79              // Overwrite the existing one, if any
80              final Manifest.Attribute classPathAttr = manifest.getMainSection().getAttribute( CLASS_PATH_KEY );
81              if ( classPathAttr != null )
82              {
83                  classPathAttr.setValue( earManifestClassPathEntry );
84              }
85              else
86              {
87                  final Manifest.Attribute attr = new Manifest.Attribute( CLASS_PATH_KEY, earManifestClassPathEntry );
88                  manifest.addConfiguredAttribute( attr );
89              }
90          }
91          return manifest;
92      }
93  
94      /**
95       * Generates the <tt>Class-Path</tt> entry of the manifest according to the list of ear modules.
96       * 
97       * @param classPathPrefix the classpath prefix to use
98       * @return the <tt>Class-Path</tt> entry
99       */
100     protected String generateClassPathEntry( String classPathPrefix )
101     {
102         final StringBuilder classpath = new StringBuilder();
103         for ( final EarModule earModule : earModules )
104         {
105             if ( !earModule.isExcluded() )
106             {
107                 classpath.append( classPathPrefix ).append( earModule.getUri() ).append( " " );
108             }
109         }
110         return classpath.toString().trim();
111     }
112 
113     protected String getUserSuppliedClassPathEntry( MavenArchiveConfiguration config )
114     {
115         if ( config.getManifestEntries() != null )
116         {
117             @SuppressWarnings( "unchecked" )
118             final Set<String> keys = config.getManifestEntries().keySet();
119             for ( String key : keys )
120             {
121                 String value = config.getManifestEntries().get( key );
122                 if ( "Class-Path".equals( key ) && value != null )
123                 {
124                     return value;
125 
126                 }
127 
128             }
129         }
130         return null;
131     }
132 }