Skip to content

Commit

Permalink
Add 'ports' configuration to maven plugin (#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
TadCordle authored Jun 26, 2018
1 parent 2df6685 commit db87171
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 1 deletion.
2 changes: 2 additions & 0 deletions jib-maven-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ All notable changes to this project will be documented in this file.

### Added

- `<container><ports>` parameter to define container's exposed ports (similar to Dockerfile `EXPOSE`) ([#383](https://github.com/GoogleContainerTools/jib/issues/383))

### Changed

- Fetches credentials from inferred credential helper before Docker config ([#401](https://github.com/GoogleContainerTools/jib/issues/401))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ public void execute() throws MojoExecutionException {
.setMainClass(mainClass)
.setJavaArguments(getArgs())
.setJvmFlags(getJvmFlags())
.setEnvironment(getEnvironment());
.setEnvironment(getEnvironment())
.setExposedPorts(getExposedPorts());
CacheConfiguration applicationLayersCacheConfiguration =
CacheConfiguration.forPath(mavenProjectProperties.getCacheDirectory());
buildConfigurationBuilder.setApplicationLayersCacheConfiguration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
.setJavaArguments(getArgs())
.setJvmFlags(getJvmFlags())
.setEnvironment(getEnvironment())
.setExposedPorts(getExposedPorts())
.setTargetFormat(ImageFormat.valueOf(getFormat()).getManifestTemplateClass());
CacheConfiguration applicationLayersCacheConfiguration =
CacheConfiguration.forPath(mavenProjectProperties.getCacheDirectory());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
.setJvmFlags(getJvmFlags())
.setMainClass(mainClass)
.setJavaArguments(getArgs())
.setExposedPorts(getExposedPorts())
.generate(Paths.get(targetDir));

mavenBuildLogger.info("Created Docker context at " + targetDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public static class ContainerParameters {
@Nullable
@Parameter(required = true)
private String format = "Docker";

@Parameter private List<String> ports = Collections.emptyList();
}

/**
Expand Down Expand Up @@ -194,6 +196,10 @@ List<String> getArgs() {
return container.args;
}

List<String> getExposedPorts() {
return container.ports;
}

String getFormat() {
return Preconditions.checkNotNull(container.format);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.nio.file.Path;
import org.apache.maven.it.VerificationException;
import org.apache.maven.it.Verifier;
import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.ClassRule;
import org.junit.Test;
Expand Down Expand Up @@ -54,6 +55,15 @@ private static String buildToDockerDaemonAndRun(Path projectRoot, String imageRe
verifier.executeGoal("jib:" + BuildDockerMojo.GOAL_NAME);
verifier.verifyErrorFreeLog();

Assert.assertThat(
new Command("docker", "inspect", imageReference).run(),
CoreMatchers.containsString(
" \"ExposedPorts\": {\n"
+ " \"1000/tcp\": {},\n"
+ " \"2000/udp\": {},\n"
+ " \"2001/udp\": {},\n"
+ " \"2002/udp\": {},\n"
+ " \"2003/udp\": {}"));
return new Command("docker", "run", imageReference).run();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ private static String buildAndRun(Path projectRoot, String imageReference)
timeOne > timeTwo);

new Command("docker", "pull", imageReference).run();
Assert.assertThat(
new Command("docker", "inspect", imageReference).run(),
CoreMatchers.containsString(
" \"ExposedPorts\": {\n"
+ " \"1000/tcp\": {},\n"
+ " \"2000/udp\": {},\n"
+ " \"2001/udp\": {},\n"
+ " \"2002/udp\": {},\n"
+ " \"2003/udp\": {}"));
return new Command("docker", "run", imageReference).run();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.nio.file.Path;
import org.apache.maven.it.VerificationException;
import org.apache.maven.it.Verifier;
import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.ClassRule;
import org.junit.Test;
Expand Down Expand Up @@ -50,6 +51,16 @@ public void testExecute() throws VerificationException, IOException, Interrupted

String imageName = "jib/integration-test";
new Command("docker", "build", "-t", imageName, dockerContextDirectory.toString()).run();
Assert.assertThat(
new Command("docker", "inspect", imageName).run(),
CoreMatchers.containsString(
" \"ExposedPorts\": {\n"
+ " \"1000/tcp\": {},\n"
+ " \"2000/udp\": {},\n"
+ " \"2001/udp\": {},\n"
+ " \"2002/udp\": {},\n"
+ " \"2003/udp\": {}"));

Assert.assertEquals(
"Hello, world. An argument.\n", new Command("docker", "run", imageName).run());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
<configuration>
<container>
<args>An argument.</args>
<ports>
<port>1000/tcp</port>
<port>2000-2003/udp</port>
</ports>
</container>
<!-- Does not have tests use user-level cache for base image layers. -->
<useOnlyProjectCache>true</useOnlyProjectCache>
Expand Down
6 changes: 6 additions & 0 deletions jib-maven-plugin/src/test/resources/projects/empty/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
<to>
<image>gcr.io/jib-integration-testing/emptyimage:maven</image>
</to>
<container>
<ports>
<port>1000/tcp</port>
<port>2000-2003/udp</port>
</ports>
</container>
<!-- Does not have tests use user-level cache for base image layers. -->
<useOnlyProjectCache>true</useOnlyProjectCache>
</configuration>
Expand Down
4 changes: 4 additions & 0 deletions jib-maven-plugin/src/test/resources/projects/simple/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
</to>
<container>
<args>An argument.</args>
<ports>
<port>1000/tcp</port>
<port>2000-2003/udp</port>
</ports>
</container>
<!-- Does not have tests use user-level cache for base image layers. -->
<useOnlyProjectCache>true</useOnlyProjectCache>
Expand Down

0 comments on commit db87171

Please sign in to comment.