Чтение файла свойств из файла Maven POM
У меня есть файл Maven POM с некоторой конфигурацией и в плагинах раздела, у меня есть плагин maven tomcat с такой конфигурацией, как это:
<configuration>
<url>http://localhost:8080/manager/html</url>
<server>tomcat</server>
</configuration>
Я хотел бы экспортировать настройку url в некоторый файл свойств, например tomcat.properties, с помощью этого ключа:
url=http://localhost:8080/manager/html
И как я могу прочитать этот ключ в моем POM файле?
Ответы
Ответ 1
Maven позволяет вам определять свойства в проекте POM. Вы можете сделать это, используя файл POM, подобный следующему:
<project>
...
<properties>
<server.url>http://localhost:8080/manager/html</server.url>
</properties>
...
<build>
<plugins>
<plugin>
...
<configuration>
<url>${server.url}</url>
<server>tomcat</server>
</configuration>
...
</plugin>
</plugins>
</build>
</project>
Вы можете избежать указания свойства в теге properties
и передать значение из командной строки как:
mvn -Dserver.url=http://localhost:8080/manager/html some_maven_goal
Теперь, если вы не хотите указывать их из командной строки, и если вам нужно дополнительно изолировать эти свойства из POM проекта, в файл свойств, то вам нужно будет использовать Свойства плагина Maven и запустить его read-project-properties
на этапе инициализации жизненного цикла Maven. Пример со страницы плагина воспроизводится здесь:
<project>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<!-- Associate the read-project-properties goal with the initialize phase, to read the properties file. -->
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>etc/config/dev.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Ответ 2
Полный рабочий пример доступен по адресу: http://hg.defun.work/exp/file/tip/maven/properties
Здесь важна часть pom.xml:
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
</execution>
</executions>
<configuration>
<files>
<file>dev.properties</file>
</files>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<echo>project.build.sourceEncoding is "${project.build.sourceEncoding}"</echo>
<echo>foo is "${foo}"</echo>
<echo>with-spaces is "${with-spaces}"</echo>
<echo>existent.property is "${existent.property}"</echo>
<echo>nonexistent.property is "${nonexistent.property}"</echo>
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Как вы видите свойства-maven-plugin все еще на альфа-этапе, поэтому я ненавижу Maven как инструменты сборки...
Ответ 3
На самом деле невозможно загрузить свойства из файла, используя инструкции в принятом ответе, поскольку эти свойства недоступны в файле pom, хотя они могут использоваться для фильтрации.
Пример минимального счетчика:
В pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<!-- Associate the read-project-properties goal with the initialize phase, to read the properties file. -->
<execution>
<!-- Apart from this test, the phase must be initialize -->
<phase>validate</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>dev.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<echo>Displaying value of properties</echo>
<echo>[foo] ${foo}</echo>
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
В dev.properties
:
foo=bar
Затем выполните команду mvn validate
, выведя вывод:
[echo] Displaying value of properties
[echo] [foo] bar