Skip to content

[MBUILDCACHE-123] remove duplicated codes by adding field cacheState to MojoParametersListener #209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,28 @@ public void execute(
// Forked execution should be thought as a part of originating mojo internal implementation
// If forkedExecution is detected, it means that originating mojo is not cached so forks should rerun too
boolean forkedExecution = lifecyclePhasesHelper.isForkedProject(project);
String projectName = getVersionlessProjectKey(project);
List<MojoExecution> cleanPhase = null;
if (source == Source.LIFECYCLE && !forkedExecution) {
cleanPhase = lifecyclePhasesHelper.getCleanSegment(project, mojoExecutions);
for (MojoExecution mojoExecution : cleanPhase) {
mojoExecutionRunner.run(mojoExecution);
}
if (!cacheIsDisabled) {
cacheState = cacheConfig.initialize();
if (cacheState == INITIALIZED) {
// change mojoListener cacheState to INITIALIZED
mojoListener.setCacheState(cacheState);
}
LOGGER.info("Cache is {} on project level for {}", cacheState, projectName);
} else {
LOGGER.info(
"Cache is explicitly disabled on project level for {}", getVersionlessProjectKey(project));
LOGGER.info("Cache is explicitly disabled on project level for {}", projectName);
}
cleanPhase = lifecyclePhasesHelper.getCleanSegment(project, mojoExecutions);
for (MojoExecution mojoExecution : cleanPhase) {
mojoExecutionRunner.run(mojoExecution);
}
if (cacheState == INITIALIZED || skipCache) {
result = cacheController.findCachedBuild(session, project, mojoExecutions, skipCache);
}
} else {
LOGGER.info("Cache is disabled on project level for {}", projectName);
}

boolean restorable = result.isSuccess() || result.isPartialSuccess();
Expand Down Expand Up @@ -163,9 +170,7 @@ public void execute(

if (cacheConfig.isFailFast() && !result.isSuccess() && !skipCache && !forkedExecution) {
throw new LifecycleExecutionException(
"Failed to restore project[" + getVersionlessProjectKey(project)
+ "] from cache, failing build.",
project);
"Failed to restore project[" + projectName + "] from cache, failing build.", project);
}
} catch (MojoExecutionException e) {
throw new LifecycleExecutionException(e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@
*/
package org.apache.maven.buildcache;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

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

private final CacheConfig cacheConfig;

@Inject
public MojoParametersListener(CacheConfig cacheConfig) {
this.cacheConfig = cacheConfig;
}
private CacheState cacheState = DISABLED;

@Override
public void beforeMojoExecution(MojoExecutionEvent event) {
final String executionKey = CacheUtils.mojoExecutionKey(event.getExecution());
LOGGER.debug(
"Starting mojo execution: {}, class: {}",
"Starting mojo execution: {}, class: {}, cacheState: {}",
executionKey,
event.getMojo().getClass());
final MavenProject project = event.getProject();
CacheState cacheState = DISABLED;
boolean cacheIsDisabled = MavenProjectInput.isCacheDisabled(project);
if (!cacheIsDisabled) {
cacheState = cacheConfig.initialize();
}
LOGGER.debug("cacheState: {}", cacheState);
event.getMojo().getClass(),
cacheState);
if (cacheState == INITIALIZED) {
final MavenProject project = event.getProject();
Map<String, MojoExecutionEvent> projectEvents = projectExecutions.get(project);
if (projectEvents == null) {
Map<String, MojoExecutionEvent> candidate = new ConcurrentHashMap<>();
Expand All @@ -99,4 +86,8 @@ public void afterExecutionFailure(MojoExecutionEvent event) {
public Map<String, MojoExecutionEvent> getProjectExecutions(MavenProject project) {
return projectExecutions.get(project);
}

public void setCacheState(CacheState cacheState) {
this.cacheState = cacheState;
}
}