C0d*_*ack 30 java dependencies maven maven-jar-plugin maven-shade-plugin
我正在尝试使用Maven将我的测试类打包到一个带有依赖项的可执行jar中,但我很难做到这一点.
到目前为止这是我的pom.xml:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.c0deattack</groupId>
<artifactId>executable-tests</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>executable-tests</name>
<dependencies>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.21.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
<build>
<plugins>
<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>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>cucumber-tests</finalName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>cucumber.cli.Main</mainClass>
</transformer>
</transformers>
<artifactSet>
<includes>
<include>info.cukes:*</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.c0deattack</groupId>
<artifactId>executable-tests</artifactId>
<version>1.0</version>
<type>test-jar</type>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
当我执行mvn clean package
构建时创建3个罐子:
Cucumber-tests.jar
包含info.cuke
依赖项但它不包含executable-tests-1.0-tests.jar
.
我已经做了各种各样的尝试,包括测试课程,但没有任何工作,我错过了什么?
编辑:我已经将我的示例项目推送到GitHub,如果有任何人想玩它:) https://github.com/C0deAttack/ExecutableTests
A_D*_*teo 18
回答很晚,但得到了一个可行的解决方案,可以帮助未来的访问者这个问题.
我成功使用了一个只使用一个Maven插件的胖罐,包括:
compile
范围内)test
范围内)这基本上意味着增加了测试类(及其依赖项)的胖罐.在Maven的Jar插件和它的test-jar
目标将无法满足这一需求.在Maven的阴影插件和它的shadeTestJar
方案不会帮助也没有.
那么,如何在Maven中创建一个带有测试类和外部依赖项的胖罐?
在这种情况下,Maven Assembly Plugin是一个完美的候选者.
这是一个最小的POM样本:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample</groupId>
<artifactId>sample-project</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>com.sample.TestMain</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)
上面的配置还设置了在测试类中定义的主类(用于快速检查它是否有效,在答案上).但这还不够.
您还需要在文件夹中创建一个描述符文件,其中包含以下内容:src\main\assembly
assembly.xml
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>fat-tests</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>test</scope>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.build.directory}/test-classes</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>**/*.class</include>
</includes>
<useDefaultExcludes>true</useDefaultExcludes>
</fileSet>
</fileSets>
</assembly>
Run Code Online (Sandbox Code Playgroud)
上面的配置是:
test
范围中获取的外部依赖关系(也将采用compile
范围)fileset
包含已编译的测试类作为打包的胖jar的一部分fat-tests
分类器设置一个最终的jar (因此你的最终文件会是这样的sampleproject-1.0-SNAPSHOT-fat-tests.jar
).建立jar:
mvn clean compile test-compile assembly:single
Run Code Online (Sandbox Code Playgroud)
从target
文件夹运行:
java -jar sampleproject-1.0-SNAPSHOT-fat-tests.jar
Run Code Online (Sandbox Code Playgroud)
我们将执行main(来自测试类).main可以调用其他测试或应用程序代码(因此需要外部依赖,在两者compile
或test
范围内),一切都会正常工作.
从这样的main,您还可以调用所有测试用例,如下所示:
测试套件示例:
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({ AppTest.class })
public class AllTests {
}
Run Code Online (Sandbox Code Playgroud)
注意:在这种情况下,测试套件仅涉及AppTest
样本测试.
然后你可以有一个主类如下:
import org.junit.internal.TextListener;
import org.junit.runner.JUnitCore;
public class MainAppTest {
public static void main(String[] args) {
System.out.println("Running tests!");
JUnitCore engine = new JUnitCore();
engine.addListener(new TextListener(System.out)); // required to print reports
engine.run(AllTests.class);
}
}
Run Code Online (Sandbox Code Playgroud)
然后,上面的主要部分将执行测试套件,该套件将链执行所有已配置的测试.
我以不同的方式解决了我的问题;使用另外两个插件。
首先我使用maven-dependency-plugin
来获取依赖项并在本地解压它们,然后我使用maven-jar-plugin
来创建一个test-jar
并包含解压后的依赖项中的类。
归档时间: |
|
查看次数: |
15492 次 |
最近记录: |