title: Pass port number from docker-maven-plugin to spring property
body: <p>I'm developing Spring Data JPA project that targets a MySQL database, and I want to run end-to-end integration tests from Maven.</p>
  
  <p>So far, I've configured <code>io.fabric8.docker-maven-plugin</code> to spin up a MySQL container during <code>pre-integration-test</code> phase. It will use a random available port, which I need to pass to my <code>application.properties</code> file.</p>
  
  <p>I've tried <a href="https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html" rel="nofollow noreferrer">Automatic property expansion using Maven</a> but I suspect that the <code>mysql.port</code> maven property is only getting resolved <em>after</em> the spring properties are getting updated.</p>
  
  <p>pom.xml</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;example&lt;/groupId&gt;
      &lt;artifactId&gt;pass-port-number-from-docker-maven-plugin-to-spring-property&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;packaging&gt;jar&lt;/packaging&gt;
  
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;1.5.6.RELEASE&lt;/version&gt;
          &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
      &lt;/parent&gt;
  
      &lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;!-- other jpa dependencies ... --&gt;
  
      &lt;/dependencies&gt;
  
      &lt;build&gt;
  
          &lt;resources&gt;
              &lt;resource&gt;
                  &lt;directory&gt;src/main/resources&lt;/directory&gt;
                  &lt;filtering&gt;true&lt;/filtering&gt;
              &lt;/resource&gt;
          &lt;/resources&gt;
  
          &lt;plugins&gt;
  
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
  
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-failsafe-plugin&lt;/artifactId&gt;
                  &lt;executions&gt;
                  &lt;execution&gt;
                      &lt;goals&gt;
                      &lt;goal&gt;integration-test&lt;/goal&gt;
                      &lt;goal&gt;verify&lt;/goal&gt;
                      &lt;/goals&gt;
                  &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
  
          &lt;/plugins&gt;
      &lt;/build&gt;
  
      &lt;profiles&gt;
          &lt;profile&gt;
              &lt;id&gt;docker-test&lt;/id&gt;
              &lt;properties&gt;
                  &lt;docker-maven.version&gt;0.21.0&lt;/docker-maven.version&gt;
              &lt;/properties&gt;
              &lt;build&gt;
                  &lt;resources&gt;
                      &lt;resource&gt;
                          &lt;directory&gt;src/main/resources&lt;/directory&gt;
                          &lt;filtering&gt;true&lt;/filtering&gt;
                      &lt;/resource&gt;
                  &lt;/resources&gt;
                  &lt;plugins&gt;
  
                      &lt;plugin&gt;
                          &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
                          &lt;artifactId&gt;build-helper-maven-plugin&lt;/artifactId&gt;
                          &lt;version&gt;3.0.0&lt;/version&gt;
                          &lt;executions&gt;
                          &lt;execution&gt;
                              &lt;id&gt;reserve-network-port&lt;/id&gt;
                              &lt;goals&gt;
                              &lt;goal&gt;reserve-network-port&lt;/goal&gt;
                              &lt;/goals&gt;
                              &lt;phase&gt;process-resources&lt;/phase&gt;
                              &lt;configuration&gt;
                              &lt;portNames&gt;
                                  &lt;portName&gt;mysql.port&lt;/portName&gt;
                              &lt;/portNames&gt;
                              &lt;/configuration&gt;
                          &lt;/execution&gt;
                          &lt;/executions&gt;
                      &lt;/plugin&gt;
  
                      &lt;plugin&gt;
                      &lt;groupId&gt;io.fabric8&lt;/groupId&gt;
                      &lt;artifactId&gt;docker-maven-plugin&lt;/artifactId&gt;
                      &lt;version&gt;${docker-maven.version}&lt;/version&gt;
  
                      &lt;configuration&gt;
                          &lt;images&gt;
                              &lt;image&gt;
                              &lt;alias&gt;mysql&lt;/alias&gt;
                              &lt;name&gt;mysql:5.7&lt;/name&gt;
                              &lt;run&gt;
                                  &lt;env&gt;
                                      &lt;MYSQL_ROOT_PASSWORD&gt;my-secret-pw&lt;/MYSQL_ROOT_PASSWORD&gt;
                                  &lt;/env&gt;
                                  &lt;ports&gt;
                                      &lt;port&gt;mysql.port:3306&lt;/port&gt;
                                  &lt;/ports&gt;
                                  &lt;wait&gt;
                                      &lt;log&gt;ready for connections&lt;/log&gt;
                                      &lt;!-- &lt;time&gt;20000&lt;/time&gt; --&gt;
                                  &lt;/wait&gt;
                                  &lt;log&gt;
                                      &lt;prefix&gt;mysql&lt;/prefix&gt;
                                      &lt;date&gt;ISO8601&lt;/date&gt;
                                      &lt;color&gt;blue&lt;/color&gt;
                                  &lt;/log&gt;
                              &lt;/run&gt;
                              &lt;/image&gt;
                          &lt;/images&gt;
                      &lt;/configuration&gt;
  
                      &lt;!-- Connect start/stop to pre- and
                          post-integration-test phase, respectively if you want to start
                          your docker containers during integration tests --&gt;
                      &lt;executions&gt;
                          &lt;execution&gt;
                          &lt;id&gt;start&lt;/id&gt;
                          &lt;phase&gt;pre-integration-test&lt;/phase&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;start&lt;/goal&gt;
                          &lt;/goals&gt;
                          &lt;/execution&gt;
                          &lt;execution&gt;
                          &lt;id&gt;stop&lt;/id&gt;
                          &lt;phase&gt;post-integration-test&lt;/phase&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;stop&lt;/goal&gt;
                          &lt;/goals&gt;
                          &lt;/execution&gt;
                      &lt;/executions&gt;
                      &lt;/plugin&gt;
                  &lt;/plugins&gt;
              &lt;/build&gt;
          &lt;/profile&gt;
      &lt;/profiles&gt;
  
  &lt;/project&gt;
  </code></pre>
  
  <p>application.properties</p>
  
  <pre><code>mysql.port = @mysql.port@
  </code></pre>
  
  <p>When I run my test I get a connection error, and when I inspect <code>target/classes/application.properties</code> I see that <code>@mysql.port@</code> hasn't been updated.</p>
  
  <p>Any suggestions would be much appreciated.</p>