使用Maven构建单独的JAR文件,以便对自定义类加载器进行单元测试 [英] Using Maven to build separate JAR files for unit testing a custom class loader

查看:118
本文介绍了使用Maven构建单独的JAR文件,以便对自定义类加载器进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为我当前项目的一部分,我创建了一个自定义类加载器。自定义加载器的部分单元测试涉及使用一些JAR文件来演示加载器的正确行为。

As part of my current project I've created a custom class loader. Part of the unit tests for the custom loader involves using some JAR files to demonstrate the proper behavior of the loader.

我想从Java构建测试JAR文件在运行实际单元测试之前的来源。此外,测试JAR文件在运行单元测试时不能在类路径上,因为我想在测试执行期间动态加载它们。

I'd like to build the test JAR files from Java sources ahead of running the actual unit tests. Further, the test JAR files cannot be on the class path when the unit tests are run, since I want to dynamically load them during the test execution.

是否有一种标准模式可以实现这种在测试阶段之前在侧面构建一些JAR但是将它们排除在类路径之外的要求?我不敢相信我是第一个尝试使用Maven 2进行此操作的人,但我似乎无法找到正确的POM结构和依赖关系。通常我最终会在测试阶段之前没有构建一些测试罐,但是我也遇到了不一致的构建顺序的问题,导致构建在一台机器上正常工作,但是无法构建一些在另一个上测试罐子。

Is there a standard pattern for accomplishing this sort of "build some JARs on the side before the test phase but leave them out of the class path" requirement? I can't believe I'm the first person to try doing this with Maven 2, but I can't seem to hit on the right POM structure and dependencies. Usually I end up with some of the test jars not being built ahead of the test phase, but I've also had problems with inconsistent order-of-build causing the build to work properly on one machine, but fail to build some of the test jars on another.

推荐答案

最简单的方法是设置另一个项目来打包测试jar的类,然后将其设置为正常的测试范围依赖。

The simplest thing to do is to set up another project to package the classes for your test jar, then set that as a normal test-scoped dependency.

如果您不想/不能这样做,您可以使用程序集插件在中创建一个jar process-test-classes 阶段(即在编译测试之后但在执行测试之前)。下面的配置将调用程序集插件,在目标目录的该阶段创建一个名为 classloader-test-deps 的jar。然后你的测试可以根据需要使用那个jar。

If you don't want/aren't able to do that, you can use the assembly plugin to create a jar in the process-test-classes phase (i.e. after the tests have been compiled but before the tests are executed). The configuration below will invoke the assembly plugin to create a jar called classloader-test-deps in that phase in the target directory. Your tests can then use that jar as needed.

程序集插件使用一个程序集描述符(在src / main / assembly中,名为test-assembly.xml)打包目标/测试类的内容。我已经设置了一个过滤器来包含com.test包及其子项的内容。这假设您有一些包名称约定,您可以申请jar的内容。

The assembly plugin uses an assembly descriptor (in src/main/assembly, called test-assembly.xml) that packages the contents of target/test-classes. I've set up a filter to include the contents of com.test package and its children. This assumes you have some package name convention you can apply for the contents of the jar.

默认情况下,程序集插件将jar作为附加工件附加,通过指定附加为false,不会安装/部署。

The assembly plugin will by default attach the jar as an additional artifact, by specifying attach as false, it will not be installed/deployed.

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>2.2-beta-2</version>
  <executions>
    <execution>
      <id>create-test-dependency</id>
      <phase>process-test-classes</phase>
      <goals>
        <goal>single</goal>
      </goals>
      <configuration>
        <finalName>classloader-test-deps</finalName>
        <attach>false</attach>
        <descriptors>
          <descriptor>src/main/assembly/test-assembly.xml</descriptor>
        </descriptors>
      </configuration>
    </execution>
  </executions>
</plugin>

这是test-assembly.xml的内容

This is the content of test-assembly.xml

<assembly>
  <id>test-classloader</id>
  <formats>
    <format>jar</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <fileSets>
    <fileSet>
      <directory>${project.build.testOutputDirectory}</directory>
      <outputDirectory>/</outputDirectory>
      <!--modify/add include to match your package(s) -->
      <includes>
        <include>com/test/**</include>
      </includes>
    </fileSet>
  </fileSets>
</assembly>

这篇关于使用Maven构建单独的JAR文件,以便对自定义类加载器进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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