maven 将主要项目包含到测试罐中
maven include main project into test-jar
我已经用 maven-jar-plugin 创建了一个测试 jar
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
但是测试 jar 需要主要 类 才能工作,那么我如何将它们包含在测试 jar 中(创建类似 uber-test-jar 的东西)?
通常我导入依赖两次:
- 普通的;
- 测试 jar 范围 = 测试。
这样您就不会因为缺少 类 而烦恼。但是,如果您的应用程序中不需要主要源(第一个依赖项)并且它们仅用于测试,您也可以将其范围切换为测试。
<scope>test</scope>
您不能通过 maven-jar-plugin 添加其他 类 到测试 jar,但您可以创建一个包含主项目 类 和测试 类 的 jar另一种方式:使用 maven-assembly-plugin!
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<descriptor>src/assembly/dep.xml</descriptor>
</configuration>
<executions>
<execution>
<id>create-archive</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
这将使用程序集描述符中描述的信息创建一个工件,描述符的默认路径是 src/assembly/。xml
在您要添加的描述符中:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly- plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>example</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.testOutputDirectory}</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
</assembly>
- id -> jar 的后缀
- 格式 -> 输出文件的格式(我们正在创建一个 jar)
- fileSets -> 所有要添加的目录
- fileSet -> 要添加的单个目录,带有 outputDirectory,包括 exclude ecc。
此示例将创建一个带有 -example 作为后缀的 jar(例如:plugin-1.0-example.jar),同时包含主文件和测试文件
我已经用 maven-jar-plugin 创建了一个测试 jar
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
但是测试 jar 需要主要 类 才能工作,那么我如何将它们包含在测试 jar 中(创建类似 uber-test-jar 的东西)?
通常我导入依赖两次:
- 普通的;
- 测试 jar 范围 = 测试。
这样您就不会因为缺少 类 而烦恼。但是,如果您的应用程序中不需要主要源(第一个依赖项)并且它们仅用于测试,您也可以将其范围切换为测试。
<scope>test</scope>
您不能通过 maven-jar-plugin 添加其他 类 到测试 jar,但您可以创建一个包含主项目 类 和测试 类 的 jar另一种方式:使用 maven-assembly-plugin!
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<descriptor>src/assembly/dep.xml</descriptor>
</configuration>
<executions>
<execution>
<id>create-archive</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
这将使用程序集描述符中描述的信息创建一个工件,描述符的默认路径是 src/assembly/。xml 在您要添加的描述符中:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly- plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>example</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.testOutputDirectory}</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
</assembly>
- id -> jar 的后缀
- 格式 -> 输出文件的格式(我们正在创建一个 jar)
- fileSets -> 所有要添加的目录
- fileSet -> 要添加的单个目录,带有 outputDirectory,包括 exclude ecc。
此示例将创建一个带有 -example 作为后缀的 jar(例如:plugin-1.0-example.jar),同时包含主文件和测试文件