当前位置: 首页 > 知识库问答 >
问题:

从maven zip程序集运行测试

鲜于子琪
2023-03-14

我终于成功地让Maven使用一个汇编文件压缩一堆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/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文件,而是从本地repo中获取所有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个答案

封昊天
2023-03-14

您需要:

  1. 从zip中提取jar-maven-dependency-plugin非常简单
  2. 切断传递依赖项,这样您的JAR就不会在路径中出现两次-您可以在源代码中使用maven-shade-plugin或在测试项目本身中使用依赖项排除项来完成此操作
  3. 将JAR添加到您的测试类路径中,有许多方法可以做到这一点,我会尝试首先在surefire配置中使用额外的参数
 类似资料:
  • 我试图在我的项目中实现BDD,我也使用Jbehave,我参考了可用的在线材料,我创建了故事文件、Steps类和Runner类,如下所示,但我得到了一些奇怪的异常,我无法解决它。 1)故事文件,'sample.Story' 但是如果我使用Junit运行Runner类,我会得到 如果有人能帮我解决这个问题,那就太好了。

  • 问题内容: 所以我在这里有一个菜鸟般的时刻,我以前从未使用过命令行来运行Java程序,但现在我需要。我遇到的问题是,当我尝试运行程序时,出现ClassNotFoundException。我的课叫做OmadUpdate。我已经使用javac命令将OmadUpdate.java文件编译为OmadUpdate.class。我已经检查了目录,并且它们都绝对存在,但是当我运行java OmadUpdate命

  • 我下载了一个java程序,它由两个文件夹src和classes组成,分别包含源文件和类文件。现在,src和classes文件夹包含几个嵌套的子文件夹,其中最后一个子文件夹分别包含源文件和类文件。更准确地说,源文件和类文件的路径是src/edu/univ/。java和classes/edu/univ/。班假设包含main函数的文件是main。java,如何从命令行运行此程序。 我尝试过: 我也尝试过

  • 问题内容: 我需要实现一个工具,该工具可以在数据库记录匹配某些条件时从SQL Server数据库运行程序。有办法吗? 问题答案: 我对xp_cmdshell会很小心,因为它会造成相当大的安全漏洞。在http://www.databasejournal.com/features/mssql/article.php/3372131/Using- xpcmdshell.htm中 查看此文章 如果服务帐户

  • 问题内容: 我需要从自己的Java程序中运行其他几个程序,基本上我需要运行这些命令行语句。 和 并且我需要在自己的程序中使用从这些程序写入控制台的文本输出。我已经尝试过使用svn运行Runtime.getRuntime()。exec(),但是它似乎没有做任何事情,因为它没有生成svn.log文件。同样,两个程序都需要在不同的地方调用,svn行需要从一个文件夹内调用,而java行需要从另一文件夹内调