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/19 08:03:47 UTC

[maven-build-cache-extension] branch master updated: Ability to specify the build cache location (#16)

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


The following commit(s) were added to refs/heads/master by this push:
     new 4794ac6  Ability to specify the build cache location (#16)
4794ac6 is described below

commit 4794ac677d2ef7c92d19237a46ff6559dc056618
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Thu May 19 10:03:43 2022 +0200

    Ability to specify the build cache location (#16)
    
    * add a configuration field for the build cache location
    * defaults to `build-cache` instead of `cache`
    * add the remote repository id in the build cache path
---
 .../maven/buildcache/LocalCacheRepositoryImpl.java | 28 ++++++++++++++++------
 .../apache/maven/buildcache/xml/CacheConfig.java   |  2 ++
 .../maven/buildcache/xml/CacheConfigImpl.java      |  8 ++++++-
 src/main/mdo/build-cache-config.mdo                |  6 +++++
 4 files changed, 36 insertions(+), 8 deletions(-)

diff --git a/src/main/java/org/apache/maven/buildcache/LocalCacheRepositoryImpl.java b/src/main/java/org/apache/maven/buildcache/LocalCacheRepositoryImpl.java
index f94190c..abbb41d 100644
--- a/src/main/java/org/apache/maven/buildcache/LocalCacheRepositoryImpl.java
+++ b/src/main/java/org/apache/maven/buildcache/LocalCacheRepositoryImpl.java
@@ -436,19 +436,28 @@ public class LocalCacheRepositoryImpl implements LocalCacheRepository
 
     private Path artifactCacheDir( MavenSession session, String groupId, String artifactId ) throws IOException
     {
-        final String localRepositoryRoot = session.getLocalRepository().getBasedir();
-        final Path path = Paths.get( localRepositoryRoot, "..", "cache", CACHE_IMPLEMENTATION_VERSION, groupId,
-                artifactId ).normalize();
-        if ( !Files.exists( path ) )
+        final Path vga = Paths.get( CACHE_IMPLEMENTATION_VERSION, groupId, artifactId );
+        final Path path = baseDir( session ).resolve( vga );
+        Files.createDirectories( path );
+        return path;
+    }
+
+    private Path baseDir( MavenSession session )
+    {
+        String loc = cacheConfig.getLocalRepositoryLocation();
+        if ( loc != null )
         {
-            Files.createDirectories( path );
+            return Paths.get( loc );
+        }
+        else
+        {
+            return Paths.get( session.getLocalRepository().getBasedir() ).getParent().resolve( "build-cache" );
         }
-        return path;
     }
 
     private Path remoteBuildPath( CacheContext context, String filename ) throws IOException
     {
-        return buildCacheDir( context ).resolve( filename );
+        return remoteBuildDir( context ).resolve( filename );
     }
 
     private Path localBuildPath( CacheContext context, String filename, boolean createDir ) throws IOException
@@ -461,6 +470,11 @@ public class LocalCacheRepositoryImpl implements LocalCacheRepository
         return localBuildDir.resolve( filename );
     }
 
+    private Path remoteBuildDir( CacheContext context ) throws IOException
+    {
+        return buildCacheDir( context ).resolve( cacheConfig.getId() );
+    }
+
     private Path localBuildDir( CacheContext context ) throws IOException
     {
         return buildCacheDir( context ).resolve( "local" );
diff --git a/src/main/java/org/apache/maven/buildcache/xml/CacheConfig.java b/src/main/java/org/apache/maven/buildcache/xml/CacheConfig.java
index c3090c4..066c0f4 100644
--- a/src/main/java/org/apache/maven/buildcache/xml/CacheConfig.java
+++ b/src/main/java/org/apache/maven/buildcache/xml/CacheConfig.java
@@ -98,6 +98,8 @@ public interface CacheConfig
 
     int getMaxLocalBuildsCached();
 
+    String getLocalRepositoryLocation();
+
     List<String> getAttachedOutputs();
 
     boolean adjustMetaInfVersion();
diff --git a/src/main/java/org/apache/maven/buildcache/xml/CacheConfigImpl.java b/src/main/java/org/apache/maven/buildcache/xml/CacheConfigImpl.java
index 313cf58..326e833 100644
--- a/src/main/java/org/apache/maven/buildcache/xml/CacheConfigImpl.java
+++ b/src/main/java/org/apache/maven/buildcache/xml/CacheConfigImpl.java
@@ -148,7 +148,6 @@ public class CacheConfigImpl implements org.apache.maven.buildcache.xml.CacheCon
                             {
                                 LOGGER.info( "Loading cache configuration from {}", configPath );
                                 cacheConfig = xmlService.loadCacheConfig( configPath.toFile() );
-                                fillWithDefaults( cacheConfig );
                             }
                             catch ( Exception e )
                             {
@@ -585,6 +584,13 @@ public class CacheConfigImpl implements org.apache.maven.buildcache.xml.CacheCon
         return getLocal().getMaxBuildsCached();
     }
 
+    @Override
+    public String getLocalRepositoryLocation()
+    {
+        checkInitializedState();
+        return getLocal().getLocation();
+    }
+
     @Override
     public List<String> getAttachedOutputs()
     {
diff --git a/src/main/mdo/build-cache-config.mdo b/src/main/mdo/build-cache-config.mdo
index 54c0430..e546db6 100644
--- a/src/main/mdo/build-cache-config.mdo
+++ b/src/main/mdo/build-cache-config.mdo
@@ -387,6 +387,12 @@ under the License.
         <class>
             <name>Local</name>
             <fields>
+                <field>
+                    <name>location</name>
+                    <type>String</type>
+                    <description>The base directory where the local cache is located.
+                        Defaults to {@code $\{localRepository}/../cache}.</description>
+                </field>
                 <field>
                     <name>maxBuildsCached</name>
                     <type>int</type>