2121package com .github .wvengen .maven .proguard ;
2222
2323import java .io .File ;
24- import java .io .FileWriter ;
2524import java .io .FileInputStream ;
2625import java .io .FileOutputStream ;
26+ import java .io .FileWriter ;
2727import java .io .IOException ;
2828import java .net .URL ;
2929import java .util .ArrayList ;
30+ import java .util .Arrays ;
3031import java .util .Collection ;
3132import java .util .Collections ;
3233import java .util .HashMap ;
@@ -398,6 +399,15 @@ public class ProGuardMojo extends AbstractMojo {
398399
399400 private Log log ;
400401
402+ /**
403+ * ProGuard filter which excludes the {@code MANIFEST.MF} file
404+ */
405+ private static final String MANIFEST_FILTER = "!META-INF/MANIFEST.MF" ;
406+ /**
407+ * ProGuard filter which excludes the Maven descriptors in the {@code META-INF/maven/} directory
408+ */
409+ private static final String MAVEN_DESCRIPTORS_FILTER = "!META-INF/maven/**" ;
410+
401411 /**
402412 * ProGuard docs: Names with special characters like spaces and parentheses must be quoted with single or double
403413 * quotes.
@@ -410,14 +420,29 @@ private String fileToString(File file) {
410420 return fileNameToString (file .toString ());
411421 }
412422
423+ /**
424+ * Creates a ProGuard classpath filter string.
425+ */
426+ private String createFilterString (List <String > names ) {
427+ if (names .isEmpty ()) {
428+ return "" ;
429+ }
430+
431+ return "(" + String .join ("," , names ) + ")" ;
432+ }
433+
434+ private String createFilterString (String ... names ) {
435+ return createFilterString (Arrays .asList (names ));
436+ }
437+
413438 private String libFileToStringWithInLibsFilter (File file ) {
414439 return libFileToStringWithInLibsFilter (file .toString ());
415440 }
416441
417442 private String libFileToStringWithInLibsFilter (String file ) {
418443 StringBuilder filter = new StringBuilder (fileNameToString (file ));
419444 if ((inLibsFilter != null )) {
420- filter .append ("(" ). append ( inLibsFilter ). append ( ")" );
445+ filter .append (createFilterString ( inLibsFilter ));
421446 }
422447 return filter .toString ();
423448 }
@@ -547,17 +572,15 @@ public void execute() throws MojoExecutionException, MojoFailureException {
547572 StringBuilder filter = new StringBuilder (fileToString (file ));
548573 List <String > filterList = new ArrayList <>();
549574 if (!addManifest ) {
550- filterList .add ("!META-INF/MANIFEST.MF" );
575+ filterList .add (MANIFEST_FILTER );
551576 }
552577 if (!addMavenDescriptor ) {
553- filterList .add ("!META-INF/maven/**" );
578+ filterList .add (MAVEN_DESCRIPTORS_FILTER );
554579 }
555580 if (entry .getValue ().filter != null ) {
556581 filterList .add (entry .getValue ().filter );
557582 }
558- if (filterList .size () > 0 ){
559- filter .append ("(" ).append (String .join ("," ,filterList )).append ( ")" );
560- }
583+ filter .append (createFilterString (filterList ));
561584 args .add ("-injars" );
562585 args .add (filter .toString ());
563586 }
@@ -580,27 +603,34 @@ public void execute() throws MojoExecutionException, MojoFailureException {
580603 args .add ("-injars" );
581604 StringBuilder filter = new StringBuilder (fileToString (inJarFile ));
582605 if ((inFilter != null ) || (!addMavenDescriptor )) {
583- filter .append ("(" );
584- boolean coma = false ;
606+ List <String > filterList = new ArrayList <>();
585607
586608 if (!addMavenDescriptor ) {
587- coma = true ;
588- filter .append ("!META-INF/maven/**" );
609+ filterList .add (MAVEN_DESCRIPTORS_FILTER );
589610 }
590611
591612 if (inFilter != null ) {
592- if (coma ) {
593- filter .append ("," );
594- }
595- filter .append (inFilter );
613+ filterList .add (inFilter );
596614 }
597615
598- filter .append (")" );
616+ filter .append (createFilterString ( filterList ) );
599617 }
600618 args .add (filter .toString ());
601619 }
602620
603621 if (includeDependency ) {
622+ List <String > dependencyInjarFilterList = new ArrayList <>();
623+ if (!addManifest ) {
624+ dependencyInjarFilterList .add (MANIFEST_FILTER );
625+ }
626+ if (!addMavenDescriptor ) {
627+ dependencyInjarFilterList .add (MAVEN_DESCRIPTORS_FILTER );
628+ }
629+ if (inFilter != null ) {
630+ dependencyInjarFilterList .add (inFilter );
631+ }
632+ String dependencyInjarFilter = createFilterString (dependencyInjarFilterList );
633+
604634 @ SuppressWarnings ("unchecked" )
605635 List <Artifact > dependency = this .mavenProject .getCompileArtifacts ();
606636 for (Artifact artifact : dependency ) {
@@ -617,7 +647,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
617647 if (includeDependencyInjar ) {
618648 log .debug ("--- ADD library as injars:" + artifact .getArtifactId ());
619649 args .add ("-injars" );
620- args .add (fileToString (file ));
650+ args .add (fileToString (file ) + dependencyInjarFilter );
621651 } else {
622652 log .debug ("--- ADD libraryjars:" + artifact .getArtifactId ());
623653 if (putLibraryJarsInTempDir ) {
@@ -634,7 +664,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
634664 args .add ("-outjars" );
635665 StringBuilder filter = new StringBuilder (fileToString (outJarFile ));
636666 if (outFilter != null ) {
637- filter .append ("(" ). append ( outFilter ). append ( ")" );
667+ filter .append (createFilterString ( outFilter ));
638668 }
639669 args .add (filter .toString ());
640670 }
0 commit comments