Skip to content

Commit 7fd9397

Browse files
committed
remove duplicated codes by adding field cacheState to MojoParametersListener
1 parent dfc6cd9 commit 7fd9397

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

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

+10-6
Original file line numberDiff line numberDiff line change
@@ -112,21 +112,25 @@ public void execute(
112112
// Forked execution should be thought as a part of originating mojo internal implementation
113113
// If forkedExecution is detected, it means that originating mojo is not cached so forks should rerun too
114114
boolean forkedExecution = lifecyclePhasesHelper.isForkedProject(project);
115+
String projectName = getVersionlessProjectKey(project);
115116
List<MojoExecution> cleanPhase = null;
116117
if (source == Source.LIFECYCLE && !forkedExecution) {
117-
cleanPhase = lifecyclePhasesHelper.getCleanSegment(project, mojoExecutions);
118-
for (MojoExecution mojoExecution : cleanPhase) {
119-
mojoExecutionRunner.run(mojoExecution);
120-
}
121118
if (!cacheIsDisabled) {
122119
cacheState = cacheConfig.initialize();
120+
mojoListener.setCacheState(cacheState);
121+
LOGGER.info("Cache is {} on project level for {}", cacheState, projectName);
123122
} else {
124-
LOGGER.info(
125-
"Cache is explicitly disabled on project level for {}", getVersionlessProjectKey(project));
123+
LOGGER.info("Cache is explicitly disabled on project level for {}", projectName);
124+
}
125+
cleanPhase = lifecyclePhasesHelper.getCleanSegment(project, mojoExecutions);
126+
for (MojoExecution mojoExecution : cleanPhase) {
127+
mojoExecutionRunner.run(mojoExecution);
126128
}
127129
if (cacheState == INITIALIZED || skipCache) {
128130
result = cacheController.findCachedBuild(session, project, mojoExecutions, skipCache);
129131
}
132+
} else {
133+
LOGGER.info("Cache is disabled on project level for {}", projectName);
130134
}
131135

132136
boolean restorable = result.isSuccess() || result.isPartialSuccess();

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

+9-18
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,13 @@
1818
*/
1919
package org.apache.maven.buildcache;
2020

21-
import javax.inject.Inject;
2221
import javax.inject.Named;
2322
import javax.inject.Singleton;
2423

2524
import java.util.Map;
2625
import java.util.concurrent.ConcurrentHashMap;
2726
import java.util.concurrent.ConcurrentMap;
2827

29-
import org.apache.maven.buildcache.checksum.MavenProjectInput;
30-
import org.apache.maven.buildcache.xml.CacheConfig;
3128
import org.apache.maven.buildcache.xml.CacheState;
3229
import org.apache.maven.execution.MojoExecutionEvent;
3330
import org.apache.maven.execution.MojoExecutionListener;
@@ -52,28 +49,18 @@ public class MojoParametersListener implements MojoExecutionListener {
5249
private final ConcurrentMap<MavenProject, Map<String, MojoExecutionEvent>> projectExecutions =
5350
new ConcurrentHashMap<>();
5451

55-
private final CacheConfig cacheConfig;
56-
57-
@Inject
58-
public MojoParametersListener(CacheConfig cacheConfig) {
59-
this.cacheConfig = cacheConfig;
60-
}
52+
private CacheState cacheState = DISABLED;
6153

6254
@Override
6355
public void beforeMojoExecution(MojoExecutionEvent event) {
6456
final String executionKey = CacheUtils.mojoExecutionKey(event.getExecution());
6557
LOGGER.debug(
66-
"Starting mojo execution: {}, class: {}",
58+
"Starting mojo execution: {}, class: {}, cacheState: {}",
6759
executionKey,
68-
event.getMojo().getClass());
69-
final MavenProject project = event.getProject();
70-
CacheState cacheState = DISABLED;
71-
boolean cacheIsDisabled = MavenProjectInput.isCacheDisabled(project);
72-
if (!cacheIsDisabled) {
73-
cacheState = cacheConfig.initialize();
74-
}
75-
LOGGER.debug("cacheState: {}", cacheState);
60+
event.getMojo().getClass(),
61+
cacheState);
7662
if (cacheState == INITIALIZED) {
63+
final MavenProject project = event.getProject();
7764
Map<String, MojoExecutionEvent> projectEvents = projectExecutions.get(project);
7865
if (projectEvents == null) {
7966
Map<String, MojoExecutionEvent> candidate = new ConcurrentHashMap<>();
@@ -99,4 +86,8 @@ public void afterExecutionFailure(MojoExecutionEvent event) {
9986
public Map<String, MojoExecutionEvent> getProjectExecutions(MavenProject project) {
10087
return projectExecutions.get(project);
10188
}
89+
90+
public void setCacheState(CacheState cacheState) {
91+
this.cacheState = cacheState;
92+
}
10293
}

0 commit comments

Comments
 (0)