View Javadoc
1   package org.apache.maven.plugins.assembly.archive;
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 org.apache.maven.archiver.MavenArchiveConfiguration;
23  import org.apache.maven.archiver.MavenArchiver;
24  import org.apache.maven.artifact.DependencyResolutionRequiredException;
25  import org.apache.maven.execution.MavenSession;
26  import org.apache.maven.project.MavenProject;
27  import org.codehaus.plexus.archiver.AbstractArchiveFinalizer;
28  import org.codehaus.plexus.archiver.Archiver;
29  import org.codehaus.plexus.archiver.ArchiverException;
30  import org.codehaus.plexus.archiver.jar.JarArchiver;
31  import org.codehaus.plexus.archiver.jar.Manifest;
32  import org.codehaus.plexus.archiver.jar.ManifestException;
33  import org.codehaus.plexus.util.IOUtil;
34  
35  import java.io.File;
36  import java.io.FileInputStream;
37  import java.io.FileNotFoundException;
38  import java.io.IOException;
39  import java.io.InputStreamReader;
40  import java.io.Reader;
41  import java.nio.charset.StandardCharsets;
42  import java.util.Collections;
43  import java.util.List;
44  
45  /**
46   *
47   */
48  public class ManifestCreationFinalizer
49      extends AbstractArchiveFinalizer
50  {
51  
52      private final MavenProject project;
53  
54      private final MavenSession session;
55  
56      private final MavenArchiveConfiguration archiveConfiguration;
57  
58      // TODO: I'd really prefer to rewrite MavenArchiver as either a
59      // separate manifest creation utility (and to
60      // create an include pom.properties etc into another archiver), or
61      // an implementation of an archiver
62      // (the first is preferable).
63      private final MavenArchiver mavenArchiver = new MavenArchiver();
64  
65      public ManifestCreationFinalizer( final MavenSession session, final MavenProject project,
66                                        final MavenArchiveConfiguration archiveConfiguration )
67      {
68          this.session = session;
69          this.project = project;
70          this.archiveConfiguration = archiveConfiguration;
71      }
72  
73      @Override
74      public void finalizeArchiveCreation( final Archiver archiver )
75      {
76          if ( archiveConfiguration != null )
77          {
78              try
79              {
80                  Manifest manifest;
81                  final File manifestFile = archiveConfiguration.getManifestFile();
82  
83                  if ( manifestFile != null )
84                  {
85                      Reader manifestFileReader = null;
86                      try
87                      {
88                          manifestFileReader = new InputStreamReader( new FileInputStream( manifestFile ),
89                              StandardCharsets.UTF_8 );
90                          manifest = new Manifest( manifestFileReader );
91                          manifestFileReader.close();
92                          manifestFileReader = null;
93                      }
94                      catch ( final FileNotFoundException e )
95                      {
96                          throw new ArchiverException( "Manifest not found: " + e.getMessage(), e );
97                      }
98                      catch ( final IOException e )
99                      {
100                         throw new ArchiverException( "Error processing manifest: " + e.getMessage(), e );
101                     }
102                     finally
103                     {
104                         IOUtil.close( manifestFileReader );
105                     }
106                 }
107                 else
108                 {
109                     manifest = mavenArchiver.getManifest( session, project, archiveConfiguration );
110                 }
111 
112                 if ( ( manifest != null ) && ( archiver instanceof JarArchiver ) )
113                 {
114                     final JarArchiver jarArchiver = (JarArchiver) archiver;
115                     jarArchiver.addConfiguredManifest( manifest );
116                 }
117             }
118             catch ( final ManifestException e )
119             {
120                 throw new ArchiverException( "Error creating manifest: " + e.getMessage(), e );
121             }
122             catch ( final DependencyResolutionRequiredException e )
123             {
124                 throw new ArchiverException( "Dependencies were not resolved: " + e.getMessage(), e );
125             }
126         }
127     }
128 
129     @Override
130     public List<String> getVirtualFiles()
131     {
132         if ( archiveConfiguration != null )
133         {
134             try
135             {
136                 if ( mavenArchiver.getManifest( project, archiveConfiguration.getManifest() ) != null )
137                 {
138                     return Collections.singletonList( "META-INF/MANIFEST.MF" );
139                 }
140             }
141             catch ( final ManifestException | DependencyResolutionRequiredException ignore )
142             {
143                 // noop
144             }
145         }
146 
147         return null;
148     }
149 
150 }