나는 메이븐 POM 일부 구성 및 섹션 플러그인에서 파일을 가지고, 내가 받는다는 바람둥이 같은 일부 구성 플러그인이 파일 메이븐 POM에서 파일 해당 키가있는 tomcat.properties와 같은 일부 속성 파일 :읽기 속성은
url=http://localhost:8080/manager/html
그리고 어떻게이 POM 파일에서이 키를 읽을 수 있습니까?
나는 메이븐 POM 일부 구성 및 섹션 플러그인에서 파일을 가지고, 내가 받는다는 바람둥이 같은 일부 구성 플러그인이 파일 메이븐 POM에서 파일 해당 키가있는 tomcat.properties와 같은 일부 속성 파일 :읽기 속성은
url=http://localhost:8080/manager/html
그리고 어떻게이 POM 파일에서이 키를 읽을 수 있습니까?
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 plugin을 사용하고 initialize phase of the Maven lifecycle에 read-project-properties
목표를 실행해야합니다. . 플러그인 페이지에서 예는 여기 재현 : 그들은 사용할 수 있지만
<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>
이러한 속성은 치어 파일에서 사용할 수 없습니다로 허용 대답의 지침을 사용하여 파일의 속성을로드 실제로 가능하지 않다 필터링합니다. 최소 이의 예 : 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
에 갖는
[echo] Displaying value of properties
[echo] [foo] bar
전체 동작 예 :
는foo=bar
그런 명령 거릴 mvn validate
는 출력을 생성 이용 가능 : 의 pom.xml의 http://hg.defun.work/exp/file/tip/maven/properties
여기에서 중요한 부분 : 당신이 알파 단계에서 여전히 속성 - 받는다는 - 플러그인을 볼 수 있듯이 내가 빌드 도구로 Maven을 싫어하는 이유
<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>
, 즉. ..
이것은 작동합니다! .. 고맙습니다!! –