From c8438fb214a3b93d5c96cbf56aa8d4409522079c Mon Sep 17 00:00:00 2001 From: Marco Semiao Date: Fri, 4 Jul 2025 15:58:39 +0200 Subject: [PATCH 1/2] bugfix : sometimes MavenSession is not started when Guice Injection --- .../apache/maven/buildcache/CacheControllerImpl.java | 11 +++++++---- .../maven/buildcache/DefaultMultiModuleSupport.java | 11 ++++++++--- .../buildcache/DefaultProjectInputCalculator.java | 8 +++++--- .../apache/maven/buildcache/xml/CacheConfigImpl.java | 10 +++++++--- .../maven/buildcache/xml/CacheConfigImplTest.java | 4 +++- 5 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java b/src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java index 5a774735..832c399a 100644 --- a/src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java +++ b/src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java @@ -54,6 +54,7 @@ import java.util.function.Function; import java.util.regex.Pattern; +import com.google.inject.Provider; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.mutable.MutableBoolean; @@ -126,7 +127,7 @@ public class CacheControllerImpl implements CacheController { private final LocalCacheRepository localCache; private final RemoteCacheRepository remoteCache; private final ConcurrentMap cacheResults = new ConcurrentHashMap<>(); - private final LifecyclePhasesHelper lifecyclePhasesHelper; + private final Provider providerLifecyclePhasesHelper; private volatile Map projectIndex; private final ProjectInputCalculator projectInputCalculator; private final RestoredArtifactHandler restoreArtifactHandler; @@ -152,8 +153,7 @@ public CacheControllerImpl( CacheConfig cacheConfig, ProjectInputCalculator projectInputCalculator, RestoredArtifactHandler restoreArtifactHandler, - LifecyclePhasesHelper lifecyclePhasesHelper, - MavenSession session) { + Provider providerLifecyclePhasesHelper) { // CHECKSTYLE_OFF: ParameterNumber this.projectHelper = projectHelper; this.localCache = localCache; @@ -161,7 +161,7 @@ public CacheControllerImpl( this.cacheConfig = cacheConfig; this.artifactHandlerManager = artifactHandlerManager; this.xmlService = xmlService; - this.lifecyclePhasesHelper = lifecyclePhasesHelper; + this.providerLifecyclePhasesHelper = providerLifecyclePhasesHelper; this.projectInputCalculator = projectInputCalculator; this.restoreArtifactHandler = restoreArtifactHandler; } @@ -170,6 +170,7 @@ public CacheControllerImpl( @Nonnull public CacheResult findCachedBuild( MavenSession session, MavenProject project, List mojoExecutions, boolean skipCache) { + final LifecyclePhasesHelper lifecyclePhasesHelper = providerLifecyclePhasesHelper.get(); final String highestPhase = lifecyclePhasesHelper.resolveHighestLifecyclePhase(project, mojoExecutions); if (!lifecyclePhasesHelper.isLaterPhaseThanClean(highestPhase)) { @@ -256,6 +257,7 @@ private CacheResult analyzeResult(CacheContext context, List mojo build.getCacheImplementationVersion()); } + final LifecyclePhasesHelper lifecyclePhasesHelper = providerLifecyclePhasesHelper.get(); List cachedSegment = lifecyclePhasesHelper.getCachedSegment(context.getProject(), mojoExecutions, build); List missingMojos = build.getMissingExecutions(cachedSegment); @@ -295,6 +297,7 @@ private CacheResult analyzeResult(CacheContext context, List mojo } private boolean canIgnoreMissingSegment(MavenProject project, Build info, List mojoExecutions) { + final LifecyclePhasesHelper lifecyclePhasesHelper = providerLifecyclePhasesHelper.get(); final List postCachedSegment = lifecyclePhasesHelper.getPostCachedSegment(project, mojoExecutions, info); diff --git a/src/main/java/org/apache/maven/buildcache/DefaultMultiModuleSupport.java b/src/main/java/org/apache/maven/buildcache/DefaultMultiModuleSupport.java index 41cb0217..0d0d90e3 100644 --- a/src/main/java/org/apache/maven/buildcache/DefaultMultiModuleSupport.java +++ b/src/main/java/org/apache/maven/buildcache/DefaultMultiModuleSupport.java @@ -35,6 +35,7 @@ import java.util.function.Function; import java.util.stream.Collectors; +import com.google.inject.Provider; import org.apache.maven.SessionScoped; import org.apache.maven.buildcache.checksum.KeyUtils; import org.apache.maven.buildcache.xml.CacheConfig; @@ -58,21 +59,23 @@ public class DefaultMultiModuleSupport implements MultiModuleSupport { private final ProjectBuilder projectBuilder; private final CacheConfig cacheConfig; - private final MavenSession session; + private final Provider providerSession; private volatile boolean built; private volatile Map projectMap; private volatile Map sessionProjectMap; @Inject - public DefaultMultiModuleSupport(ProjectBuilder projectBuilder, CacheConfig cacheConfig, MavenSession session) { + public DefaultMultiModuleSupport( + ProjectBuilder projectBuilder, CacheConfig cacheConfig, Provider providerSession) { this.projectBuilder = projectBuilder; this.cacheConfig = cacheConfig; - this.session = session; + this.providerSession = providerSession; } @Override public boolean isPartOfSession(String groupId, String artifactId, String version) { + MavenSession session = providerSession.get(); return getProjectMap(session).containsKey(KeyUtils.getProjectKey(groupId, artifactId, version)); } @@ -85,6 +88,7 @@ public Optional tryToResolveProject(String groupId, String artifac @Override public boolean isPartOfMultiModule(String groupId, String artifactId, String version) { String projectKey = KeyUtils.getProjectKey(groupId, artifactId, version); + MavenSession session = providerSession.get(); return getProjectMap(session).containsKey(projectKey) || getMultiModuleProjectsMap().containsKey(projectKey); } @@ -102,6 +106,7 @@ private Map getMultiModuleProjectsMap() { if (projectMap != null) { return projectMap; } + MavenSession session = providerSession.get(); return getMultiModuleProjectsMapInner(session); } diff --git a/src/main/java/org/apache/maven/buildcache/DefaultProjectInputCalculator.java b/src/main/java/org/apache/maven/buildcache/DefaultProjectInputCalculator.java index 3e974fc0..315746ac 100644 --- a/src/main/java/org/apache/maven/buildcache/DefaultProjectInputCalculator.java +++ b/src/main/java/org/apache/maven/buildcache/DefaultProjectInputCalculator.java @@ -26,6 +26,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +import com.google.inject.Provider; import org.apache.maven.SessionScoped; import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager; import org.apache.maven.buildcache.checksum.MavenProjectInput; @@ -44,7 +45,7 @@ public class DefaultProjectInputCalculator implements ProjectInputCalculator { private static final Logger LOGGER = LoggerFactory.getLogger(DefaultProjectInputCalculator.class); - private final MavenSession mavenSession; + private final Provider providerSession; private final RemoteCacheRepository remoteCache; private final CacheConfig cacheConfig; private final RepositorySystem repoSystem; @@ -58,14 +59,14 @@ public class DefaultProjectInputCalculator implements ProjectInputCalculator { @Inject public DefaultProjectInputCalculator( - MavenSession mavenSession, + Provider providerSession, RemoteCacheRepository remoteCache, CacheConfig cacheConfig, RepositorySystem repoSystem, NormalizedModelProvider rawModelProvider, MultiModuleSupport multiModuleSupport, ArtifactHandlerManager artifactHandlerManager) { - this.mavenSession = mavenSession; + this.providerSession = providerSession; this.remoteCache = remoteCache; this.cacheConfig = cacheConfig; this.repoSystem = repoSystem; @@ -104,6 +105,7 @@ private ProjectsInputInfo calculateInputInternal(String key, MavenProject projec + ", setOfCalculatingProjects=" + projectsSet + "]"); } try { + final MavenSession mavenSession = providerSession.get(); final MavenProjectInput input = new MavenProjectInput( project, normalizedModelProvider, 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 566a067a..5a2c3cc0 100644 --- a/src/main/java/org/apache/maven/buildcache/xml/CacheConfigImpl.java +++ b/src/main/java/org/apache/maven/buildcache/xml/CacheConfigImpl.java @@ -32,6 +32,7 @@ import java.util.Optional; import java.util.regex.Pattern; +import com.google.inject.Provider; import org.apache.commons.lang3.StringUtils; import org.apache.maven.SessionScoped; import org.apache.maven.buildcache.DefaultPluginScanConfig; @@ -114,7 +115,7 @@ public class CacheConfigImpl implements org.apache.maven.buildcache.xml.CacheCon private static final Logger LOGGER = LoggerFactory.getLogger(CacheConfigImpl.class); private final XmlService xmlService; - private final MavenSession session; + private final Provider providerSession; private final RuntimeInformation rtInfo; private volatile CacheState state; @@ -123,9 +124,9 @@ public class CacheConfigImpl implements org.apache.maven.buildcache.xml.CacheCon private List excludePatterns; @Inject - public CacheConfigImpl(XmlService xmlService, MavenSession session, RuntimeInformation rtInfo) { + public CacheConfigImpl(XmlService xmlService, Provider providerSession, RuntimeInformation rtInfo) { this.xmlService = xmlService; - this.session = session; + this.providerSession = providerSession; this.rtInfo = rtInfo; } @@ -152,6 +153,7 @@ public CacheState initialize() { if (StringUtils.isNotBlank(configPathText)) { configPath = Paths.get(configPathText); } else { + final MavenSession session = providerSession.get(); configPath = getMultimoduleRoot(session).resolve(".mvn").resolve("maven-build-cache-config.xml"); } @@ -634,6 +636,7 @@ private void checkInitializedState() { } private String getProperty(String key, String defaultValue) { + MavenSession session = providerSession.get(); String value = session.getUserProperties().getProperty(key); if (value == null) { value = session.getSystemProperties().getProperty(key); @@ -645,6 +648,7 @@ private String getProperty(String key, String defaultValue) { } private boolean getProperty(String key, boolean defaultValue) { + MavenSession session = providerSession.get(); String value = session.getUserProperties().getProperty(key); if (value == null) { value = session.getSystemProperties().getProperty(key); diff --git a/src/test/java/org/apache/maven/buildcache/xml/CacheConfigImplTest.java b/src/test/java/org/apache/maven/buildcache/xml/CacheConfigImplTest.java index 2e06907c..1037675f 100644 --- a/src/test/java/org/apache/maven/buildcache/xml/CacheConfigImplTest.java +++ b/src/test/java/org/apache/maven/buildcache/xml/CacheConfigImplTest.java @@ -34,6 +34,7 @@ import java.util.Properties; import java.util.stream.Collectors; +import com.google.inject.Provider; import org.apache.commons.lang3.tuple.Pair; import org.apache.maven.buildcache.DefaultPluginScanConfig; import org.apache.maven.buildcache.hash.HashFactory; @@ -110,8 +111,9 @@ void setUp() throws IOException { testCacheConfig = new XmlService().loadCacheConfig("".getBytes()); when(xmlService.loadCacheConfig(rootConfigFile)).thenReturn(testCacheConfig); + Provider provider = (() -> mavenSession); // test object - testObject = new CacheConfigImpl(xmlService, mavenSession, rtInfo); + testObject = new CacheConfigImpl(xmlService, provider, rtInfo); } private static void deepMockConfigFile(File mockFile, boolean exists) throws IOException { From 93cbec9674d218de4f4228aa38569eff001b0f9d Mon Sep 17 00:00:00 2001 From: Marco Semiao Date: Fri, 4 Jul 2025 18:12:37 +0200 Subject: [PATCH 2/2] use javax.inject.Provider and not com.google.inject.Provider --- .../java/org/apache/maven/buildcache/CacheControllerImpl.java | 2 +- .../org/apache/maven/buildcache/DefaultMultiModuleSupport.java | 2 +- .../apache/maven/buildcache/DefaultProjectInputCalculator.java | 2 +- .../java/org/apache/maven/buildcache/xml/CacheConfigImpl.java | 2 +- .../org/apache/maven/buildcache/xml/CacheConfigImplTest.java | 3 ++- 5 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java b/src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java index 832c399a..1cb7ea94 100644 --- a/src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java +++ b/src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java @@ -21,6 +21,7 @@ import javax.annotation.Nonnull; import javax.inject.Inject; import javax.inject.Named; +import javax.inject.Provider; import java.io.File; import java.io.FileNotFoundException; @@ -54,7 +55,6 @@ import java.util.function.Function; import java.util.regex.Pattern; -import com.google.inject.Provider; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.mutable.MutableBoolean; diff --git a/src/main/java/org/apache/maven/buildcache/DefaultMultiModuleSupport.java b/src/main/java/org/apache/maven/buildcache/DefaultMultiModuleSupport.java index 0d0d90e3..358aaf84 100644 --- a/src/main/java/org/apache/maven/buildcache/DefaultMultiModuleSupport.java +++ b/src/main/java/org/apache/maven/buildcache/DefaultMultiModuleSupport.java @@ -20,6 +20,7 @@ import javax.inject.Inject; import javax.inject.Named; +import javax.inject.Provider; import java.io.File; import java.util.ArrayList; @@ -35,7 +36,6 @@ import java.util.function.Function; import java.util.stream.Collectors; -import com.google.inject.Provider; import org.apache.maven.SessionScoped; import org.apache.maven.buildcache.checksum.KeyUtils; import org.apache.maven.buildcache.xml.CacheConfig; diff --git a/src/main/java/org/apache/maven/buildcache/DefaultProjectInputCalculator.java b/src/main/java/org/apache/maven/buildcache/DefaultProjectInputCalculator.java index 315746ac..95852d99 100644 --- a/src/main/java/org/apache/maven/buildcache/DefaultProjectInputCalculator.java +++ b/src/main/java/org/apache/maven/buildcache/DefaultProjectInputCalculator.java @@ -20,13 +20,13 @@ import javax.inject.Inject; import javax.inject.Named; +import javax.inject.Provider; import java.util.LinkedHashSet; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; -import com.google.inject.Provider; import org.apache.maven.SessionScoped; import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager; import org.apache.maven.buildcache.checksum.MavenProjectInput; 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 5a2c3cc0..2a5fc703 100644 --- a/src/main/java/org/apache/maven/buildcache/xml/CacheConfigImpl.java +++ b/src/main/java/org/apache/maven/buildcache/xml/CacheConfigImpl.java @@ -22,6 +22,7 @@ import javax.annotation.Nullable; import javax.inject.Inject; import javax.inject.Named; +import javax.inject.Provider; import java.nio.file.Files; import java.nio.file.Path; @@ -32,7 +33,6 @@ import java.util.Optional; import java.util.regex.Pattern; -import com.google.inject.Provider; import org.apache.commons.lang3.StringUtils; import org.apache.maven.SessionScoped; import org.apache.maven.buildcache.DefaultPluginScanConfig; diff --git a/src/test/java/org/apache/maven/buildcache/xml/CacheConfigImplTest.java b/src/test/java/org/apache/maven/buildcache/xml/CacheConfigImplTest.java index 1037675f..6ec8892d 100644 --- a/src/test/java/org/apache/maven/buildcache/xml/CacheConfigImplTest.java +++ b/src/test/java/org/apache/maven/buildcache/xml/CacheConfigImplTest.java @@ -18,6 +18,8 @@ */ package org.apache.maven.buildcache.xml; +import javax.inject.Provider; + import java.io.File; import java.io.IOException; import java.lang.reflect.InvocationTargetException; @@ -34,7 +36,6 @@ import java.util.Properties; import java.util.stream.Collectors; -import com.google.inject.Provider; import org.apache.commons.lang3.tuple.Pair; import org.apache.maven.buildcache.DefaultPluginScanConfig; import org.apache.maven.buildcache.hash.HashFactory;