2014-10-03 1 views
3

전적으로 maven이 관리하는 junit/integration 테스트로 구성된 자바 프로젝트가 있습니다. 의존성 중 하나는 zip 아카이브이며 테스트의 실행시 classpath에서 사용할 수있는 컨텐츠입니다. maven does not put the content of a zip dependency on the classpath 이래로 내가 해킹 된 해결 방법으로 생각한 것을 생각해 내야했습니다. zip 아카이브를 임시 디렉토리에 압축을 풀고 결과 디렉토리 중 하나를 /test-classes 폴더로 복사합니다. 또한 clean 단계에서 임시 디렉토리를 삭제해야했습니다. 여기에 치어의 관련 부분은 다음과 같습니다자바 테스트를위한 클래스 패스에 maven zip 의존성을 두는 법

<groupId>com.my.package</groupId> 
<artifactId>test-project</artifactId> 
<version>0.0.1-SNAPSHOT</version> 
<name>My Test Project</name> 

<properties> 
    <config.artifactId>environment-dev</config.artifactId> 
    <config.version>2.0.8-SNAPSHOT</config.version> 
    <tempDir>${project.basedir}/temp</tempDir> 
</properties> 

<build> 
    <plugins> 
     ... 
     <!-- clean out our custom temp directory as well as the default dir during clean phase--> 
     <plugin> 
      <artifactId>maven-clean-plugin</artifactId> 
      <version>2.3</version> 
      <configuration> 
       <filesets> 
        <fileset> 
         <directory>${tempDir}</directory> 
        </fileset> 
       </filesets> 
      </configuration> 
     </plugin> 
     <!-- since the config dependency is a zip it does not get added to the classpath. So we extract 
     it to a temp dir, then copy the content we need into a directory on the classpath --> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-dependency-plugin</artifactId> 
      <version>2.8</version> 
      <executions> 
       <execution> 
        <id>unpack-config</id> 
        <phase>compile</phase> 
        <goals><goal>unpack-dependencies</goal></goals> 
        <configuration> 
         <includeGroupIds>com.my.package.config</includeGroupIds> 
         <includeArtifactIds>${config.artifactId}</includeArtifactIds> 
         <includeClassifiers>config</includeClassifiers> 
         <outputDirectory>${tempDir}</outputDirectory> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
     <!-- copy the content of the zip file that we extracted into a directory on the classpath --> 
     <plugin> 
      <artifactId>maven-resources-plugin</artifactId> 
      <version>2.6</version> 
      <executions> 
       <execution> 
        <phase>compile</phase> 
        <goals><goal>copy-resources</goal></goals> 
        <configuration> 
         <outputDirectory>${project.build.directory}/test-classes/TargetDir</outputDirectory> 
         <resources> 
          <resource> 
           <directory>${tempDir}/${config.artifactId}-${config.version}/TargetDir</directory> 
           <filtering>true</filtering> 
          </resource> 
         </resources> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 
<dependencies> 
    ... 
    <dependency> 
     <groupId>com.my.package.config</groupId> 
     <artifactId>${config.artifactId}</artifactId> 
     <version>${config.version}</version> 
     <classifier>config</classifier> 
     <type>zip</type> 
    </dependency> 
</dependencies> 

이 일을 더 나은 방법이 있어야합니다.

maven이 zip 파일을 jar 파일로 취급하도록 강제 할 수 있습니까? 필자가 제공 한 링크에는이 기능이 한 번 가능했을 것이라는 암시가 있지만 설명서에는 관련이 없습니다. 이것은 할 수있는 그런 간단한 것 같이 보인다, 나는 진짜로 나가 구성 매개 변수를 어딘가에 놓쳤다는 것을 진짜로 희망한다. 누구든지 클래스 패스에 우편 종속성의 컨텐츠를 가져 오는 더 좋은 방법을 제안 할 수 있습니까?

+0

https://issues.apache.org/jira/browse/MNG-5567 –

+0

@ Michael-O Awesome! 감사합니다. 큰 차이를 만들 것입니다. –

답변

2

종속성을 target 디렉토리의 하위 디렉토리에 압축을 해제하고 해당 디렉토리를 surefire 플러그인의 additionalClasspathElements 구성에 추가합니다. 모든 것이 기본적으로 깨끗한 플러그인 삭제됩니다 target 폴더 아래에 있기 때문에

<properties> 
    <config.artifactId>environment-dev</config.artifactId> 
    <config.version>2.0.8-SNAPSHOT</config.version> 
    <unzipDir>${project.build.directory}/addTestClasspath</unzipDir> 
</properties> 

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-dependency-plugin</artifactId> 
      <version>2.8</version> 
      <executions> 
       <execution> 
        <id>unpack-config</id> 
        <phase>compile</phase> 
        <goals><goal>unpack-dependencies</goal></goals> 
        <configuration> 
         <includeGroupIds>com.my.package.config</includeGroupIds> 
         <includeArtifactIds>${config.artifactId}</includeArtifactIds> 
         <includeClassifiers>config</includeClassifiers> 
         <outputDirectory>${unzipDir}</outputDirectory> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <version>2.17</version> 
      <configuration> 
       <additionalClasspathElements> 
        <additionalClasspathElement>${unzipDir}</additionalClasspathElement> 
       </additionalClasspathElements> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

는이 경우 깨끗한 플러그인 설정을 생략 할 수 있습니다.

슬프게도이 구성은 m2e 플러그인이 additionalClasspathElement을 따르지 않기 때문에 일식이 아닌 명령 행에서만 작동합니다. jira 문제 참조 MNGECLIPSE-1213

+0

응답 해 주셔서 감사합니다. 중간 복사본이없는'target'에 직접 압축을 풀 때의 문제는 언팩이 리소스 룩업에 문제가되는'/ target/environment-dev-2.0.8-SNAPSHOT /'에 씁니다. 이것이 우리가 필요로하는 폴더를 복사하기 전에 임시 디렉터리에 저장하는 이유입니다. –

+0

@ReneLink, https://issues.apache.org/jira/browse/MNG-5567을 확인하십시오. –

관련 문제