This is an automated email from the ASF dual-hosted git repository.

skygo pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/netbeans-mavenutils-nbm-maven-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new 8473ff2  [NETBEANSINFRA-189]: Fix regression introduced by 
[NETBEANSINFRA-188]
     new 0370229  Merge pull request #10 from codingfred/fix_file_truncations
8473ff2 is described below

commit 8473ff24faf6b69b8e087167d3b21c571acee8df
Author: Frederic Simons <codingf...@gmail.com>
AuthorDate: Fri May 8 20:16:03 2020 +0200

    [NETBEANSINFRA-189]: Fix regression introduced by [NETBEANSINFRA-188]
    
    The optimization  - implemented in #8 - introduced a serious regression
    by truncating the whole jar file instead of modifying a single entry in
    the jar file.
    
    This PR fixes this by applying another approach. Using the in Java 7
    introduced Files API, one can create a virtual filesystem of a zip file
    and read or write single entries.
    
    In my tests the now modfied jar files are no longer truncated
    and the whole module suite can be compiled and executed successfully.
    The intended speedup retains.
---
 .../netbeans/nbm/CreateNetBeansFileStructure.java  | 51 ++++++++++++----------
 1 file changed, 28 insertions(+), 23 deletions(-)

diff --git 
a/src/main/java/org/apache/netbeans/nbm/CreateNetBeansFileStructure.java 
b/src/main/java/org/apache/netbeans/nbm/CreateNetBeansFileStructure.java
index 4bce068..9d33cc6 100644
--- a/src/main/java/org/apache/netbeans/nbm/CreateNetBeansFileStructure.java
+++ b/src/main/java/org/apache/netbeans/nbm/CreateNetBeansFileStructure.java
@@ -19,12 +19,16 @@ package org.apache.netbeans.nbm;
  * under the License.
  */
 
+import java.io.BufferedOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.lang.reflect.Field;
+import java.nio.file.FileSystem;
+import java.nio.file.FileSystems;
+import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -34,6 +38,7 @@ import java.util.Hashtable;
 import java.util.List;
 import java.util.Map;
 import java.util.jar.Attributes;
+import java.util.jar.JarFile;
 import java.util.jar.JarInputStream;
 import java.util.jar.JarOutputStream;
 import java.util.jar.Manifest;
@@ -280,43 +285,43 @@ public abstract class CreateNetBeansFileStructure
         }
         catch ( IOException x )
         {
-            throw new MojoExecutionException( "Cannot copy module jar", x );
+            throw new MojoExecutionException( "Cannot copy module JAR", x );
         }
-        
-        {   // Temporary scope so that the Variables m,a & s (see below) can 
be reclaimed earlier
-            // and do not pollute the current scope
-            // Check if Manifest needs to be patched
-            Manifest m = null;
-            try ( JarInputStream jis = new JarInputStream( new 
FileInputStream( moduleFile ) ) )
-            {
-                // save the manifest & done
-                m = jis.getManifest(); 
-            }
-            catch ( IOException x )
-            {
-                throw new MojoExecutionException( "Could not read manifest of 
copied module jar", x );
-            }
-         
+
+        try ( JarInputStream jis = new JarInputStream( Files.newInputStream( 
jarFile.toPath() ) ) )
+        {
+            Manifest m = jis.getManifest();               
             Attributes a = m.getMainAttributes();
             String classPath = ( String ) a.remove( new Attributes.Name( 
"X-Class-Path" ) );
-         
+
             if ( classPath != null )
             {
                 // update the manifest
-                getLog().info( "Updating manifest of module jar" );
+                getLog().info( "Updating manifest of copied module JAR" );
                 a.putValue( "Class-Path", classPath );
                 a.remove( new Attributes.Name( "Maven-Class-Path" ) );
-                try ( JarOutputStream jos = new JarOutputStream( new 
FileOutputStream( moduleFile ), m ) )
+
+                try ( FileSystem fs = FileSystems.newFileSystem( 
moduleFile.toPath(), null ) )
                 {
-                    // nothing to do here. just close the stream. 
-                    // The updated manifest gets written in the constructor
+                    try ( BufferedOutputStream mfWriter = new 
BufferedOutputStream( Files.newOutputStream( fs.getPath( JarFile.MANIFEST_NAME 
) ) ) )
+                    {
+                        m.write( mfWriter );
+                    }
+                    catch ( IOException ioex )
+                    {
+                        throw new MojoExecutionException( "Could not overwrite 
manifest entry", ioex );
+                    }
                 }
-                catch ( IOException x )
+                catch ( IOException ex )
                 {
-                    throw new MojoExecutionException( "Could not update 
manifest of copied module jar", x );
+                    throw new MojoExecutionException( "Unable to create zip 
filesystem", ex );
                 }
             }
         }
+        catch ( IOException x )
+        {
+            throw new MojoExecutionException( "Could not read manifest of 
module JAR", x );
+        }
         
         ExamineManifest modExaminator = new ExamineManifest( getLog() );
         modExaminator.setJarFile( moduleFile );


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org
For additional commands, e-mail: commits-h...@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to