Skip to content

Commit

Permalink
Fix fabric8io#1482: SkipPom is ignored by push goal
Browse files Browse the repository at this point in the history
Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia committed Feb 6, 2022
1 parent e8baa24 commit 29a6ea4
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 8 deletions.
1 change: 1 addition & 0 deletions doc/changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# ChangeLog

* **0.39-SNAPSHOT** :
- `skipPom` is ignored by "push" goal ([1482](https://github.com/fabric8io/docker-maven-plugin/issues/1482))
- Cleanup dangling images as a result of image tagging, auto-pulling a base image, or auto-pulling a cacheFrom image ([#1513](https://github.com/fabric8io/docker-maven-plugin/pull/1513)) @rkhmelichek

* **0.38.1** (2021-12-18):
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/io/fabric8/maven/docker/AbstractDockerMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ public abstract class AbstractDockerMojo extends AbstractMojo implements Context
@Parameter
private DockerMachineConfiguration machine;

@Parameter(defaultValue = "${project.packaging}", required = true)
protected String packaging;

@Parameter(property = "docker.skip.pom", defaultValue = "false")
protected boolean skipPom;

// Images resolved with external image resolvers and hooks for subclass to
// mangle the image configurations.
private List<ImageConfiguration> resolvedImages;
Expand Down Expand Up @@ -575,6 +581,10 @@ protected void pullImage(QueryService queryService, RegistryService registryServ
registryService.pullImageWithPolicy(imageName, pullManager, registryConfig, queryService.hasImage(imageName));
}

protected boolean shouldSkipPom() {
return skipPom && packaging.equalsIgnoreCase("pom");
}

private boolean containerMatchesPattern(Container container, Matcher imageNameMatcher, Matcher containerNameMatcher,
String patternConfigName) {
if (imageNameMatcher != null && container.getImage() != null && imageNameMatcher.reset(container.getImage())
Expand Down
8 changes: 1 addition & 7 deletions src/main/java/io/fabric8/maven/docker/BuildMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,9 @@ public class BuildMojo extends AbstractBuildSupportMojo {
@Parameter(property = "docker.skip.build", defaultValue = "false")
protected boolean skipBuild;

@Parameter(property = "docker.skip.pom", defaultValue = "false")
protected boolean skipPom;

@Parameter(property = "docker.name", defaultValue = "")
protected String name;

@Parameter(defaultValue = "${project.packaging}", required = true)
protected String packaging;

/**
* Skip Sending created tarball to docker daemon
*/
Expand Down Expand Up @@ -151,7 +145,7 @@ private void processImageConfig(ServiceHub hub, ImageConfiguration aImageConfig)
BuildImageConfiguration buildConfig = aImageConfig.getBuildConfiguration();

if (buildConfig != null) {
if(buildConfig.skip() || (skipPom && packaging.equalsIgnoreCase("pom"))) {
if (buildConfig.skip() || shouldSkipPom()) {
log.info("%s : Skipped building", aImageConfig.getDescription());
} else {
buildAndTag(hub, aImageConfig);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/fabric8/maven/docker/PushMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class PushMojo extends AbstractDockerMojo {
*/
@Override
public void executeInternal(ServiceHub hub) throws DockerAccessException, MojoExecutionException {
if (skipPush) {
if (skipPush || shouldSkipPom()) {
return;
}

Expand Down
112 changes: 112 additions & 0 deletions src/test/java/io/fabric8/maven/docker/PushMojoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package io.fabric8.maven.docker;

import io.fabric8.maven.docker.access.DockerAccessException;
import io.fabric8.maven.docker.config.ImageConfiguration;
import io.fabric8.maven.docker.service.RegistryService;
import mockit.Deencapsulation;
import mockit.Mocked;
import mockit.Tested;
import mockit.Verifications;
import org.apache.maven.plugin.MojoExecutionException;
import org.junit.Test;

import java.io.IOException;
import java.util.Collection;

public class PushMojoTest extends BaseMojoTest {
@Tested
private PushMojo pushMojo;

@Mocked
private RegistryService registryService;

@Test
public void executeInternal_whenSkipPomEnabledInPomPackaging_thenImagePushSkipped() throws MojoExecutionException, IOException {
Deencapsulation.setField(serviceHub, "registryService", registryService);
givenMavenProject(pushMojo);
givenPackaging("pom");
givenSkipPom(true);

whenMojoExecutes();

thenImageNotPushed();
}

@Test
public void executeInternal_whenPomPackaging_thenImageIsPushed() throws MojoExecutionException, IOException {
Deencapsulation.setField(serviceHub, "registryService", registryService);
givenMavenProject(pushMojo);
givenPackaging("pom");
givenSkipPom(false);

whenMojoExecutes();

thenImagePushed();
}

@Test
public void executeInternal_whenJarPackaging_thenImageIsPushed() throws MojoExecutionException, IOException {
Deencapsulation.setField(serviceHub, "registryService", registryService);
givenMavenProject(pushMojo);
givenPackaging("jar");

whenMojoExecutes();

thenImagePushed();
}

@Test
public void executeInternal_whenSkipEnabled_thenImageIsPushed() throws MojoExecutionException, IOException {
Deencapsulation.setField(serviceHub, "registryService", registryService);
givenMavenProject(pushMojo);
givenPackaging("jar");
givenSkipPush(true);

whenMojoExecutes();

thenImageNotPushed();
}

@Test
public void executeInternal_whenSkipDisabled_thenImageIsPushed() throws MojoExecutionException, IOException {
Deencapsulation.setField(serviceHub, "registryService", registryService);
givenMavenProject(pushMojo);
givenPackaging("jar");
givenSkipPush(false);

whenMojoExecutes();

thenImagePushed();
}

private void thenImagePushed() throws MojoExecutionException, DockerAccessException {
new Verifications() {{
registryService.pushImages((Collection<ImageConfiguration>) any, anyInt, (RegistryService.RegistryConfig)any, anyBoolean);
times = 1;
}};
}

private void thenImageNotPushed() throws DockerAccessException, MojoExecutionException {
new Verifications() {{
registryService.pushImages((Collection<ImageConfiguration>) any, anyInt, (RegistryService.RegistryConfig)any, anyBoolean);
times = 0;
}};
}

private void whenMojoExecutes() throws IOException, MojoExecutionException {
pushMojo.executeInternal(serviceHub);
}

private void givenPackaging(String packaging) {
Deencapsulation.setField(pushMojo, "packaging", packaging);
}

private void givenSkipPom(boolean skipPom) {
Deencapsulation.setField(pushMojo, "skipPom", skipPom);
}

private void givenSkipPush(boolean skip) {
Deencapsulation.setField(pushMojo, "skipPush", skip);
}

}

0 comments on commit 29a6ea4

Please sign in to comment.