Skip to content

Commit

Permalink
refactor: extracting docker prepare execution in its own method
Browse files Browse the repository at this point in the history
Signed-off-by: Sun Tan <sutan@redhat.com>
  • Loading branch information
sunix committed Jan 20, 2022
1 parent c82d91d commit 5a4634b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
Expand Up @@ -416,12 +416,21 @@ public RuntimeMode getConfiguredRuntimeMode() {

@Override
public final void execute() throws MojoExecutionException, MojoFailureException {
init();
if (!canExecute()) {
log.info("`%s` goal is skipped.", mojoExecution.getMojoDescriptor().getFullGoalName());
return;
try {
init();
if (!canExecute()) {
log.info("`%s` goal is skipped.", mojoExecution.getMojoDescriptor().getFullGoalName());
return;
}
prepareExecution();
executeInternal();
} catch (IOException | DependencyResolutionRequiredException exp) {
logException(exp);
throw new MojoExecutionException(exp.getMessage());
} catch (MojoExecutionException exp) {
logException(exp);
throw exp;
}
doExecute();
}

protected void init() {
Expand All @@ -437,39 +446,29 @@ protected boolean canExecute() {
return !skip;
}

protected void doExecute() throws MojoExecutionException, MojoFailureException {
protected void prepareExecution() throws MojoExecutionException, MojoFailureException, DependencyResolutionRequiredException, IOException {
final boolean ansiRestore = Ansi.isEnabled();
try {
DockerAccess dockerAccess = null;
try {
javaProject = MavenUtil.convertMavenProjectToJKubeProject(project, session);
resources = updateResourceConfigNamespace(namespace, resources);
// The 'real' images configuration to use (configured images + externally resolved images)
if (isDockerAccessRequired()) {
DockerAccessFactory.DockerAccessContext dockerAccessContext = getDockerAccessContext();
dockerAccess = dockerAccessFactory.createDockerAccess(dockerAccessContext);
}
jkubeServiceHub = JKubeServiceHub.builder()
.log(log)
.configuration(initJKubeConfiguration())
.clusterAccess(clusterAccess)
.platformMode(getConfiguredRuntimeMode())
.dockerServiceHub(DockerServiceHub.newInstance(log, dockerAccess, logOutputSpecFactory))
.buildServiceConfig(buildServiceConfigBuilder().build())
.offline(offline)
.build();
resolvedImages = ConfigHelper.initImageConfiguration(apiVersion, getBuildTimestamp(getPluginContext(), CONTEXT_KEY_BUILD_TIMESTAMP, project.getBuild().getDirectory(), DOCKER_BUILD_TIMESTAMP), javaProject, images, imageConfigResolver, log, filter, this);
executeInternal();
} catch (IOException | DependencyResolutionRequiredException exp) {
logException(exp);
throw new MojoExecutionException(exp.getMessage());
} catch (MojoExecutionException exp) {
logException(exp);
throw exp;
} finally {
Optional.ofNullable(jkubeServiceHub).ifPresent(JKubeServiceHub::close);
javaProject = MavenUtil.convertMavenProjectToJKubeProject(project, session);
resources = updateResourceConfigNamespace(namespace, resources);
// The 'real' images configuration to use (configured images + externally resolved images)
if (isDockerAccessRequired()) {
DockerAccessFactory.DockerAccessContext dockerAccessContext = getDockerAccessContext();
dockerAccess = dockerAccessFactory.createDockerAccess(dockerAccessContext);
}
jkubeServiceHub = JKubeServiceHub.builder()
.log(log)
.configuration(initJKubeConfiguration())
.clusterAccess(clusterAccess)
.platformMode(getConfiguredRuntimeMode())
.dockerServiceHub(DockerServiceHub.newInstance(log, dockerAccess, logOutputSpecFactory))
.buildServiceConfig(buildServiceConfigBuilder().build())
.offline(offline)
.build();
resolvedImages = ConfigHelper.initImageConfiguration(apiVersion, getBuildTimestamp(getPluginContext(), CONTEXT_KEY_BUILD_TIMESTAMP, project.getBuild().getDirectory(), DOCKER_BUILD_TIMESTAMP), javaProject, images, imageConfigResolver, log, filter, this);
} finally {
Optional.ofNullable(jkubeServiceHub).ifPresent(JKubeServiceHub::close);
Ansi.setEnabled(ansiRestore);
}
}
Expand Down
Expand Up @@ -43,7 +43,8 @@ public class SkipGoalsTest {

public void setupBuildGoal() throws Exception {
doNothing().when(buildMojo).init();
doNothing().when(buildMojo).doExecute();
doNothing().when(buildMojo).prepareExecution();
doNothing().when(buildMojo).executeInternal();
when(mojoExecution.getMojoDescriptor().getFullGoalName()).thenReturn("k8s:build");
}

Expand All @@ -55,7 +56,7 @@ public void should_execute_build_goal_if_skip_false() throws Exception {
// when
buildMojo.execute();
// then
verify(buildMojo).doExecute();
verify(buildMojo).executeInternal();
}

@Test
Expand All @@ -66,7 +67,7 @@ public void should_log_informative_message_when_build_goal_is_skipped() throws E
// when
buildMojo.execute();
// then
verify(buildMojo, never()).doExecute();
verify(buildMojo, never()).executeInternal();
verify(log).info("`%s` goal is skipped.", "k8s:build");
}

Expand Down

0 comments on commit 5a4634b

Please sign in to comment.