Свойства Maven- Pom.xml не распознаются в файле конфигурации unitils
Я включил свою конфигурацию базы данных в файл 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 их не распознают.
2 ответа
Вам нужно использовать плагин ресурсов Maven с возможностью фильтрации. Более подробное объяснение можно найти здесь: http://www.manydesigns.com/en/portofino/portofino3/tutorials/using-maven-profiles-and-resource-filtering/
Как отметил Марцин Красовски, вам нужно использовать плагин ресурсов, то есть просто добавить
<build>
<resources>
<resource>
<directory>(path to the parent directory of your unitils conf file)</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
Maven не заменяет ${}
переменные во всех файлах по умолчанию, вы говорите это с помощью плагина ресурсов.