1717import java .util .Collections ;
1818import java .util .HashMap ;
1919import java .util .Map ;
20+ import java .util .Objects ;
2021import java .util .Set ;
2122import java .util .Map .Entry ;
2223import java .util .jar .JarOutputStream ;
2324import java .util .zip .ZipEntry ;
2425import java .util .zip .ZipFile ;
2526
27+ import org .codehaus .groovy .runtime .InvokerHelper ;
2628import org .gradle .api .Task ;
29+ import org .gradle .api .file .RegularFile ;
30+ import org .gradle .api .provider .Provider ;
2731import org .gradle .api .tasks .TaskInputs ;
2832import org .gradle .api .file .FileTreeElement ;
2933import org .gradle .api .file .FileVisitDetails ;
3034import org .gradle .api .file .FileVisitor ;
31- import org .gradle .api .file .RegularFileProperty ;
3235import org .gradle .api .model .ObjectFactory ;
3336import org .gradle .api .provider .Property ;
3437import org .gradle .api .specs .Spec ;
@@ -48,7 +51,7 @@ public class SignTask implements PatternFilterable {
4851 private final Property <String > storePass ;
4952 private final Property <String > keyPass ;
5053 private final Property <String > keyStoreData ;
51- private final RegularFileProperty keyStoreFile ;
54+ private final Property < File > keyStoreFile ;
5255 private final PatternSet patternSet = new PatternSet ();
5356
5457 @ SuppressWarnings ("serial" )
@@ -65,7 +68,7 @@ public class SignTask implements PatternFilterable {
6568 this .storePass = objs .property (String .class );
6669 this .keyPass = objs .property (String .class );
6770 this .keyStoreData = objs .property (String .class );
68- this .keyStoreFile = objs .fileProperty ( );
71+ this .keyStoreFile = objs .property ( File . class );
6972
7073 this .parent .configure (new Closure <Object >(parent ) {
7174 @ SuppressWarnings ("unused" )
@@ -112,11 +115,32 @@ private boolean hasEnoughInfo() {
112115 (this .keyStoreData .isPresent () || this .keyStoreFile .isPresent ());
113116 }
114117
118+ @ SuppressWarnings ("unchecked" )
119+ private static File getTaskArchiveFile (Zip task ) {
120+ try {
121+ // Try getting the new thing first, to avoid potential deprecation methods
122+ Provider <RegularFile > archiveFile = (Provider <RegularFile >) Objects .requireNonNull (
123+ InvokerHelper .getProperty (task , "archiveFile" )
124+ );
125+ return archiveFile .get ().getAsFile ();
126+ } catch (Exception e ) {
127+ // In older gradle? hope the old thing works.
128+ try {
129+ return task .getArchivePath ();
130+ } catch (Exception suppressed ) {
131+ // Well, we tried. Throw the first exception since it's for the latest Gradle version.
132+ // Add the first exception so it's not lost in the stacktrace, just in case.
133+ e .addSuppressed (suppressed );
134+ throw new IllegalStateException ("Could not access the parent task's output file" , e );
135+ }
136+ }
137+ }
138+
115139 private <T extends Task > void sign (T task ) throws IOException {
116140 final Map <String , Entry <byte [], Long >> ignoredStuff = new HashMap <>();
117141
118142 File tmp = this .parent .getTemporaryDir ();
119- File output = this .parent . getArchiveFile (). get (). getAsFile ( );
143+ File output = getTaskArchiveFile ( this .parent );
120144 File original = new File (tmp , output .getName () + ".original" );
121145 Files .move (output .toPath (), original .toPath (), StandardCopyOption .REPLACE_EXISTING );
122146
@@ -132,7 +156,7 @@ private <T extends Task> void sign(T task) throws IOException {
132156 if (this .keyStoreFile .isPresent ()) {
133157 if (this .keyStoreData .isPresent ())
134158 throw new IllegalStateException ("Both KeyStoreFile and KeyStoreData can not be set at the same time" );
135- keyStore = this .keyStoreFile .get (). getAsFile () ;
159+ keyStore = this .keyStoreFile .get ();
136160 } else if (this .keyStoreData .isPresent ()) {
137161 byte [] data = Base64 .getDecoder ().decode (this .keyStoreData .get ().getBytes (StandardCharsets .UTF_8 ));
138162 keyStore = new File (tmp , "keystore" );
@@ -158,7 +182,7 @@ private <T extends Task> void sign(T task) throws IOException {
158182 }
159183
160184 if (!ignoredStuff .isEmpty ())
161- writeOutputJar (output , this .parent . getArchiveFile (). get (). getAsFile ( ), ignoredStuff );
185+ writeOutputJar (output , getTaskArchiveFile ( this .parent ), ignoredStuff );
162186 }
163187
164188 private void processInputJar (File input , File output , final Map <String , Entry <byte [], Long >> unsigned ) throws IOException {
0 commit comments