2013-03-18 1 views
3

Java 클래스에 settings.xml 프로필 매개 변수를 주입하고 싶습니다. 나는 maven-annotation-plugin을 사용하려고 시도했지만 null 값이왔다. 나는이 플러그인은 모조Java 클래스에 Maven 매개 변수 삽입

을 위해 설계 되었기 때문에이 경우

Setting.xml가 나는 .properties 파일을 생성하고 코드를 생성 방지하기 위해 maven-resources-plugin을 사용 클래스

@Parameter(defaultValue = "test.email", readonly = true) 
private String userEmail; 

@Parameter(defaultValue = "test.password", readonly = true) 
private String userPassword; 

답변

7

에서

<profiles> 
    <profile> 
     <id>APP_NAME</id> 
     <properties> 
     <test.email>USER_EMAIL</test.email> 
     <test.password>USER_PASSWORD</test.password> 
     </properties> 
    </profile> 
    </profiles> 

스 니펫 궁금합니다.

<build> 
    <resources> 
    <resource> 
     <directory>src/main/resources</directory> 
     <filtering>true</filtering> 
    </resource> 
    </resources> 
<build> 

그리고 파일 src/main/resources/com/example/your/file.properties 작성 : 자바 액세스 그것에서

testMail = ${test.email} 
propertyName = ${maven.variable.name} 

:

getClass().getResourceAsStream("/com/example/your/file.properties") 

더 이동을, 당신은 maven-enforcer-plugin으로 test.email 속성의 존재를 적용 할 수 있습니다 :

<build> 
    <plugin> 
    <artifactId>maven-enforcer-plugin</artifactId> 
    <executions> 
     <execution> 
     <id>enforce-email-properties</id> 
     <goals> 
      <goal>enforce</goal> 
     </goals> 
     <configuration> 
      <rules> 
      <requireProperty> 
       <property>test.email</property> 
       <message> 
       The 'test.email' property is missing. 
       It must [your free error text here] 
       </message> 
      </requireProperty> 
      </rules> 
     </configuration> 
     </execution> 
    </executions> 
    </plugin> 
</build> 
+0

나는 또한 그것을 고려하고 있었지만, 개발자/기고자 중 일부는 repo에 자신의 개인 자격 증명을 위탁한다는 인간적인 요인이 항상 있습니다. 나는 settings.xml이 더 안전한 솔루션이 될 것이지만 –

+0

나는 대답을 완료했다. 당신이 볼 수 있듯이, 자격 증명은'settings.xml' 또는 명령 행에서 설정 될 수있다. – shellholic

+1

+1을 강요합니다. 나는 그 플러그인을 사용할 것이다. –

1

Maven을 사용하여 Java 클래스에 Properties 파일과 load 파일을 생성 할 수 있습니다.

관련 문제