如何将Maven zip依赖项放在Java测试的类路径中 [英] How to put maven zip dependency on classpath for Java tests

查看:79
本文介绍了如何将Maven zip依赖项放在Java测试的类路径中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java项目,该项目完全由javen/integration测试组成,由maven管理.依赖项之一是zip存档,运行测试时,我希望其内容可在classpath上找到.由于 maven不会将zip依赖项的内容放在类路径上我认为这是个骇人的解决方法.我将zip归档文件解压缩到一个临时目录中,然后将结果目录之一复制到/test-classes文件夹中.我还必须使clean步骤删除temp目录.这是pom的相关部分:

I have a Java project entirely consisting of junit/integration tests which is managed by maven. One of the dependencies is a zip archive, the contents of which I would like to be available on the classpath when the tests are run. Since maven does not put the content of a zip dependency on the classpath I have had to come up with what I consider to be a hacky workaround. I unpack the zip archive to a temp directory then copy one of the resulting directories into the /test-classes folder. I also had to make the clean step delete the temp directory. Here are the relevant parts of the 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>

必须有一种更好的方法.

我可以强制Maven将zip文件当作一个jar来对待吗?我提供的链接非常诱人,暗示这曾经可能实现,但是我在文档中找不到任何相关的内容.这似乎很简单,我真的希望我只是在某个地方错过了config参数.谁能建议一种更好的方法来将zip依赖项的内容放到类路径中?

Can I force maven to treat the zip file as if it were a jar? The link I provided has a tantalising hint that this might once have been possible, but I can't find anything relevant in the documentation. This seems like such a simple thing to be able to do, I really hope I've just missed a config parameter somewhere. Can anyone suggest a better way of getting the content of a zip dependency onto the classpath?

推荐答案

我将把依赖关系解压缩到target目录的子目录中,然后将该目录添加到

I would unzip the dependency into a subdirectory of the target directory and add that directory to the additionalClasspathElements configuration of the surefire plugin.

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

在这种情况下,您可以省略clean插件配置,因为所有内容都在target文件夹下,默认情况下该文件夹将由clean插件删除.

In this case you can omit the clean plugin config because everything is under the target folder which will be deleted by the clean plugin by default.

遗憾的是,此配置仅在命令行上有效,而不能在eclipse中使用,因为m2e插件不支持additionalClasspathElement.请参见jira问题 MNGECLIPSE-1213

Sadly this configuration does only work on the command line and not within eclipse, because the m2e plugin does not honor the additionalClasspathElement. See the jira issue MNGECLIPSE-1213

这篇关于如何将Maven zip依赖项放在Java测试的类路径中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