5

我正在开发针对 MySQL 数据库的 Spring Data JPA 项目,并且我想从 Maven 运行端到端集成测试。

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

我已经尝试使用 Maven 进行自动属性扩展,但我怀疑mysql.portmaven 属性只有在 spring 属性更新后才会得到解决。

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>

应用程序属性

mysql.port = @mysql.port@

当我运行我的测试时,我得到一个连接错误,当我检查时target/classes/application.properties我发现它@mysql.port@没有更新。

任何建议将不胜感激。

4

1 回答 1

2

@ spring-boot-starter-parent当您从;扩展时才有效 你没有显示你的相关部分pom.xmlreserve-network-port假设您这样做了process-sources,请尝试 process-resources. 当Maven复制资源时,很有可能reserve-network-port还没有运行。

如果你硬编码 3306 会发生什么application.properties

于 2017-08-27T21:17:49.910 回答