将端口号从 docker-maven-plugin 传递到 spring 属性

Pass port number from docker-maven-plugin to spring property

我正在开发 Spring 以 MySQL 数据库为目标的 Data JPA 项目,我想 运行 来自 Maven 的端到端集成测试。

到目前为止,我已将 io.fabric8.docker-maven-plugin 配置为在 pre-integration-test 阶段启动 MySQL 容器。它将使用随机可用端口,我需要将其传递到我的 application.properties 文件。

我试过 Automatic property expansion using Maven 但我怀疑 mysql.port maven 属性 仅在 [=43= 之后得到解决] 属性正在更新。

pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
<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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>example</groupId>
    <artifactId>pass-port-number-from-docker-maven-plugin-to-spring-property</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- other jpa dependencies ... -->

    </dependencies>

    <build>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>

        <plugins>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <executions>
                <execution>
                    <goals>
                    <goal>integration-test</goal>
                    <goal>verify</goal>
                    </goals>
                </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

    <profiles>
        <profile>
            <id>docker-test</id>
            <properties>
                <docker-maven.version>0.21.0</docker-maven.version>
            </properties>
            <build>
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                        <filtering>true</filtering>
                    </resource>
                </resources>
                <plugins>

                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>build-helper-maven-plugin</artifactId>
                        <version>3.0.0</version>
                        <executions>
                        <execution>
                            <id>reserve-network-port</id>
                            <goals>
                            <goal>reserve-network-port</goal>
                            </goals>
                            <phase>process-resources</phase>
                            <configuration>
                            <portNames>
                                <portName>mysql.port</portName>
                            </portNames>
                            </configuration>
                        </execution>
                        </executions>
                    </plugin>

                    <plugin>
                    <groupId>io.fabric8</groupId>
                    <artifactId>docker-maven-plugin</artifactId>
                    <version>${docker-maven.version}</version>

                    <configuration>
                        <images>
                            <image>
                            <alias>mysql</alias>
                            <name>mysql:5.7</name>
                            <run>
                                <env>
                                    <MYSQL_ROOT_PASSWORD>my-secret-pw</MYSQL_ROOT_PASSWORD>
                                </env>
                                <ports>
                                    <port>mysql.port:3306</port>
                                </ports>
                                <wait>
                                    <log>ready for connections</log>
                                    <!-- <time>20000</time> -->
                                </wait>
                                <log>
                                    <prefix>mysql</prefix>
                                    <date>ISO8601</date>
                                    <color>blue</color>
                                </log>
                            </run>
                            </image>
                        </images>
                    </configuration>

                    <!-- Connect start/stop to pre- and
                        post-integration-test phase, respectively if you want to start
                        your docker containers during integration tests -->
                    <executions>
                        <execution>
                        <id>start</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>start</goal>
                        </goals>
                        </execution>
                        <execution>
                        <id>stop</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                        </execution>
                    </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

</project>

application.properties

mysql.port = @mysql.port@

当我 运行 测试时出现连接错误,当我检查 target/classes/application.properties 时,我发现 @mysql.port@ 尚未更新。

如有任何建议,我们将不胜感激。

@ 只有 如果您从 spring-boot-starter-parent 扩展才有效;您没有显示 pom.xml 的相关部分。假设您这样做了,请尝试将 reserve-network-port 附加到 process-sources 阶段, process-resources 之前。很有可能Maven拷贝资源的时候reserve-network-port还没有运行

如果在 application.properties 中硬编码 3306 会怎样?