首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将端口号从docker-maven-plugin传递到spring属性。

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

Stack Overflow用户
提问于 2017-08-27 20:52:11
回答 1查看 1.6K关注 0票数 5

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

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

我尝试过使用Maven的自动属性扩展,但我怀疑mysql.port maven属性只有在spring属性更新之后才能得到解析。

pom.xml

代码语言:javascript
复制
    <?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

代码语言:javascript
复制
mysql.port = @mysql.port@

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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-27 21:17:49

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

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45909245

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档