Skip to content

Commit 4794ac6

Browse files
authored
Ability to specify the build cache location (apache#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
1 parent b2b7a5d commit 4794ac6

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

src/main/java/org/apache/maven/buildcache/LocalCacheRepositoryImpl.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -436,19 +436,28 @@ private Path buildCacheDir( CacheContext context ) throws IOException
436436

437437
private Path artifactCacheDir( MavenSession session, String groupId, String artifactId ) throws IOException
438438
{
439-
final String localRepositoryRoot = session.getLocalRepository().getBasedir();
440-
final Path path = Paths.get( localRepositoryRoot, "..", "cache", CACHE_IMPLEMENTATION_VERSION, groupId,
441-
artifactId ).normalize();
442-
if ( !Files.exists( path ) )
439+
final Path vga = Paths.get( CACHE_IMPLEMENTATION_VERSION, groupId, artifactId );
440+
final Path path = baseDir( session ).resolve( vga );
441+
Files.createDirectories( path );
442+
return path;
443+
}
444+
445+
private Path baseDir( MavenSession session )
446+
{
447+
String loc = cacheConfig.getLocalRepositoryLocation();
448+
if ( loc != null )
443449
{
444-
Files.createDirectories( path );
450+
return Paths.get( loc );
451+
}
452+
else
453+
{
454+
return Paths.get( session.getLocalRepository().getBasedir() ).getParent().resolve( "build-cache" );
445455
}
446-
return path;
447456
}
448457

449458
private Path remoteBuildPath( CacheContext context, String filename ) throws IOException
450459
{
451-
return buildCacheDir( context ).resolve( filename );
460+
return remoteBuildDir( context ).resolve( filename );
452461
}
453462

454463
private Path localBuildPath( CacheContext context, String filename, boolean createDir ) throws IOException
@@ -461,6 +470,11 @@ private Path localBuildPath( CacheContext context, String filename, boolean crea
461470
return localBuildDir.resolve( filename );
462471
}
463472

473+
private Path remoteBuildDir( CacheContext context ) throws IOException
474+
{
475+
return buildCacheDir( context ).resolve( cacheConfig.getId() );
476+
}
477+
464478
private Path localBuildDir( CacheContext context ) throws IOException
465479
{
466480
return buildCacheDir( context ).resolve( "local" );

src/main/java/org/apache/maven/buildcache/xml/CacheConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ public interface CacheConfig
9898

9999
int getMaxLocalBuildsCached();
100100

101+
String getLocalRepositoryLocation();
102+
101103
List<String> getAttachedOutputs();
102104

103105
boolean adjustMetaInfVersion();

src/main/java/org/apache/maven/buildcache/xml/CacheConfigImpl.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ public CacheState initialize()
148148
{
149149
LOGGER.info( "Loading cache configuration from {}", configPath );
150150
cacheConfig = xmlService.loadCacheConfig( configPath.toFile() );
151-
fillWithDefaults( cacheConfig );
152151
}
153152
catch ( Exception e )
154153
{
@@ -585,6 +584,13 @@ public int getMaxLocalBuildsCached()
585584
return getLocal().getMaxBuildsCached();
586585
}
587586

587+
@Override
588+
public String getLocalRepositoryLocation()
589+
{
590+
checkInitializedState();
591+
return getLocal().getLocation();
592+
}
593+
588594
@Override
589595
public List<String> getAttachedOutputs()
590596
{

src/main/mdo/build-cache-config.mdo

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,12 @@ under the License.
387387
<class>
388388
<name>Local</name>
389389
<fields>
390+
<field>
391+
<name>location</name>
392+
<type>String</type>
393+
<description>The base directory where the local cache is located.
394+
Defaults to {@code $\{localRepository}/../cache}.</description>
395+
</field>
390396
<field>
391397
<name>maxBuildsCached</name>
392398
<type>int</type>

0 commit comments

Comments
 (0)