Чтение файла свойств из файла 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?
3 ответа
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 проекта в файле свойств, тогда вам нужно будет использовать плагин Properties 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>
Полный рабочий пример доступен по адресу: 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 как инструменты для сборки...
На самом деле невозможно загрузить свойства из файла, используя инструкции в принятом ответе, так как эти свойства недоступны в файле 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