2017-11-27 2 views
2

주 리소스와 테스트 리소스 모두에 대해 하나의 모듈 (shared-resources)에 의존하는 다중 모듈 프로젝트를 리팩터링합니다.다중 모듈 프로젝트에서 테스트 리소스를 테스트하십시오.

parent 
|_ shared-resources 
|_ child 

현재 모듈은 maven-resource-plugin를 사용하여 shared-resources를 포함한다.

현재 child/pom.xml :

... 
<resources> 
    <resource> 
    <directory>../shared-resources/src/main/resources</directory> 
    </resource> 
</resources> 
<testResources> 
    <testResource> 
    <directory>../shared-resources/src/test/resources</directory> 
    </testResource> 
</testResources> 
... 

나는 그들이이 shared-resources 모듈의 항아리와 테스트 항아리 포장을 통해 포함되도록 몇 가지 관련 질문 (예 : "Specify common resources in a multi-module maven project"과 "로, 종속성을 구조 조정하고 싶습니다 Share test resources between maven projects ")를 제안합니다. 새로운 shared-resources/pom.xml

: child/pom.xml

... 
    <build> 
    <plugins> 
     <plugin> 
     <artifactId>maven-jar-plugin</artifactId> 
     <executions> 
      <execution> 
      <goals> 
       <goal>test-jar</goal> 
      </goals> 
      </execution> 
     </executions> 
     </plugin> 
    </plugins> 
    </build> 
... 

새로운 :

... 
<dependency> 
    <groupId>com.example</groupId> 
    <artifactId>shared-resources</artifactId> 
    <version>0.0.1</version> 
</dependency> 
<dependency> 
    <groupId>com.example</groupId> 
    <artifactId>shared-resources</artifactId> 
    <version>0.0.1</version> 
    <scope>test</scope> 
    <type>test-jar</type> 
</dependency> 
... 

나는 자원의 부족 그러나 아이 테스트, 테스트를 실행

. 속성 테스트가 수행 액세스하는 위치

maven clean test --projects child --also-make

maven clean install -DskipTests && mvn test --projects child

maven clean verify --projects child --also-make

내 권위있는 테스트가 mvn clean install -DskipTests -pl :child -am && mvn test -pl :child -Dtest='PropertiesAccessTest'을하고있다 :

나는 다양한 방법으로 내 빌드를 실행했습니다

public class PropertiesAccessTest { 

    @Test 
    public void resourceAccessible() throws URISyntaxException, IOException { 
     propertyExists(
       "abcdef=12345", 
       "test.properties" 
     ); 
    } 

    private void propertyExists(String string, String fileName) throws IOException, URISyntaxException { 
     URL url = getClass().getClassLoader().getResource(fileName); 
     assertNotNull(url); 
     assertTrue("file should exist", new File(url.getFile()).exists()); 

     assertTrue("file should contain property", Files.readAllLines(Paths.get(url.toURI())) 
       .stream().anyMatch(l -> l.contains(string))); 
    } 
} 

test.propertiesshared-resources/src/test/resources/입니다 (위의 구성을 이해해야하므로 포함되어야 함). 그러나 테스트는 항상 "파일이 있어야합니다."와 함께 실패합니다.

내 로컬 .m2 리포지토리에 테스트 리소스가 포함 된 테스트 jar가 예상대로 포함되어 있는지 확인할 수 있습니다.

내가 뭘 잘못하고 있니?

+0

'공유 리소스'는 어떤 종류의 리소스입니까? 이러한 리소스가 적절한 모듈에 속한다는 인상을 받았습니까? – khmarbaise

+0

어떤 명령을 실행합니까? 'shared-resources' 아티팩트가 제대로 설치 되었습니까?두 프로젝트 모두에 대한 전체 POM을 제공하면 도움이 될 수 있으며, 주어진 요소에서 예술을 말합니다. –

+0

@ 피에르 B. 위의 빌드 명령을 상세히 설명했습니다. 그것이 가치있는 일이라면'share-resources' jar와 test jar의 내용이 예상 한 것과 같은지 확인할 수 있습니다. – jwilner

답변

1

파일을 별도의 모듈에 넣었습니다. 즉, 이제 JAR 파일에 임베드 될 것입니다. 더 이상 File을 만들 수 없습니다. 하지만 당신은 URI와의 InputStream을 통해 액세스 할 수 있습니다

getClass().getClassLoader().getResource("test.properties").openStream() 

그것은 훨씬 더 흥미로운 생산 자원 생각합니다. mvn test을 실행하면 Maven은 target/classes/test.properties을 사용합니다. 그리고이 File으로 작동합니다. 하지만 대신 mvn package을 실행하면 그렇지 않습니다. package 이후 Maven은 jar 파일을 target/classes 대신 classpath에 넣을 것이기 때문에.

관련 문제