2014-12-19 5 views
5

Eclipse를 통해 Maven 웹 프로젝트에서 작업하고 있습니다. web.xml에는 Maven을 실행할 때 사용하는 프로필에 따라 값을 변경해야하는 context-param이 있습니다.Maven : web.xml 파일에서 변수를 채우는 방법

<project> 

    <profiles> 
     <profile> 
      <id>prod</id> 
      <properties> 
       <ambiente.producao>true</ambiente.producao> 
      </properties> 
     </profile> 
     <profile> 
      <id>desenv</id> 
      <properties> 
       <ambiente.producao>false</ambiente.producao> 
      </properties> 
     </profile> 
    </profiles> 

    <build> 

     <resources> 
      <resource> 
       <directory>src/main/resources</directory> 
       <filtering>true</filtering> 
      </resource> 
      <resource> 
       <directory>src/main/webapp/WEB-INF/</directory> 
       <filtering>true</filtering> 
       <includes> 
        <include>**/web.xml</include> 
       </includes> 
      </resource> 
     </resources> 

     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-war-plugin</artifactId> 
       <version>2.5</version> 
       <configuration> 
        <webResources> 
         <resource> 
          <filtering>true</filtering> 
          <directory>src/main/webapp</directory> 
          <includes> 
           <include>**/web.xml</include> 
          </includes> 
         </resource> 
        </webResources> 
        <warSourceDirectory>src/main/webapp</warSourceDirectory> 
        <webXml>src/main/webapp/WEB-INF/web.xml</webXml> 
       </configuration> 
      </plugin> 
     </plugins> 

    </build> 

</project> 

내가 인터넷에서 발견 된 참조에 따라 플러그인을 받는다는 전쟁 - 플러그인으로 모두 resources 태그를 사용하고 있습니다 : 나는 다음과 같은 구성이 프로젝트의 POM 파일에서

<context-param> 
    <param-name>producao</param-name> 
    <param-value>${ambiente.producao}</param-value> 
</context-param> 

. 그러나 예상대로 작동하지 않았습니다. 이클립스에서는 목표가 인 maven을 실행하여을 새로 설치하고 "프로파일"로 또는 을 지정하고으로 꾸밉니다. Maven을 실행 한 후 web.xml에서 ${ambiente.producao} 속성이 대체되지 않는다는 것을 알았습니다. 따라서 내가 뭘 잘못했는지 알고 싶습니다. 필터링 리소스 또는 maven-war-plugin 만 사용해야합니까?

감사합니다,

라파엘 아폰소는

답변

16

는 인터넷에서 읽은 모든 것을 믿지 마세요.

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-war-plugin</artifactId> 
    <version>2.5</version> 
    <configuration> 
    <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors> 
    </configuration> 
</plugin> 

를 이제 구축 여부 : 당신은 주// WEB-INF는/자원 목록에서, 그냥 target/classes

귀하의 maven-war-plugin 구성으로 web.xml 파일을 필터링합니다은 필요 SRC를 웹 애플리케이션/제거해야 Maven CLI 또는 Eclipse에서 이러한 프로파일 중 하나를 활성화해야합니다. 명령 줄에서, 당신은 이클립스에서

(당신이 루나 자바 EE 배포 또는 더 최근의를 사용하는 가정) -Pdesenv

MVN 깨끗한 패키지를 실행해야합니다, 당신은 당신이 원하는 프로필을 활성화 할 수 있습니다 Ctrl+Alt+P을 눌러서 desenv을 사용하면 필터링 된 web.xml이 target/m2e-wtp/web-resources/WEB-INF/ 아래에 표시됩니다.이 파일이 응용 프로그램 서버에 게시됩니다.

두 프로필을 동시에 활성화하면 pom.xml에 나열된 최신 프로필이 우선 적용됩니다.

개인적으로 프로덕션 프로필을 제거하고 프로덕션 속성을 pom.xml의 기본 속성 섹션에 배치합니다. 이렇게하면 실제 값으로 web.xml을 항상 필터링하므로 비 생산 설정으로 실수로 응용 프로그램을 빌드하고 배포 할 기회가 줄어 듭니다.

<profile> 
    <id>desenv</id> 
    <!-- This profile is only activated when building in Eclipse with m2e --> 
    <activation> 
    <property> 
     <name>m2e.version</name> 
    </property> 
    </activation> 
    <properties> 
    <ambiente.producao>false</ambiente.producao> 
    </properties> 
</profile> 

에서 지원 필터링 동적 리소스에 대한 자세한 내용은 여기 m2e을-WTP : https://developer.jboss.org/en/tools/blog/2011/05/03/m2eclipse-wtp-0120-new-noteworthy

그리고를

그리고, 다음 활성화 규칙을 추가 할 수 있습니다 이클립스에서 자동으로 desenv 프로파일을 가능하게하기 위해

거기 screencast : http://screencast.com/t/hwodHqODBB

+1

이것은 아주 적당하고 합리적인 접근 및 설명이다. 감사! –

+1

이것은 마술 열쇠였습니다 : "filteringDeploymentDescriptors> true" –