Java 如何使用应用程序属性和pom文件共享/传递变量

Java 如何使用应用程序属性和pom文件共享/传递变量,java,maven,spring-boot,spring-boot-maven-plugin,Java,Maven,Spring Boot,Spring Boot Maven Plugin,我有一个多模块spring boot项目,在对我的应用程序进行集成测试之前,我启动了另一个子模块(它是另一个spring boot应用程序制作的存根),您可以看到它被附加到“预集成测试”中,它最终运行良好 Parent Pom | |----myRealApp module(spring boot app) |----stub module(This is also a spring-boot app) 我的问题是,有没有办法随机分配和共享这个端口(不固定为8090),这样Jenkin

我有一个多模块spring boot项目,在对我的应用程序进行集成测试之前,我启动了另一个子模块(它是另一个spring boot应用程序制作的存根),您可以看到它被附加到“预集成测试”中,它最终运行良好

 Parent Pom
|   
|----myRealApp module(spring boot app)
|----stub module(This is also a spring-boot app)
我的问题是,有没有办法随机分配和共享这个端口(不固定为8090),这样Jenkins服务器上的并发构建就可以运行测试,而不会因为地址已经在使用而失败

我知道我可以在spring属性文件中生成随机数/端口。但是找不到办法把它传给Pom

应用程序测试。myRealApp的属性

 stub.port=8090
 stub.url=http://localhost:${stub.port}/stub/api/v1/domains/
      <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>${spring.boot.mainclass}</mainClass>
            </configuration>
            <executions>
                <execution>
                    <id>start-stub</id>
                    <configuration>
                        <arguments>
                            <argument>--server.port=8090</argument>
                        </arguments>
                        <mainClass>io.swagger.Stub</mainClass>
                        <classesDirectory>../my-stub/target/classes</classesDirectory>
                    </configuration>
                    <goals>
                        <goal>start</goal>
                    </goals>
                    <phase>pre-integration-test</phase>
                </execution>

                <execution>
                    <goals>
                        <goal>build-info</goal>
                    </goals>
                </execution>
            </executions>
      </plugin>
myRealApp的Pom:

 stub.port=8090
 stub.url=http://localhost:${stub.port}/stub/api/v1/domains/
      <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>${spring.boot.mainclass}</mainClass>
            </configuration>
            <executions>
                <execution>
                    <id>start-stub</id>
                    <configuration>
                        <arguments>
                            <argument>--server.port=8090</argument>
                        </arguments>
                        <mainClass>io.swagger.Stub</mainClass>
                        <classesDirectory>../my-stub/target/classes</classesDirectory>
                    </configuration>
                    <goals>
                        <goal>start</goal>
                    </goals>
                    <phase>pre-integration-test</phase>
                </execution>

                <execution>
                    <goals>
                        <goal>build-info</goal>
                    </goals>
                </execution>
            </executions>
      </plugin>

org.springframework.boot
springbootmaven插件
${spring.boot.mainclass}
起始存根
--server.port=8090
伊奥·斯威格·斯图克
../my stub/target/classes
开始
预集成测试
构建信息

我建议你根本不要随机化。我的建议是在POM和application-test.properties文件中参数化服务器端口,并根据Jenkins提供的一些变量设置一个值:例如,
BUILD\u NUMBER
,它在每次构建时都会递增,从而保证唯一性

但是,这有一个问题:您还需要将端口号括在有效边界内:TCP端口必须在1024和65535之间,但是
BUILD_number
根本不受限制


如何应对?我认为绑定到
初始化
阶段的简单Ant任务可以读取
构建编号
值,将其应用于一个简单的公式
1024+(构建编号%64512)
,并将其设置为最终端口号变量,这是您将在POM和application-test.properties文件中引用的文件。

我建议您根本不要随机化。我的建议是在POM和application-test.properties文件中参数化服务器端口,并根据Jenkins提供的一些变量设置一个值:例如,
BUILD\u NUMBER
,它在每次构建时都会递增,从而保证唯一性

但是,这有一个问题:您还需要将端口号括在有效边界内:TCP端口必须在1024和65535之间,但是
BUILD_number
根本不受限制


如何应对?我认为绑定到
初始化
阶段的简单Ant任务可以读取
构建编号
值,将其应用于一个简单的公式
1024+(构建编号%64512)
,并将其设置为最终端口号变量,这是您将在POM和application-test.properties文件中引用的文件。

您可以通过jenkins完成此操作

一旦分配了端口(比如说
HTTP\u port
),就可以将其作为命令行传递

-Dstub.port=$HTTP_PORT

你可以通过詹金斯做到

一旦分配了端口(比如说
HTTP\u port
),就可以将其作为命令行传递

-Dstub.port=$HTTP_PORT

我不清楚为什么你需要一个存根应用程序进行测试?通常在Spring Boot中,编写单元/集成测试非常容易。@khmarbaise一直说“Spring Boot编写测试非常容易”并不能解决任何问题。MockRestServerServer使用自定义ClientHttpRequestFactory配置RestTemplate,从而覆盖自定义requestFactory,后者具有超时和身份验证设置。你能解释一下你打算如何测试这些吗?我不清楚你为什么需要一个存根应用程序来测试?通常在Spring Boot中,编写单元/集成测试非常容易。@khmarbaise一直说“Spring Boot编写测试非常容易”并不能解决任何问题。MockRestServerServer使用自定义ClientHttpRequestFactory配置RestTemplate,从而覆盖自定义requestFactory,后者具有超时和身份验证设置。你能解释一下你打算如何测试这些吗?实际上没有使用这种方法,但这是最好的答案。实际上没有使用这种方法,但这是最好的答案。