2009-08-24 6 views

답변

30

build-helper-maven-plugin을 사용하여 다음과 같이 추가 테스트 리소스 디렉토리를 지정할 수 있습니다. 아래의 구성을 사용하여 테스트 자원 디렉토리의 내용은 생성 테스트 - 소스 중에 대상/테스트-classes 디렉토리에 복사됩니다 단계 : 당신이 당신의 재산을 넣어하려는 경우

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>build-helper-maven-plugin</artifactId> 
    <version>1.12</version> 
    <executions> 
    <execution> 
     <id>add-test-resource</id> 
     <phase>generate-test-sources</phase> 
     <goals> 
     <goal>add-test-resource</goal> 
     </goals> 
     <configuration> 
     <resources> 
      <resource> 
      <directory>path/to/additional/test/resources</directory> 
      <excludes> 
       <exclude>**/folder-to-exclude/**</exclude> 
      </excludes> 
      </resource> 
     </resources> 
     </configuration> 
    </execution> 
    </executions> 
</plugin> 
+4

기능적으로는 상관 없지만 질서 정연한 경우 위상 생성 테스트를해서는 안됩니다 generate-test-sources 대신 -resources를 사용합니까? – thSoft

23

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-surefire-plugin</artifactId> 
    <configuration> 
    <additionalClasspathElements> 
     <additionalClasspathElement>/add/this/to/path</additionalClasspathElement> 
    </additionalClasspathElements> 
    </configuration> 
</plugin> 
+1

불행하게도, Eclipse에서 테스트를 실행할 때 m2e는 Surefire의 구성을 선택하지 않습니다. build-helper-maven-plugin의 add-test-resource 목표보다 약간 간단하기 때문에 너무 나쁘다. – thSoft

7

왜 그냥 test/resources를 사용에서 클래스 패스에 속성을 배치 파일을 디스크에있는 곳과 빌드시/테스트 클래스를 대상으로 그 속성 파일을 복사하지 않으려는, 당신은이 방법을 수행 할 수 있습니다 그 점. 그들은 테스트 단계에만있을 것입니다.

+1

하나의 폴더 만 있지만 테스트 환경이 두 개 이상인 경우가 많습니다. – 30thh

+0

친구 인 Maven 프로필을 확인하십시오. –

4

리소스 환경이 여러 개인 경우 maven 프로필을 사용하여 테스트중인 프로필에 따라 다양한 리소스를 배치 할 수 있습니다.

test/resources/uat 
test/resources/prod 
test/resources/dev 

하지만 일반적으로 통합 테스트를 수행해야하는 경우 build-helper-maven-plugin이 필요하지 않습니다.

1

maven-resources-plugin은 리소스를 복사 할 수있는 copy-resources 목표를 가지고 있습니다. (당신이 project.build.testOutputDirectory을 수정하지 않는 한)

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-resources-plugin</artifactId> 
    <version>2.6</version> 
    <executions> 
     <execution> 
     <id>additional-resources</id> 
     <phase>process-test-resources</phase> 
     <goals> 
      <goal>copy-resources</goal> 
     </goals> 
     <configuration> 
      <outputDirectory>${project.build.testOutputDirectory}</outputDirectory> 
      <resources> 
      <resource> 
       <directory>${project.basedir}/conf</directory> 
      </resource> 
      </resources> 
     </configuration> 
     </execution> 
    </executions> 
    </plugin> 

이는 target/test-classes 폴더에 프로젝트의 자료에 conf 폴더의 내용을 복사합니다 단위 테스트 중에 클래스 경로에 추가됩니다 예를 들면 다음과 같습니다 .

23

새 테스트 리소스 폴더를 추가 할 수도 있습니다.

<build> 
    <testResources> 
     <testResource> 
      <directory>${project.basedir}/src/test/resources</directory> 
     </testResource> 
     <testResource> 
      <directory>${project.basedir}/src/test/something_else</directory> 
     </testResource> 
    </testResources> 
</build> 

첫 번째 경로 인 src/test/resources이 기본값입니다. 여전히 기본 경로가 사용되기를 원한다면 경로가 포함되어 있는지 확인하십시오. (testResources 태그는 기본값을 덮어 씁니다. 따라서 기본 경로를 명시 적으로 포함하지 않으면 사용이 중지됩니다.

+0

이것은 나를 위해 일했습니다. 기본 값에 대한 필요성을 지적하기 위해 +1. –

+2

그게 바로 내가 뭘 찾고 있었는지 고마워! 수락 된'build-helper-maven-plugin' 솔루션보다 훨씬 간단합니다. 이렇게하는 좋은 방법을 찾고있는 다른 사람에게는' ... '구문을 Maven 프로파일에 추가 할 수 있습니다. 다른 환경 (local/dev/test/prod)에 대해 다른 자원을 지정하려는 경우에 사용됩니다. –

관련 문제