You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by gn...@apache.org on 2022/05/17 21:11:34 UTC

[maven-build-cache-extension] 02/02: Fix wagon transport when using webdav

This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-build-cache-extension.git

commit 7e4732d1eeb5b421d30d73cd3392ccdeb4341b88
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Tue May 17 21:23:51 2022 +0200

    Fix wagon transport when using webdav
---
 pom.xml                                            |  5 ++++
 .../buildcache/RemoteCacheRepositoryImpl.java      | 34 +++++++++++++++-------
 2 files changed, 28 insertions(+), 11 deletions(-)

diff --git a/pom.xml b/pom.xml
index deb17e9..c0b042f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -139,6 +139,11 @@ under the License.
             <version>3.0.2</version>
             <scope>compile</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.maven.wagon</groupId>
+            <artifactId>wagon-webdav-jackrabbit</artifactId>
+            <version>3.5.1</version>
+        </dependency>
         <dependency>
             <groupId>org.junit.jupiter</groupId>
             <artifactId>junit-jupiter-engine</artifactId>
diff --git a/src/main/java/org/apache/maven/buildcache/RemoteCacheRepositoryImpl.java b/src/main/java/org/apache/maven/buildcache/RemoteCacheRepositoryImpl.java
index b04e3e8..32138ac 100644
--- a/src/main/java/org/apache/maven/buildcache/RemoteCacheRepositoryImpl.java
+++ b/src/main/java/org/apache/maven/buildcache/RemoteCacheRepositoryImpl.java
@@ -22,6 +22,7 @@ import java.io.Closeable;
 import java.io.File;
 import java.io.IOException;
 import java.net.URI;
+import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.List;
 import java.util.Optional;
@@ -133,7 +134,7 @@ public class RemoteCacheRepositoryImpl implements RemoteCacheRepository, Closeab
     public void saveCacheReport( String buildId, MavenSession session, CacheReport cacheReport ) throws IOException
     {
         MavenProject rootProject = session.getTopLevelProject();
-        final String resourceUrl = cacheConfig.getUrl() + "/" + MavenProjectInput.CACHE_IMPLEMENTATION_VERSION
+        final String resourceUrl = MavenProjectInput.CACHE_IMPLEMENTATION_VERSION
                 + "/" + rootProject.getGroupId()
                 + "/" + rootProject.getArtifactId()
                 + "/" + buildId
@@ -159,14 +160,14 @@ public class RemoteCacheRepositoryImpl implements RemoteCacheRepository, Closeab
     {
         try
         {
-            LOGGER.info( "Downloading {}", url );
+            LOGGER.info( "Downloading {}", getFullUrl( url ) );
             GetTask task = new GetTask( new URI( url ) );
             transporter.get( task );
             return Optional.of( task.getDataBytes() );
         }
         catch ( Exception e )
         {
-            LOGGER.info( "Cannot download {}", url, e );
+            LOGGER.info( "Cannot download {}", getFullUrl( url ), e );
             return Optional.empty();
         }
     }
@@ -175,14 +176,14 @@ public class RemoteCacheRepositoryImpl implements RemoteCacheRepository, Closeab
     {
         try
         {
-            LOGGER.info( "Downloading {}", url );
+            LOGGER.info( "Downloading {}", getFullUrl( url ) );
             GetTask task = new GetTask( new URI( url ) ).setDataFile( target.toFile() );
             transporter.get( task );
             return true;
         }
         catch ( Exception e )
         {
-            LOGGER.info( "Cannot download {}: {}", url, e.toString() );
+            LOGGER.info( "Cannot download {}: {}", getFullUrl( url ), e.toString() );
             return false;
         }
     }
@@ -197,22 +198,28 @@ public class RemoteCacheRepositoryImpl implements RemoteCacheRepository, Closeab
 
     private String getResourceUrl( String filename, String groupId, String artifactId, String checksum )
     {
-        return cacheConfig.getUrl() + "/" + MavenProjectInput.CACHE_IMPLEMENTATION_VERSION + "/" + groupId + "/"
+        return MavenProjectInput.CACHE_IMPLEMENTATION_VERSION + "/" + groupId + "/"
                 + artifactId + "/" + checksum + "/" + filename;
     }
 
     private void putToRemoteCache( byte[] bytes, String url ) throws IOException
     {
+        Path tmp = Files.createTempFile( "mbce-", ".tmp" );
         try
         {
+            Files.write( tmp, bytes );
             PutTask put = new PutTask( new URI( url ) );
-            put.setDataBytes( bytes );
+            put.setDataFile( tmp.toFile() );
             transporter.put( put );
-            LOGGER.info( "Saved to remote cache {}", url );
+            LOGGER.info( "Saved to remote cache {}", getFullUrl( url ) );
         }
         catch ( Exception e )
         {
-            LOGGER.info( "Unable to save to remote cache {}", url, e );
+            LOGGER.info( "Unable to save to remote cache {}", getFullUrl( url ), e );
+        }
+        finally
+        {
+            Files.deleteIfExists( tmp );
         }
     }
 
@@ -223,11 +230,11 @@ public class RemoteCacheRepositoryImpl implements RemoteCacheRepository, Closeab
             PutTask put = new PutTask( new URI( url ) );
             put.setDataFile( file );
             transporter.put( put );
-            LOGGER.info( "Saved to remote cache {}", url );
+            LOGGER.info( "Saved to remote cache {}", getFullUrl( url ) );
         }
         catch ( Exception e )
         {
-            LOGGER.info( "Unable to save to remote cache {}", url, e );
+            LOGGER.info( "Unable to save to remote cache {}", getFullUrl( url ), e );
         }
     }
 
@@ -304,4 +311,9 @@ public class RemoteCacheRepositoryImpl implements RemoteCacheRepository, Closeab
         return report;
     }
 
+    private String getFullUrl( String url )
+    {
+        return cacheConfig.getUrl() + "/" + url;
+    }
+
 }