View Javadoc
1   package org.apache.maven.plugins.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.plugins.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      /**
44       * {@code Class-Path}.
45       */
46      public static final String CLASS_PATH_KEY = "Class-Path";
47  
48      private final List<EarModule> earModules;
49  
50      /**
51       * Creates an instance with the ear modules that will be packaged in the EAR archive.
52       * 
53       * @param earModules the intitialized list of ear modules
54       */
55      public EarMavenArchiver( List<EarModule> earModules )
56      {
57          this.earModules = earModules;
58      }
59  
60      /**
61       * @param project {@link MavenProject}
62       * @param config {@link MavenArchiveConfiguration}
63       * @throws ManifestException in case of an error
64       * @throws DependencyResolutionRequiredException in case of an resolution error.
65       * @return Manifest
66       * @deprecated
67       */
68      public Manifest getManifest( MavenProject project, MavenArchiveConfiguration config )
69          throws ManifestException, DependencyResolutionRequiredException
70      {
71          return this.getManifest( null, project, config );
72      }
73  
74      /** {@inheritDoc} */
75      public Manifest getManifest( MavenSession session, MavenProject project, MavenArchiveConfiguration config )
76          throws ManifestException, DependencyResolutionRequiredException
77      {
78          final Manifest manifest = super.getManifest( session, project, config );
79          if ( config.getManifest().isAddClasspath() )
80          {
81              String earManifestClassPathEntry = generateClassPathEntry( config.getManifest().getClasspathPrefix() );
82              // Class-path can be customized. Let's make sure we don't overwrite this
83              // with our custom change!
84              final String userSuppliedClassPathEntry = getUserSuppliedClassPathEntry( config );
85              if ( userSuppliedClassPathEntry != null )
86              {
87                  earManifestClassPathEntry = userSuppliedClassPathEntry + " " + earManifestClassPathEntry;
88              }
89  
90              // Overwrite the existing one, if any
91              final Manifest.Attribute classPathAttr = manifest.getMainSection().getAttribute( CLASS_PATH_KEY );
92              if ( classPathAttr != null )
93              {
94                  classPathAttr.setValue( earManifestClassPathEntry );
95              }
96              else
97              {
98                  final Manifest.Attribute attr = new Manifest.Attribute( CLASS_PATH_KEY, earManifestClassPathEntry );
99                  manifest.addConfiguredAttribute( attr );
100             }
101         }
102         return manifest;
103     }
104 
105     /**
106      * Generates the {@code Class-Path} entry of the manifest according to the list of ear modules.
107      * 
108      * @param classPathPrefix the classpath prefix to use
109      * @return the {@code Class-Path} entry
110      */
111     protected String generateClassPathEntry( String classPathPrefix )
112     {
113         final StringBuilder classpath = new StringBuilder();
114         for ( final EarModule earModule : earModules )
115         {
116             if ( !earModule.isExcluded() )
117             {
118                 classpath.append( classPathPrefix ).append( earModule.getUri() ).append( " " );
119             }
120         }
121         return classpath.toString().trim();
122     }
123 
124     /**
125      * @param config {@link MavenArchiveConfiguration}
126      * @return The class path entry.
127      */
128     protected String getUserSuppliedClassPathEntry( MavenArchiveConfiguration config )
129     {
130         if ( config.getManifestEntries() != null )
131         {
132             final Set<String> keys = config.getManifestEntries().keySet();
133             for ( String key : keys )
134             {
135                 String value = config.getManifestEntries().get( key );
136                 if ( "Class-Path".equals( key ) && value != null )
137                 {
138                     return value;
139 
140                 }
141 
142             }
143         }
144         return null;
145     }
146 }