@@ -276,7 +276,7 @@ private void addAutomaticModuleName(File originalJar, File moduleJar, AutomaticM
276276 try (JarOutputStream outputStream = newJarOutputStream (Files .newOutputStream (moduleJar .toPath ()), manifest )) {
277277 Map <String , List <String >> providers = new LinkedHashMap <>();
278278 Set <String > packages = new TreeSet <>();
279- copyAndExtractProviders (inputStream , outputStream , !automaticModule .getMergedJars ().isEmpty (), providers , packages );
279+ copyAndExtractProviders (inputStream , outputStream , automaticModule . getRemovedPackages (), !automaticModule .getMergedJars ().isEmpty (), providers , packages );
280280 mergeJars (automaticModule , outputStream , providers , packages );
281281 }
282282 } catch (IOException e ) {
@@ -289,7 +289,7 @@ private void addModuleDescriptor(File originalJar, File moduleJar, ModuleInfo mo
289289 try (JarOutputStream outputStream = newJarOutputStream (Files .newOutputStream (moduleJar .toPath ()), inputStream .getManifest ())) {
290290 Map <String , List <String >> providers = new LinkedHashMap <>();
291291 Set <String > packages = new TreeSet <>();
292- byte [] existingModuleInfo = copyAndExtractProviders (inputStream , outputStream , !moduleInfo .getMergedJars ().isEmpty (), providers , packages );
292+ byte [] existingModuleInfo = copyAndExtractProviders (inputStream , outputStream , moduleInfo . getRemovedPackages (), !moduleInfo .getMergedJars ().isEmpty (), providers , packages );
293293 mergeJars (moduleInfo , outputStream , providers , packages );
294294 outputStream .putNextEntry (newReproducibleEntry ("module-info.class" ));
295295 outputStream .write (addModuleInfo (moduleInfo , providers , versionFromFilePath (originalJar .toPath ()),
@@ -314,7 +314,7 @@ private JarOutputStream newJarOutputStream(OutputStream out, @Nullable Manifest
314314 }
315315
316316 @ Nullable
317- private byte [] copyAndExtractProviders (JarInputStream inputStream , JarOutputStream outputStream , boolean willMergeJars , Map <String , List <String >> providers , Set <String > packages ) throws IOException {
317+ private byte [] copyAndExtractProviders (JarInputStream inputStream , JarOutputStream outputStream , List < String > removedPackages , boolean willMergeJars , Map <String , List <String >> providers , Set <String > packages ) throws IOException {
318318 JarEntry jarEntry = inputStream .getNextJarEntry ();
319319 byte [] existingModuleInfo = null ;
320320 while (jarEntry != null ) {
@@ -334,25 +334,26 @@ private byte[] copyAndExtractProviders(JarInputStream inputStream, JarOutputStre
334334 existingModuleInfo = content ;
335335 } else if (!JAR_SIGNATURE_PATH .matcher (entryName ).matches () && !"META-INF/MANIFEST.MF" .equals (entryName )) {
336336 if (!willMergeJars || !isFileInServicesFolder ) { // service provider files will be merged later
337- jarEntry .setCompressedSize (-1 );
338- try {
339- outputStream .putNextEntry (jarEntry );
340- outputStream .write (content );
341- outputStream .closeEntry ();
342- } catch (ZipException e ) {
343- if (!e .getMessage ().startsWith ("duplicate entry:" )) {
344- throw new RuntimeException (e );
337+ Matcher mrJarMatcher = MRJAR_VERSIONS_PATH .matcher (entryName );
338+ int i = entryName .lastIndexOf ("/" );
339+ String packagePath = i > 0 ? mrJarMatcher .matches ()
340+ ? mrJarMatcher .group (1 )
341+ : entryName .substring (0 , i )
342+ : "" ;
343+
344+ if (!removedPackages .contains (packagePath .replace ('/' , '.' ))) {
345+ if (entryName .endsWith (".class" ) && !packagePath .isEmpty ()) {
346+ packages .add (packagePath );
345347 }
346- }
347- if (entryName .endsWith (".class" )) {
348- int i = entryName .lastIndexOf ("/" );
349- if (i > 0 ) {
350- Matcher mrJarMatcher = MRJAR_VERSIONS_PATH .matcher (entryName );
351- if (mrJarMatcher .matches ()) {
352- // Strip the 'META-INF/versions/11' part
353- packages .add (mrJarMatcher .group (1 ));
354- } else {
355- packages .add (entryName .substring (0 , i ));
348+
349+ try {
350+ jarEntry .setCompressedSize (-1 );
351+ outputStream .putNextEntry (jarEntry );
352+ outputStream .write (content );
353+ outputStream .closeEntry ();
354+ } catch (ZipException e ) {
355+ if (!e .getMessage ().startsWith ("duplicate entry:" )) {
356+ throw new RuntimeException (e );
356357 }
357358 }
358359 }
@@ -497,7 +498,7 @@ private void mergeJars(ModuleSpec moduleSpec, JarOutputStream outputStream, Map<
497498
498499 if (mergeJarFile != null ) {
499500 try (JarInputStream toMergeInputStream = new JarInputStream (Files .newInputStream (mergeJarFile .getAsFile ().toPath ()))) {
500- copyAndExtractProviders (toMergeInputStream , outputStream , true , providers , packages );
501+ copyAndExtractProviders (toMergeInputStream , outputStream , moduleSpec . getRemovedPackages (), true , providers , packages );
501502 }
502503 } else {
503504 throw new RuntimeException ("Jar not found: " + identifier );
0 commit comments