如何将maven zip依赖项放在Java测试的类路径上
我有一个完全由junit/integration测试组成的Java项目,由maven管理。其中一个依赖项是zip存档,我希望在运行测试时,它的内容可以在类路径上使用。因为我不得不想出我认为是一个杂乱无章的解决办法。我将压缩文件解压到一个临时目录中,然后将其中一个结果目录复制到如何将maven zip依赖项放在Java测试的类路径上,java,unit-testing,maven,dependencies,Java,Unit Testing,Maven,Dependencies,我有一个完全由junit/integration测试组成的Java项目,由maven管理。其中一个依赖项是zip存档,我希望在运行测试时,它的内容可以在类路径上使用。因为我不得不想出我认为是一个杂乱无章的解决办法。我将压缩文件解压到一个临时目录中,然后将其中一个结果目录复制到/testclasses文件夹中。我还必须使清洁步骤删除临时目录。以下是pom的相关部分: <groupId>com.my.package</groupId> <artifactId>te
/testclasses
文件夹中。我还必须使清洁步骤删除临时目录。以下是pom的相关部分:
<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>
com.my.package
测试项目
0.0.1-快照
我的测试项目
环境发展
2.0.8-快照
${project.basedir}/temp
...
maven清洁插件
2.3
${tempDir}
org.apache.maven.plugins
maven依赖插件
2.8
解包配置
编译
解包依赖项
com.my.package.config
${config.artifactId}
配置
${tempDir}
maven资源插件
2.6
编译
复制资源
${project.build.directory}/test classes/TargetDir
${tempDir}/${config.artifactId}-${config.version}/TargetDir
真的
...
com.my.package.config
${config.artifactId}
${config.version}
配置
拉链
一定有更好的方法做到这一点。
我可以强迫maven将zip文件当作jar来处理吗?我提供的链接有一个诱人的暗示,这可能曾经是可能的,但我在文档中找不到任何相关内容。这似乎是一件很简单的事情,我真的希望我刚刚错过了某个配置参数。有谁能建议一种更好的方法将zip依赖项的内容放到类路径上吗?我会将该依赖项解压缩到目标
目录的子目录中,并将该目录添加到surefire插件的配置中
<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>
环境发展
2.0.8-快照
${project.build.directory}/addTestClasspath
org.apache.maven.plugins
谢谢您的回复。将其直接解包到target
中而没有中间副本的问题是,解包会将其写入/target/environment-dev-2.0.8-SNAPSHOT/
,这会导致资源查找问题。这就是为什么我要在复制我们需要的文件夹之前将其写入临时目录。我也在为同样的问题而挣扎。我尝试了上述解决方案,但对我无效。你能帮我进一步说一下添加额外的类路径后清单文件会是什么样子吗?@Michael-O太棒了!谢谢,那会有很大的不同