Skip to content

Commit 72eb0db

Browse files
Merge pull request #111 from MarcMil/proguard7_fix
Proguard7 fix
2 parents 55de8c8 + 89773ba commit 72eb0db

File tree

2 files changed

+65
-41
lines changed

2 files changed

+65
-41
lines changed

pom.xml

+14-3
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,23 @@
7474
</contributor>
7575
</contributors>
7676

77+
<repositories>
78+
<repository>
79+
<snapshots>
80+
<enabled>false</enabled>
81+
</snapshots>
82+
<id>bintray-guardsquare-proguard</id>
83+
<name>bintray</name>
84+
<url>https://dl.bintray.com/guardsquare/proguard</url>
85+
</repository>
86+
</repositories>
87+
7788
<dependencies>
7889

7990
<dependency>
80-
<groupId>net.sf.proguard</groupId>
91+
<groupId>com.guardsquare</groupId>
8192
<artifactId>proguard-base</artifactId>
82-
<version>6.1.1</version>
93+
<version>7.0.0</version>
8394
<scope>runtime</scope>
8495
<optional>true</optional>
8596
</dependency>
@@ -137,7 +148,7 @@
137148
<plugin>
138149
<groupId>org.apache.maven.plugins</groupId>
139150
<artifactId>maven-plugin-plugin</artifactId>
140-
<version>3.2</version>
151+
<version>3.6.0</version>
141152
</plugin>
142153

143154
<plugin>

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

+51-38
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,18 @@
2626
import java.io.IOException;
2727
import java.net.URL;
2828
import java.util.ArrayList;
29+
import java.util.Collection;
2930
import java.util.Collections;
3031
import java.util.HashMap;
3132
import java.util.HashSet;
33+
import java.util.Iterator;
3234
import java.util.List;
3335
import java.util.Map;
3436
import java.util.Map.Entry;
3537
import java.util.Set;
3638

