【问题标题】:Pass port number from docker-maven-plugin to spring property将端口号从 docker-maven-plugin 传递给 spring 属性
【发布时间】:2018-02-05 03:32:37
【问题描述】:

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

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

我已经尝试过Automatic property expansion using Maven,但我怀疑mysql.port maven 属性只是在 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>

application.properties

mysql.port = @mysql.port@

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

任何建议将不胜感激。

【问题讨论】:

  • 如果您检查您提供的链接。您需要添加配置以使用 @ 作为示例中未配置的分隔符...

标签: spring maven spring-data-jpa maven-docker-plugin


【解决方案1】:

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

如果您在application.properties 中硬编码 3306 会发生什么?

【讨论】:

  • 谢谢,将reserve-network-port 移至process-sources 阶段就成功了。
猜你喜欢
  • 2015-03-18
  • 1970-01-01
  • 2020-07-15
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 2018-05-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多