0

我在 pom.xml 文件中包含了我的数据库配置,以便将它们用于我的项目测试,我正在使用 unitils 进行数据库集成我的 pom.xml 属性是

    <test.database.rules-satellite.driver>com.mysql.jdbc.Driver</test.database.rulessatellite.driver>
    <test.database.rules-satellite.server>localhost</test.database.rules-satellite.server>
    <test.database.rules-satellite.schema>R133_isp_copie_pdx</test.database.rules-satellite.schema>
    <test.database.rules-satellite.url>jdbc:mysql://${test.database.rules-satellite.server}/${test.database.rules-satellite.schema}</test.database.rules-satellite.url>

    <test.database.rules-satellite.username>junit</test.database.rules-satellite.username>
    <test.database.rules-satellite.password>junit</test.database.rules-satellite.password>

我的unitils配置如下:

# Properties for the`enter code here` PropertiesDataSourceFactory
database.driverClassName=com.mysql.jdbc.Driver
database.url=jdbc:mysql://${test.database.rules-satellite.server}/${test.database.rules-satellite.schema}

# This property specifies the underlying DBMS implementation. Supported values are 'oracle', 'db2', 'mysql' and 'hsqldb'.
# The value of this property defines which vendor specific implementations of DbSupport and ConstraintsDisabler are chosen.
database.dialect=mysql

# user properties
database.userName=${test.database.rules-satellite.username}
database.password=${test.database.rules-satellite.password}
database.schemaNames=${test.database.rules-satellite.schema}

当我运行测试时,出现以下错误:

开始嵌套异常

java.net.UnknownHostException
MESSAGE: ${test.database.rules-satellite.server}

堆栈跟踪:

java.net.UnknownHostException: ${test.database.rules-satellite.server}
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:849)
    at java.net.InetAddress.getAddressFromNameService(InetAddress.java:1202)
    at java.net.InetAddress.getAllByName0(InetAddress.java:1153)
    at java.net.InetAddress.getAllByName(InetAddress.java:1083)
    at java.net.InetAddress.getAllByName(InetAddress.java:1019)

我认为 Maven 属性没有被传播,这就是 unitils 无法识别它们的原因。

4

2 回答 2

2

正如 Marcin Krasowski 所说,您需要使用资源插件,即只需添加

<build>
  <resources>
    <resource>
       <directory>(path to the parent directory of your unitils conf file)</directory>
       <filtering>true</filtering>
    </resource>
  </resources>
</build>

默认情况下, Maven 不会替换${}所有文件中的变量,您可以使用资源插件告诉它这样做。

于 2014-08-21T11:39:35.087 回答
1

您需要使用带有过滤选项的 Maven 资源插件。更详细的解释可以在这里找到:http: //www.manydesigns.com/en/portofino/portofino3/tutorials/using-maven-profiles-and-resource-filtering/#TOC-Resource-filtering

于 2014-08-21T11:25:06.143 回答