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

时间:2018-08-20 16:09:18

标签: java maven spring-boot spring-boot-maven-plugin

我有一个多模块spring-boot项目,在对我的应用程序进行集成测试之前,我启动了另一个子模块(这是另一个spring boot应用程序制作的Stub),您可以看到它已附加到“ pre-integration-test”并且一切正常

 Parent Pom
|   
|----myRealApp module(spring boot app)
|----stub module(This is also a spring-boot app)

我的问题是,是否有一种方法可以随机化并共享此端口(不固定为8090),因此在Jenkins服务器上进行的并发构建可以运行测试并且不会失败,因为地址已在使用中。

我知道我可以在spring属性文件中生成随机数/端口。但是找不到将其传递给Pom的方法。

myRealApp的

application-test.properties

 stub.port=8090
 stub.url=http://localhost:${stub.port}/stub/api/v1/domains/

myRealApp的pom:

      <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>

2 个答案:

答案 0 :(得分:2)

您可以通过詹金斯Port Allocator Plugin

一旦分配了端口(让我们说$user->mentorAssigned()),就可以将其作为命令行传递

import pandas as pd
import numpy as np
import scipy.stats as sp

df = pd.DataFrame(np.random.randint(1,10,(5)))
df_w = df.rolling(window=3, min_periods=1)

m1 = df_w.apply(lambda x: np.mean(x))
m2 = df_w.mean()

s1 = df_w.apply(lambda x: np.std(x))
s2 = df_w.std()

sk1 = df_w.apply(lambda x: sp.skew(x))
sk2 = df_w.skew()

答案 1 :(得分:0)

我建议您不要完全随机化。我的建议是对POM和application-test.properties文件中的服务器端口进行参数设置,并根据一些詹金斯提供的变量设置一个值:例如BUILD_NUMBER,该变量在每次构建时都会增加,因此唯一性是保证。

但是,这存在一个问题:您还需要将端口号包含在有效边界之内:TCP端口必须在1024和65535之内,但是BUILD_NUMBER不限于全部。

如何应对?我认为绑定到initialize阶段的简单Ant任务可以读取BUILD_NUMBER的值,将其应用一个简单的公式1024+(BUILD_NUMBER % 64512)并将其设置为确定的端口号变量,即您将在POM和application-test.properties文件中进行引用。

相关问题