3434import org .apache .maven .project .MavenProject ;
3535
3636import javax .inject .Inject ;
37+ import java .io .BufferedWriter ;
38+ import java .io .File ;
39+ import java .io .IOException ;
40+ import java .nio .file .*;
3741import java .util .*;
3842import java .util .regex .Pattern ;
3943
44+ import static java .nio .file .StandardOpenOption .APPEND ;
45+
4046/**
4147 * This class is responsible for starting docking containers in the pre-integration phase of the maven build. The goal
4248 * is called "start-containers"
@@ -62,11 +68,15 @@ public StartContainerMojo(List<ContainerStartConfiguration> containers) {
6268 @ Parameter (defaultValue = "${mojoExecution}" , readonly = true )
6369 private MojoExecution mojoExecution ;
6470
71+ @ Parameter (defaultValue = "${project.build.directory}/docker-plugin/docker-mappings.properties" )
72+ private File dockerMappingsFile ;
73+
6574 @ Override
6675 public void doExecute () throws MojoExecutionException , MojoFailureException {
6776 if (hasDuplicateIds () || hasInvalidLinks ()) {
6877 return ;
6978 }
79+ generateMappingsFile ();
7080 DockerProvider provider = getDockerProvider ();
7181 for (ContainerStartConfiguration configuration : containers ) {
7282 for (ContainerLink link : configuration .getLinks ()) {
@@ -84,6 +94,7 @@ public void doExecute() throws MojoExecutionException, MojoFailureException {
8494 String containerId = container .getId ();
8595 List <ExposedPort > exposedPorts = provider .getExposedPorts (containerId );
8696 exposePortsToProject (configuration , exposedPorts );
97+ writeListOfPortsToFile (configuration , exposedPorts );
8798 getLog ().info (String .format ("Started container with id '%s'" , containerId ));
8899 registerStartedContainer (configuration .getId (), container );
89100 } catch (DockerException e ) {
@@ -98,15 +109,14 @@ public void doExecute() throws MojoExecutionException, MojoFailureException {
98109 }
99110 }
100111
101- /** Avoid dangling containers if the build is interrupted (e.g. via Ctrl+C) before the StopContainer mojo runs. */
102- private void addShutdownHookToCleanUpContainers ()
103- {
112+ /**
113+ * Avoid dangling containers if the build is interrupted (e.g. via Ctrl+C) before the StopContainer mojo runs.
114+ */
115+ private void addShutdownHookToCleanUpContainers () {
104116 getLog ().info ("Started containers will be forcibly cleaned up when the build finishes" );
105- Runtime .getRuntime ().addShutdownHook (new Thread (new Runnable ()
106- {
117+ Runtime .getRuntime ().addShutdownHook (new Thread (new Runnable () {
107118 @ Override
108- public void run ()
109- {
119+ public void run () {
110120 cleanUpStartedContainers ();
111121 }
112122 }));
@@ -193,13 +203,39 @@ private boolean hasDuplicateIds() {
193203
194204 private void exposePortsToProject (ContainerStartConfiguration configuration , List <ExposedPort > exposedPorts ) {
195205 exposedPorts .parallelStream ().forEach (port -> {
196- String prefix = String .format ("docker.containers.%s.ports.%s." ,
197- configuration .getId (), port .getContainerPort ());
206+ String prefix = String .format ("docker.containers.%s.ports.%s." , configuration .getId (), port .getContainerPort ());
198207 addPropertyToProject (prefix + "host" , port .getHost ());
199208 addPropertyToProject (prefix + "port" , String .valueOf (port .getExternalPort ()));
200209 });
201210 }
202211
212+ private void generateMappingsFile () throws MojoExecutionException {
213+ try {
214+ if (dockerMappingsFile .getParentFile () != null ) {
215+ Files .createDirectories (dockerMappingsFile .getParentFile ().toPath ());
216+ }
217+ Files .createFile (dockerMappingsFile .toPath ());
218+ addPropertyToProject ("docker.mappings.file" , dockerMappingsFile .getAbsolutePath ());
219+ } catch (IOException e ) {
220+ throw new MojoExecutionException ("Error generating docker mapping file: " , e );
221+ }
222+ }
223+
224+ private void writeListOfPortsToFile (ContainerStartConfiguration configuration , List <ExposedPort > exposedPorts ) throws MojoExecutionException {
225+ try (BufferedWriter writer = Files .newBufferedWriter (dockerMappingsFile .toPath (), APPEND )) {
226+ getLog ().info (String .format ("Writing properties for container '%s' to '%s'" , configuration .getId (), dockerMappingsFile .getName ()));
227+ for (ExposedPort port : exposedPorts ) {
228+ String prefix = String .format ("docker.containers.%s.ports.%s." , configuration .getId (), port .getContainerPort ());
229+ writer .write (prefix + "host=" + port .getHost ());
230+ writer .newLine ();
231+ writer .write (prefix + "port=" + String .valueOf (port .getExternalPort ()));
232+ writer .newLine ();
233+ }
234+ } catch (IOException e ) {
235+ throw new MojoExecutionException ("Error writing to docker mapping file: " , e );
236+ }
237+ }
238+
203239 private void replaceImageWithBuiltImageIdIfInternalId (ContainerStartConfiguration configuration ) {
204240 Optional <BuiltImageInfo > builtImage = getBuiltImageForStartId (configuration .getImage ());
205241 if (builtImage .isPresent ()) {
@@ -227,6 +263,10 @@ public void setMojoExecution(final MojoExecution mojoExecution) {
227263 this .mojoExecution = mojoExecution ;
228264 }
229265
266+ public void setDockerMappingsFile (File dockerMappingsFile ) {
267+ this .dockerMappingsFile = dockerMappingsFile ;
268+ }
269+
230270 private void addPropertyToProject (String key , String value ) {
231271 getLog ().info (String .format ("Setting property '%s' to '%s'" , key , value ));
232272 project .getProperties ().setProperty (key , value );
0 commit comments