testing - 从 maven zip 程序集运行测试

标签 testing maven zip maven-assembly-plugin

我终于成功地让 Maven 使用程序集文件将一堆 jar 压缩在一起,并将其安装到我的本地存储库中。这已经够难的了……

现在我的目标是配置另一个 Maven 项目,这样当我执行“mvn 测试”时,它会拉入该 zip、解压缩它,并从该 zip 文件中的 jars 运行测试。有谁知道如何做到这一点?

这是组装项目的 POM:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.pason</groupId>
<artifactId>RigFocusOnDataHub</artifactId>
<name>RigFocusOnDataHub</name>
<version>12.2.0-SNAPSHOT</version>
<packaging>pom</packaging>

<dependencies>
    ...
</dependencies>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <descriptors>
                    <descriptor>RigFocusOnDH.xml</descriptor>
                </descriptors>
            </configuration>
            <executions>
                <execution>
                    <id>zip</id>
                    <!-- this is used for inheritance merges -->
                    <phase>package</phase>
                    <!-- append to the packaging phase. -->
                    <goals>
                        <goal>single</goal>
                        <!-- goals == mojos -->
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

这是第二个项目的 POM。不幸的是,它没有下载 RigFocusOnDataHub 的 zip 文件,它只是从本地存储库中获取所有 RigFocusOnDataHub 依赖项的 jar。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.pason</groupId>
<artifactId>RigFocusDHSystemTest</artifactId>
<version>12.2.0-SNAPSHOT</version>
<dependencies>
    <dependency>
        <groupId>com.pason</groupId>
        <artifactId>MurphyRigFocus</artifactId>
        <version>2.0.0-SNAPSHOT</version>
        <type>test-jar</type>
    </dependency>
    <dependency>
        <groupId>com.pason</groupId>
        <artifactId>RigFocusOnDataHub</artifactId>
        <version>12.2.0-SNAPSHOT</version>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>unpack</id>
                    <phase>process-test-classes</phase>
                    <goals>
                        <goal>unpack</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>com.pason</groupId>
                                <artifactId>MurphyRigFocus</artifactId>
                                <version>2.0.0-SNAPSHOT</version>
                                <type>test-jar</type>
                                <outputDirectory>${project.build.directory}/tests/MurphyRigFocus</outputDirectory>
                            </artifactItem>
                            <artifactItem>
                                <groupId>com.pason</groupId>
                                <artifactId>RigFocusOnDataHub</artifactId>
                                <version>12.2.0-SNAPSHOT</version>
                                <type>zip</type>
                                <outputDirectory>${project.build.directory}/tests/MurphyRigFocus</outputDirectory>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <testClassesDirectory>${project.build.directory}/tests/MurphyRigFocus</testClassesDirectory>
                <reportsDirectory>${project.build.directory}/surefire-reports/MurphyRigFocus</reportsDirectory>
                <includes>
                    <include>**/*IT.*</include>
                </includes>
                <argLine>-Djava.library.path=${basedir}/target/classes/</argLine>
                <forkMode>pertest</forkMode>
            </configuration>
        </plugin>
    </plugins>
</build>

最佳答案

你需要:

  1. 从 zip 中提取 jar - 使用 maven-dependency-plugin 这很容易
  2. 削减传递依赖,这样你的 jars 就不会在路径中出现两次 - 你可以在源代码中使用 maven-shade-plugin 或在测试项目本身中使用依赖排除来做到这一点
  3. 将 jars 添加到你的测试类路径,有很多方法可以做到这一点,我会先尝试在 surefire 配置中使用额外的参数

关于testing - 从 maven zip 程序集运行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10689501/

相关文章:

java - 通过 netbeans 将 Maven EAR 应用程序部署到 glassfish 失败

我可以使用 zlib 解压缩实际上只是 zip 文件的 Microsoft Office 文件吗?

java - 无法在 IntelliJ IDEA 中添加 maven 模块

php - 检查 ZIP 存档中内容的文件大小

linux - 尝试在 Linux 上使用 "find"和 "zip -m"压缩并自动删除文件

python - 对于假设策略,如何从 max_value 而不是 min_value 开始测试用例?

django - 测试 Django Rest Framework 时创建超链接关系

mysql - 第一个查询行为不稳定

Django Rest Framework APIClient 在测试期间不处理异常

java - 如何从java运行maven命令?