Skip to content

Commit a283f64

Browse files
Merge pull request wvengen#393 from Marcono1234/marcono1234/dependency-injar-filter
Apply filters when using `includeDependencyInjar`
2 parents 16a6a37 + 3f4676a commit a283f64

File tree

1 file changed

+47
-17
lines changed

1 file changed

+47
-17
lines changed

src/main/java/com/github/wvengen/maven/proguard/ProGuardMojo.java

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.IOException;
2828
import java.net.URL;
2929
import java.util.ArrayList;
30+
import java.util.Arrays;
3031
import java.util.Collection;
3132
import java.util.Collections;
3233
import java.util.HashMap;
@@ -396,6 +397,15 @@ public class ProGuardMojo extends AbstractMojo {
396397

397398
private Log log;
398399

400+
/**
401+
* ProGuard filter which excludes the {@code MANIFEST.MF} file
402+
*/
403+
private static final String MANIFEST_FILTER = "!META-INF/MANIFEST.MF";
404+
/**
405+
* ProGuard filter which excludes the Maven descriptors in the {@code META-INF/maven/} directory
406+
*/
407+
private static final String MAVEN_DESCRIPTORS_FILTER = "!META-INF/maven/**";
408+
399409
/**
400410
* ProGuard docs: Names with special characters like spaces and parentheses must be quoted with single or double
401411
* quotes.
@@ -408,14 +418,29 @@ private String fileToString(File file) {
408418
return fileNameToString(file.toString());
409419
}
410420

421+
/**
422+
* Creates a ProGuard classpath filter string.
423+
*/
424+
private String createFilterString(List<String> names) {
425+
if (names.isEmpty()) {
426+
return "";
427+
}
428+
429+
return "(" + String.join(",", names) + ")";
430+
}
431+
432+
private String createFilterString(String... names) {
433+
return createFilterString(Arrays.asList(names));
434+
}
435+
411436
private String libFileToStringWithInLibsFilter(File file) {
412437
return libFileToStringWithInLibsFilter(file.toString());
413438
}
414439

415440
private String libFileToStringWithInLibsFilter(String file) {
416441
StringBuilder filter = new StringBuilder(fileNameToString(file));
417442
if ((inLibsFilter != null)) {
418-
filter.append("(").append(inLibsFilter).append(")");
443+
filter.append(createFilterString(inLibsFilter));
419444
}
420445
return filter.toString();
421446
}
@@ -545,17 +570,15 @@ public void execute() throws MojoExecutionException, MojoFailureException {
545570
StringBuilder filter = new StringBuilder(fileToString(file));
546571
List<String> filterList = new ArrayList<>();
547572
if (!addManifest) {
548-
filterList.add("!META-INF/MANIFEST.MF");
573+
filterList.add(MANIFEST_FILTER);
549574
}
550575
if (!addMavenDescriptor) {
551-
filterList.add("!META-INF/maven/**");
576+
filterList.add(MAVEN_DESCRIPTORS_FILTER);
552577
}
553578
if (entry.getValue().filter != null) {
554579
filterList.add(entry.getValue().filter);
555580
}
556-
if (filterList.size() > 0){
557-
filter.append("(").append(String.join(",",filterList)).append( ")");
558-
}
581+
filter.append(createFilterString(filterList));
559582
args.add("-injars");
560583
args.add(filter.toString());
561584
}
@@ -578,27 +601,34 @@ public void execute() throws MojoExecutionException, MojoFailureException {
578601
args.add("-injars");
579602
StringBuilder filter = new StringBuilder(fileToString(inJarFile));
580603
if ((inFilter != null) || (!addMavenDescriptor)) {
581-
filter.append("(");
582-
boolean coma = false;
604+
List<String> filterList = new ArrayList<>();
583605

584606
if (!addMavenDescriptor) {
585-
coma = true;
586-
filter.append("!META-INF/maven/**");
607+
filterList.add(MAVEN_DESCRIPTORS_FILTER);
587608
}
588609

589610
if (inFilter != null) {
590-
if (coma) {
591-
filter.append(",");
592-
}
593-
filter.append(inFilter);
611+
filterList.add(inFilter);
594612
}
595613

596-
filter.append(")");
614+
filter.append(createFilterString(filterList));
597615
}
598616
args.add(filter.toString());
599617
}
600618

601619
if (includeDependency) {
620+
List<String> dependencyInjarFilterList = new ArrayList<>();
621+
if (!addManifest) {
622+
dependencyInjarFilterList.add(MANIFEST_FILTER);
623+
}
624+
if (!addMavenDescriptor) {
625+
dependencyInjarFilterList.add(MAVEN_DESCRIPTORS_FILTER);
626+
}
627+
if (inFilter != null) {
628+
dependencyInjarFilterList.add(inFilter);
629+
}
630+
String dependencyInjarFilter = createFilterString(dependencyInjarFilterList);
631+
602632
@SuppressWarnings("unchecked")
603633
List<Artifact> dependency = this.mavenProject.getCompileArtifacts();
604634
for (Artifact artifact : dependency) {
@@ -615,7 +645,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
615645
if (includeDependencyInjar) {
616646
log.debug("--- ADD library as injars:" + artifact.getArtifactId());
617647
args.add("-injars");
618-
args.add(fileToString(file));
648+
args.add(fileToString(file) + dependencyInjarFilter);
619649
} else {
620650
log.debug("--- ADD libraryjars:" + artifact.getArtifactId());
621651
if (putLibraryJarsInTempDir) {
@@ -632,7 +662,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
632662
args.add("-outjars");
633663
StringBuilder filter = new StringBuilder(fileToString(outJarFile));
634664
if (outFilter != null) {
635-
filter.append("(").append(outFilter).append(")");
665+
filter.append(createFilterString(outFilter));
636666
}
637667
args.add(filter.toString());
638668
}

0 commit comments

Comments
 (0)