2011-08-22 9 views
29

나는 메이븐 POM 일부 구성 및 섹션 플러그인에서 파일을 가지고, 내가 받는다는 바람둥이 같은 일부 구성 플러그인이 파일 메이븐 POM에서 파일 해당 키가있는 tomcat.properties와 같은 일부 속성 파일 :읽기 속성은

url=http://localhost:8080/manager/html 

그리고 어떻게이 POM 파일에서이 키를 읽을 수 있습니까?

답변

32

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 lifecycleread-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> 
+0

이것은 작동합니다! .. 고맙습니다!! –

4

이러한 속성은 치어 파일에서 사용할 수 없습니다로 허용 대답의 지침을 사용하여 파일의 속성을로드 실제로 가능하지 않다 필터링합니다. 최소 이의 예 : 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 
+0

** [echo] [foo] $ {foo} ** 문자열에 실패했습니다. Grrr .... – gavenkoa

+0

** validate ** ** ** 초기화 전에 ** 실행 되었기 때문입니다. – gavenkoa

+0

@gavenkoa 어떻게 그 문자열에서 실패하는지 설명 할 수 있습니까? –

8

전체 동작 예 :

foo=bar 

그런 명령 거릴 mvn validate는 출력을 생성 이용 가능 : 의 pom.xmlhttp://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> 

, 즉. ..