首页 文章

从maven zip程序集运行测试

提问于
浏览
0

我终于成功地让Maven使用汇编文件将一堆 jar 压缩在一起并将其安装到我的本地存储库中 . 这很难......

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

这是装配项目的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 回答

  • 1

    你需要:

    • 从zip中提取jar - 使用maven-dependency-plugin这很容易

    • 切断传递依赖关系,这样你的jar就不会在路径中结束两次了 - 你可以使用maven-shade-plugin在源代码中执行此操作,或者在具有依赖项排除项的测试项目本身中执行此操作

    • 将jar添加到测试类路径中,有很多方法可以做到这一点,我会先尝试在surefire配置中使用其他参数

相关问题