2014-10-03 191 views
3

我有一个完全由maven管理的junit/integration测试组成的Java项目。其中一个依赖项是一个zip压缩文件,我希望在运行测试时可以在类路径中使用它的内容。由于maven does not put the content of a zip dependency on the classpath我不得不拿出我认为是一个hacky解决方法。我将zip压缩包解压缩到临时目录,然后将其中一个生成的目录复制到/test-classes文件夹中。我还必须使clean步删除临时目录。以下是pom的相关部分:如何把maven zip依赖于Java测试的类路径

<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吗?我提供的链接有一个诱人的暗示,这可能曾经是可能的,但我无法在文档中找到任何相关内容。这似乎是这样一个简单的事情,我真的希望我错过了一个配置参数的地方。任何人都可以提出一种更好的方法将zip依赖关系的内容放到类路径中吗?

+0

https://issues.apache.org/jira/browse/MNG-5567 – 2016-05-16 18:42:20

+0

@ Michael-O太棒了!谢谢,这会产生很大的影响 – 2016-05-23 16:28:15

回答

2

我会将依赖项解压缩到target目录的子目录中,并将该目录添加到surefire插件的additionalClasspathElements配置中。

<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> 

在这种情况下,你可以省略清洁插件配置,因为一切都是target文件夹,将默认由干净的插件删除之下。

不幸的是,这个配置只能在命令行上工作,而不是在eclipse中,因为m2e插件没有兑现additionalClasspathElement。查看jira问题MNGECLIPSE-1213

+0

感谢您的回复。将它直接解包到没有中间拷贝的'target'的问题是解压缩会将它写入'/ target/environment-dev-2.0.8-SNAPSHOT /',这会导致资源查找问题。这就是为什么我在复制我们需要的文件夹之前将它写入临时目录。 – 2014-10-06 09:52:02

+0

@ReneLink,请查看https://issues.apache.org/jira/browse/MNG-5567 – 2016-05-23 20:48:00