34
34
import org .apache .maven .project .MavenProject ;
35
35
36
36
import javax .inject .Inject ;
37
+ import java .io .BufferedWriter ;
38
+ import java .io .File ;
39
+ import java .io .IOException ;
40
+ import java .nio .file .*;
37
41
import java .util .*;
38
42
import java .util .regex .Pattern ;
39
43
44
+ import static java .nio .file .StandardOpenOption .APPEND ;
45
+
40
46
/**
41
47
* This class is responsible for starting docking containers in the pre-integration phase of the maven build. The goal
42
48
* is called "start-containers"
@@ -62,11 +68,15 @@ public StartContainerMojo(List<ContainerStartConfiguration> containers) {
62
68
@ Parameter (defaultValue = "${mojoExecution}" , readonly = true )
63
69
private MojoExecution mojoExecution ;
64
70
71
+ @ Parameter (defaultValue = "${project.build.directory}/docker-plugin/docker-mappings.properties" )
72
+ private File dockerMappingsFile ;
73
+
65
74
@ Override
66
75
public void doExecute () throws MojoExecutionException , MojoFailureException {
67
76
if (hasDuplicateIds () || hasInvalidLinks ()) {
68
77
return ;
69
78
}
79
+ generateMappingsFile ();
70
80
DockerProvider provider = getDockerProvider ();
71
81
for (ContainerStartConfiguration configuration : containers ) {
72
82
for (ContainerLink link : configuration .getLinks ()) {
@@ -84,6 +94,7 @@ public void doExecute() throws MojoExecutionException, MojoFailureException {
84
94
String containerId = container .getId ();
85
95
List <ExposedPort > exposedPorts = provider .getExposedPorts (containerId );
86
96
exposePortsToProject (configuration , exposedPorts );
97
+ writeListOfPortsToFile (configuration , exposedPorts );
87
98
getLog ().info (String .format ("Started container with id '%s'" , containerId ));
88
99
registerStartedContainer (configuration .getId (), container );
89
100
} catch (DockerException e ) {
@@ -98,15 +109,14 @@ public void doExecute() throws MojoExecutionException, MojoFailureException {
98
109
}
99
110
}
100
111
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 () {
104
116
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 () {
107
118
@ Override
108
- public void run ()
109
- {
119
+ public void run () {
110
120
cleanUpStartedContainers ();
111
121
}
112
122
}));
@@ -193,13 +203,39 @@ private boolean hasDuplicateIds() {
193
203
194
204
private void exposePortsToProject (ContainerStartConfiguration configuration , List <ExposedPort > exposedPorts ) {
195
205
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 ());
198
207
addPropertyToProject (prefix + "host" , port .getHost ());
199
208
addPropertyToProject (prefix + "port" , String .valueOf (port .getExternalPort ()));
200
209
});
201
210
}
202
211
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
+
203
239
private void replaceImageWithBuiltImageIdIfInternalId (ContainerStartConfiguration configuration ) {
204
240
Optional <BuiltImageInfo > builtImage = getBuiltImageForStartId (configuration .getImage ());
205
241
if (builtImage .isPresent ()) {
@@ -227,6 +263,10 @@ public void setMojoExecution(final MojoExecution mojoExecution) {
227
263
this .mojoExecution = mojoExecution ;
228
264
}
229
265
266
+ public void setDockerMappingsFile (File dockerMappingsFile ) {
267
+ this .dockerMappingsFile = dockerMappingsFile ;
268
+ }
269
+
230
270
private void addPropertyToProject (String key , String value ) {
231
271
getLog ().info (String .format ("Setting property '%s' to '%s'" , key , value ));
232
272
project .getProperties ().setProperty (key , value );
0 commit comments