代码之家  ›  专栏  ›  技术社区  ›  Sagnik Chatterjee

Spring Boot应用程序的Maven构建过程中出错

  •  0
  • Sagnik Chatterjee  · 技术社区  · 1 年前

    我有一个spring-boot应用程序示例,其中的主类注释为 @SpringBootApplication ,并且有一个控制器、一些服务、存储库类等 application.properties 具有PostgreSQL连接配置的文件,如下所示

    spring.application.name=my-application-name
    spring.datasource.platform=postgres
    spring.datasource.url=jdbc:postgresql://${db-host}:5432/${db-name}
    spring.datasource.username=${db-user}
    spring.datasource.password=${db-pwd}
    spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.postgis.PostgisDialect
    spring.datasource.hikari.type=com.zaxxer.hikari.HikariDataSource
    spring.datasource.hikari.maximumPoolSize=${db-connection-pool}
    

    在我的 pom.xml ,喜欢的 spring-boot-starter-data-jpa ,HikariCP,我的应用程序也正在启动。

    当我以清洁安装为目标进行Maven Build时,问题就出现了。不知道为什么,在maven构建过程中,我会因为以下原因而失败:

    Caused by: org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'spring.datasource.hikari.maximum-pool-size' to int
    Caused by: org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [int] for value '${db-connection-pool}'; nested exception is java.lang.NumberFormatException: For input string: "${db-connection-pool}"
    

    它似乎以某种方式将可配置值视为硬编码的字符串值。有人能帮助提供解决方案吗?

    1 回复  |  直到 1 年前
        1
  •  1
  •   Claus Nielsen    1 年前

    当您没有为配置参数提供值时,它会这样做,因此您需要提供这些值。 如果您使用spring初始值设定项来设置项目,它将生成一个测试,用于验证您的应用程序上下文是否加载。那个测试可能就是失败的原因。如果您不需要它,您也可以跳过运行测试,将其作为maven构建的一部分。

        2
  •  0
  •   Mar-Z    1 年前

    对提供和接受的答案的补充。

    注释掉测试注释只是一种变通方法。总有一天,你会想测试你的应用程序,并使用Maven Surfire插件运行测试。以下是如何为其提供环境变量。

    解决方案

    在中添加所有必需的环境变量 pom.xml 。像这样:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
            <environmentVariables>
                <db-host>localhost</db-host>
                <db-name>my-db-name</db-name>
                <db-user>my-db-user</db-user>
                <db-pwd>my-db-password</db-pwd>
                <db-connection-pool>100</db-connection-pool>
            </environmentVariables>
        </configuration>
    </plugin>