37-
import org.apache.commons.io.IOUtils;
3839
import org.apache.commons.io.FileUtils;
40+
import org.apache.commons.io.IOUtils;
3941
import org.apache.maven.archiver.MavenArchiveConfiguration;
4042
import org.apache.maven.archiver.MavenArchiver;
4143
import org.apache.maven.artifact.Artifact;
@@ -130,7 +132,6 @@ public class ProGuardMojo extends AbstractMojo {
130132
*/
131133
private boolean putLibraryJarsInTempDir;
132134

133-
134135
/**
135136
* Specifies that project compile dependencies should be added as injar.
136137
*
@@ -298,7 +299,6 @@ public class ProGuardMojo extends AbstractMojo {
298299
*/
299300
private JarArchiver jarArchiver;
300301

301-
302302
/**
303303
* The maven archive configuration to use. only if assembly is used.
304304
*
@@ -395,6 +395,7 @@ private boolean useArtifactClassifier() {
395395
return appendClassifier && ((attachArtifactClassifier != null) && (attachArtifactClassifier.length() > 0));
396396
}
397397

398+
@Override
398399
public void execute() throws MojoExecutionException, MojoFailureException {
399400

400401
log = getLog();
@@ -564,7 +565,6 @@ public void execute() throws MojoExecutionException, MojoFailureException {
564565
args.add(filter.toString());
565566
}
566567

567-
568568
if (includeDependency) {
569569
@SuppressWarnings("unchecked")
570570
List<Artifact> dependency = this.mavenProject.getCompileArtifacts();
@@ -633,15 +633,16 @@ public void execute() throws MojoExecutionException, MojoFailureException {
633633
log.debug("Copy libraryJars to temporary directory");
634634
log.debug("Temporary directory: " + tempLibraryjarsDir);
635635
if (tempLibraryjarsDir.exists()) {
636-
try{
637-
FileUtils.deleteDirectory(tempLibraryjarsDir);
638-
} catch(IOException ignored){
639-
throw new MojoFailureException("Deleting failed libraryJars directory", ignored);
640-
}
636+
try {
637+
FileUtils.deleteDirectory(tempLibraryjarsDir);
638+
} catch (IOException ignored) {
639+
throw new MojoFailureException("Deleting failed libraryJars directory", ignored);
640+
}
641641
}
642642
tempLibraryjarsDir.mkdir();
643643
if (!tempLibraryjarsDir.exists()) {
644-
throw new MojoFailureException("Can't create temporary libraryJars directory: " + tempLibraryjarsDir.getAbsolutePath());
644+
throw new MojoFailureException(
645+
"Can't create temporary libraryJars directory: " + tempLibraryjarsDir.getAbsolutePath());
645646
}
646647
// Use this subdirectory for all libraries that are files, and not directories themselves
647648
File commonDir = new File(tempLibraryjarsDir, "0");
@@ -673,7 +674,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
673674
args.add(fileToString(mappingFile.getAbsoluteFile()));
674675

675676
args.add("-printseeds");
676-
args.add(fileToString((new File(outputDirectory,seedFileName).getAbsoluteFile())));
677+
args.add(fileToString((new File(outputDirectory, seedFileName).getAbsoluteFile())));
677678

678679
if (incremental && applyMappingFile == null) {
679680
throw new MojoFailureException("applyMappingFile is required if incremental is true");
@@ -693,8 +694,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
693694
}
694695

695696
log.info("execute ProGuard " + args.toString());
696-
proguardMain(getProguardJar(this), args, this);
697-
697+
proguardMain(getProguardJars(this), args, this);
698698

699699
if (!libraryJars.isEmpty()) {
700700
deleteFileOrDirectory(tempLibraryjarsDir);
@@ -787,26 +787,26 @@ public void execute() throws MojoExecutionException, MojoFailureException {
787787
}
788788

789789
private void attachTextFile(File theFile, String mainClassifier, String suffix) {
790-
final String classifier = (null == mainClassifier ? "" : mainClassifier+"-") + suffix;
791-
log.info("Attempting to attach "+suffix+" artifact");
790+
final String classifier = (null == mainClassifier ? "" : mainClassifier + "-") + suffix;
791+
log.info("Attempting to attach " + suffix + " artifact");
792792
if (theFile.exists()) {
793793
if (theFile.isFile()) {
794794
projectHelper.attachArtifact(mavenProject, "txt", classifier, theFile);
795795
} else {
796-
log.warn("Cannot attach file because it is not a file: "+theFile);
796+
log.warn("Cannot attach file because it is not a file: " + theFile);
797797
}
798798
} else {
799-
log.warn("Cannot attach file because it does not exist: "+theFile);
799+
log.warn("Cannot attach file because it does not exist: " + theFile);
800800

801801
}
802802
}
803803

804-
private File getProguardJar(ProGuardMojo mojo) throws MojoExecutionException {
804+
private List<File> getProguardJars(ProGuardMojo mojo) throws MojoExecutionException {
805805

806806
if (proguardJar != null) {
807807
if (proguardJar.exists()) {
808808
if (proguardJar.isFile()) {
809-
return proguardJar;
809+
return Collections.singletonList(proguardJar);
810810
} else {
811811
mojo.getLog().warn("proguard jar (" + proguardJar + ") is not a file");
812812
throw new MojoExecutionException("proguard jar (" + proguardJar + ") is not a file");
@@ -817,42 +817,53 @@ private File getProguardJar(ProGuardMojo mojo) throws MojoExecutionException {
817817
}
818818
}
819819

820-
Artifact proguardArtifact = null;
820+
List<Artifact> proguardArtifacts = new ArrayList<Artifact>();
821821
int proguardArtifactDistance = -1;
822822
// This should be solved in Maven 2.1
823+
//Starting in v. 7.0.0., proguard got split up in proguard-base and proguard-core,
824+
//both of which need to be on the classpath.
823825
for (Artifact artifact : mojo.pluginArtifacts) {
824826
mojo.getLog().debug("pluginArtifact: " + artifact.getFile());
825827
final String artifactId = artifact.getArtifactId();
826-
if (artifactId.startsWith((useDexGuard?"dexguard":"proguard")) &&
827-
!artifactId.startsWith("proguard-maven-plugin")) {
828+
if (artifactId.startsWith((useDexGuard ? "dexguard" : "proguard"))
829+
&& !artifactId.startsWith("proguard-maven-plugin")) {
828830
int distance = artifact.getDependencyTrail().size();
829831
mojo.getLog().debug("proguard DependencyTrail: " + distance);
830832
if ((mojo.proguardVersion != null) && (mojo.proguardVersion.equals(artifact.getVersion()))) {
831-
proguardArtifact = artifact;
832-
break;
833+
proguardArtifacts.add(artifact);
833834
} else if (proguardArtifactDistance == -1) {
834-
proguardArtifact = artifact;
835+
proguardArtifacts.add(artifact);
835836
proguardArtifactDistance = distance;
836-
} else if (distance < proguardArtifactDistance) {
837-
proguardArtifact = artifact;
837+
} else if (distance <= proguardArtifactDistance) {
838+
Iterator<Artifact> it = proguardArtifacts.iterator();
839+
while (it.hasNext()) {
840+
Artifact art = it.next();
841+
if (distance < art.getDependencyTrail().size())
842+
it.remove();
843+
}
844+
proguardArtifacts.add(artifact);
838845
proguardArtifactDistance = distance;
839846
}
840847
}
841848
}
842-
if (proguardArtifact != null) {
843-
mojo.getLog().debug("proguardArtifact: " + proguardArtifact.getFile());
844-
return proguardArtifact.getFile().getAbsoluteFile();
849+
if (!proguardArtifacts.isEmpty()) {
850+
List<File> resList = new ArrayList<File>(proguardArtifacts.size());
851+
for (Artifact p : proguardArtifacts) {
852+
mojo.getLog().debug("proguardArtifact: " + p.getFile());
853+
resList.add(p.getFile().getAbsoluteFile());
854+
}
855+
return resList;
845856
}
846-
mojo.getLog().info((useDexGuard?"dexguard":"proguard") + " jar not found in pluginArtifacts");
857+
mojo.getLog().info((useDexGuard ? "dexguard" : "proguard") + " jar not found in pluginArtifacts");
847858

848859
ClassLoader cl;
849860
cl = mojo.getClass().getClassLoader();
850861
// cl = Thread.currentThread().getContextClassLoader();
851862
String classResource = "/" + mojo.proguardMainClass.replace('.', '/') + ".class";
852863
URL url = cl.getResource(classResource);
853864
if (url == null) {
854-
throw new MojoExecutionException("Obfuscation failed ProGuard (" + mojo.proguardMainClass
855-
+ ") not found in classpath");
865+
throw new MojoExecutionException(
866+
"Obfuscation failed ProGuard (" + mojo.proguardMainClass + ") not found in classpath");
856867
}
857868
String proguardJar = url.toExternalForm();
858869
if (proguardJar.startsWith("jar:file:")) {
@@ -861,10 +872,10 @@ private File getProguardJar(ProGuardMojo mojo) throws MojoExecutionException {
861872
} else {
862873
throw new MojoExecutionException("Unrecognized location (" + proguardJar + ") in classpath");
863874
}
864-
return new File(proguardJar);
875+
return Collections.singletonList(new File(proguardJar));
865876
}
866877

867-
private void proguardMain(File proguardJar, List<String> argsList, ProGuardMojo mojo)
878+
private void proguardMain(Collection<File> proguardJars, List<String> argsList, ProGuardMojo mojo)
868879
throws MojoExecutionException {
869880

870881
Java java = new Java();
@@ -885,9 +896,10 @@ private void proguardMain(File proguardJar, List<String> argsList, ProGuardMojo
885896
java.setProject(antProject);
886897
java.setTaskName("proguard");
887898

888-
mojo.getLog().info("proguard jar: " + proguardJar);
899+
mojo.getLog().info("proguard jar: " + proguardJars);
889900

890-
java.createClasspath().setLocation(proguardJar);
901+
for (File p : proguardJars)
902+
java.createClasspath().createPathElement().setLocation(p);
891903
// java.createClasspath().setPath(System.getProperty("java.class.path"));
892904
java.setClassname(mojo.proguardMainClass);
893905

@@ -940,7 +952,8 @@ private boolean deleteFileOrDirectory(File path) throws MojoFailureException {
940952
}
941953
}
942954

943-
private Set<Artifact> getDependencies(final Inclusion inc, MavenProject mavenProject) throws MojoExecutionException {
955+
private Set<Artifact> getDependencies(final Inclusion inc, MavenProject mavenProject)
956+
throws MojoExecutionException {
944957
@SuppressWarnings("unchecked")
945958
Set<Artifact> dependencies = mavenProject.getArtifacts();
946959
Set<Artifact> result = new HashSet<Artifact>();

0 commit comments

Comments
 (0)