26
26
import java .io .IOException ;
27
27
import java .net .URL ;
28
28
import java .util .ArrayList ;
29
+ import java .util .Collection ;
29
30
import java .util .Collections ;
30
31
import java .util .HashMap ;
31
32
import java .util .HashSet ;
33
+ import java .util .Iterator ;
32
34
import java .util .List ;
33
35
import java .util .Map ;
34
36
import java .util .Map .Entry ;
35
37
import java .util .Set ;
36
38
37
- import org .apache .commons .io .IOUtils ;
38
39
import org .apache .commons .io .FileUtils ;
40
+ import org .apache .commons .io .IOUtils ;
39
41
import org .apache .maven .archiver .MavenArchiveConfiguration ;
40
42
import org .apache .maven .archiver .MavenArchiver ;
41
43
import org .apache .maven .artifact .Artifact ;
@@ -130,7 +132,6 @@ public class ProGuardMojo extends AbstractMojo {
130
132
*/
131
133
private boolean putLibraryJarsInTempDir ;
132
134
133
-
134
135
/**
135
136
* Specifies that project compile dependencies should be added as injar.
136
137
*
@@ -298,7 +299,6 @@ public class ProGuardMojo extends AbstractMojo {
298
299
*/
299
300
private JarArchiver jarArchiver ;
300
301
301
-
302
302
/**
303
303
* The maven archive configuration to use. only if assembly is used.
304
304
*
@@ -395,6 +395,7 @@ private boolean useArtifactClassifier() {
395
395
return appendClassifier && ((attachArtifactClassifier != null ) && (attachArtifactClassifier .length () > 0 ));
396
396
}
397
397
398
+ @ Override
398
399
public void execute () throws MojoExecutionException , MojoFailureException {
399
400
400
401
log = getLog ();
@@ -564,7 +565,6 @@ public void execute() throws MojoExecutionException, MojoFailureException {
564
565
args .add (filter .toString ());
565
566
}
566
567
567
-
568
568
if (includeDependency ) {
569
569
@ SuppressWarnings ("unchecked" )
570
570
List <Artifact > dependency = this .mavenProject .getCompileArtifacts ();
@@ -633,15 +633,16 @@ public void execute() throws MojoExecutionException, MojoFailureException {
633
633
log .debug ("Copy libraryJars to temporary directory" );
634
634
log .debug ("Temporary directory: " + tempLibraryjarsDir );
635
635
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
+ }
641
641
}
642
642
tempLibraryjarsDir .mkdir ();
643
643
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 ());
645
646
}
646
647
// Use this subdirectory for all libraries that are files, and not directories themselves
647
648
File commonDir = new File (tempLibraryjarsDir , "0" );
@@ -673,7 +674,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
673
674
args .add (fileToString (mappingFile .getAbsoluteFile ()));
674
675
675
676
args .add ("-printseeds" );
676
- args .add (fileToString ((new File (outputDirectory ,seedFileName ).getAbsoluteFile ())));
677
+ args .add (fileToString ((new File (outputDirectory , seedFileName ).getAbsoluteFile ())));
677
678
678
679
if (incremental && applyMappingFile == null ) {
679
680
throw new MojoFailureException ("applyMappingFile is required if incremental is true" );
@@ -693,8 +694,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
693
694
}
694
695
695
696
log .info ("execute ProGuard " + args .toString ());
696
- proguardMain (getProguardJar (this ), args , this );
697
-
697
+ proguardMain (getProguardJars (this ), args , this );
698
698
699
699
if (!libraryJars .isEmpty ()) {
700
700
deleteFileOrDirectory (tempLibraryjarsDir );
@@ -787,26 +787,26 @@ public void execute() throws MojoExecutionException, MojoFailureException {
787
787
}
788
788
789
789
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" );
792
792
if (theFile .exists ()) {
793
793
if (theFile .isFile ()) {
794
794
projectHelper .attachArtifact (mavenProject , "txt" , classifier , theFile );
795
795
} 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 );
797
797
}
798
798
} 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 );
800
800
801
801
}
802
802
}
803
803
804
- private File getProguardJar (ProGuardMojo mojo ) throws MojoExecutionException {
804
+ private List < File > getProguardJars (ProGuardMojo mojo ) throws MojoExecutionException {
805
805
806
806
if (proguardJar != null ) {
807
807
if (proguardJar .exists ()) {
808
808
if (proguardJar .isFile ()) {
809
- return proguardJar ;
809
+ return Collections . singletonList ( proguardJar ) ;
810
810
} else {
811
811
mojo .getLog ().warn ("proguard jar (" + proguardJar + ") is not a file" );
812
812
throw new MojoExecutionException ("proguard jar (" + proguardJar + ") is not a file" );
@@ -817,42 +817,53 @@ private File getProguardJar(ProGuardMojo mojo) throws MojoExecutionException {
817
817
}
818
818
}
819
819
820
- Artifact proguardArtifact = null ;
820
+ List < Artifact > proguardArtifacts = new ArrayList < Artifact >() ;
821
821
int proguardArtifactDistance = -1 ;
822
822
// 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.
823
825
for (Artifact artifact : mojo .pluginArtifacts ) {
824
826
mojo .getLog ().debug ("pluginArtifact: " + artifact .getFile ());
825
827
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" )) {
828
830
int distance = artifact .getDependencyTrail ().size ();
829
831
mojo .getLog ().debug ("proguard DependencyTrail: " + distance );
830
832
if ((mojo .proguardVersion != null ) && (mojo .proguardVersion .equals (artifact .getVersion ()))) {
831
- proguardArtifact = artifact ;
832
- break ;
833
+ proguardArtifacts .add (artifact );
833
834
} else if (proguardArtifactDistance == -1 ) {
834
- proguardArtifact = artifact ;
835
+ proguardArtifacts . add ( artifact ) ;
835
836
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 );
838
845
proguardArtifactDistance = distance ;
839
846
}
840
847
}
841
848
}
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 ;
845
856
}
846
- mojo .getLog ().info ((useDexGuard ? "dexguard" : "proguard" ) + " jar not found in pluginArtifacts" );
857
+ mojo .getLog ().info ((useDexGuard ? "dexguard" : "proguard" ) + " jar not found in pluginArtifacts" );
847
858
848
859
ClassLoader cl ;
849
860
cl = mojo .getClass ().getClassLoader ();
850
861
// cl = Thread.currentThread().getContextClassLoader();
851
862
String classResource = "/" + mojo .proguardMainClass .replace ('.' , '/' ) + ".class" ;
852
863
URL url = cl .getResource (classResource );
853
864
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" );
856
867
}
857
868
String proguardJar = url .toExternalForm ();
858
869
if (proguardJar .startsWith ("jar:file:" )) {
@@ -861,10 +872,10 @@ private File getProguardJar(ProGuardMojo mojo) throws MojoExecutionException {
861
872
} else {
862
873
throw new MojoExecutionException ("Unrecognized location (" + proguardJar + ") in classpath" );
863
874
}
864
- return new File (proguardJar );
875
+ return Collections . singletonList ( new File (proguardJar ) );
865
876
}
866
877
867
- private void proguardMain (File proguardJar , List <String > argsList , ProGuardMojo mojo )
878
+ private void proguardMain (Collection < File > proguardJars , List <String > argsList , ProGuardMojo mojo )
868
879
throws MojoExecutionException {
869
880
870
881
Java java = new Java ();
@@ -885,9 +896,10 @@ private void proguardMain(File proguardJar, List<String> argsList, ProGuardMojo
885
896
java .setProject (antProject );
886
897
java .setTaskName ("proguard" );
887
898
888
- mojo .getLog ().info ("proguard jar: " + proguardJar );
899
+ mojo .getLog ().info ("proguard jar: " + proguardJars );
889
900
890
- java .createClasspath ().setLocation (proguardJar );
901
+ for (File p : proguardJars )
902
+ java .createClasspath ().createPathElement ().setLocation (p );
891
903
// java.createClasspath().setPath(System.getProperty("java.class.path"));
892
904
java .setClassname (mojo .proguardMainClass );
893
905
@@ -940,7 +952,8 @@ private boolean deleteFileOrDirectory(File path) throws MojoFailureException {
940
952
}
941
953
}
942
954
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 {
944
957
@ SuppressWarnings ("unchecked" )
945
958
Set <Artifact > dependencies = mavenProject .getArtifacts ();
946
959
Set <Artifact > result = new HashSet <Artifact >();
0 commit comments