我正在尝试创建一个可执行的jar,它将启动我的硒测试。
TestClass
public class TestClass {
@Test
public void openBrowser(){
System.setProperty("webdriver.chrome.driver", "chrome location");
open("google.com");
}
}
Test Executor
public class TestExecutor {
public static void main(String[] args) {
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] { TestClass.class });
testng.addListener((ITestNGListener) tla);
testng.run();
}
}
build.gradle
plugins {
id 'java'
}
repositories {
mavenCentral()
}
task testJar(type: Jar) {
zip64 = true
manifest {
attributes 'Implementation-Title': 'Gradle',
'Implementation-Version': project.version,
'Main-Class': 'TestExecutor'
}
from sourceSets.test.output
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
with jar
}
test {
useJUnitPlatform()
}
dependencies {
testImplementation 'org.seleniumhq.selenium:selenium-java:3.141.59'
testImplementation 'org.apache.poi:poi:4.1.0'
testImplementation 'org.apache.poi:poi-ooxml:4.1.0'
testImplementation 'org.slf4j:slf4j-simple:1.7.5'
testImplementation group: 'com.codeborne', name: 'selenide', version: '5.1.0'
testImplementation group: 'com.aventstack', name: 'extentreports', version: '4.0.9'
testImplementation group: 'org.testng', name: 'testng', version: '6.14.3'
implementation group: 'org.testng', name: 'testng', version: '6.14.3'
}
group 'groupId'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
构建jar时,它找不到测试依赖项。
我已经尝试将testRuntimeClasspath像这样添加到jar中
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
configurations.testRuntimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
但随后我收到一条错误消息,提示找不到或加载主类
我一直在努力使其运行2周以上。我真的不知道该怎么办,因为所有这些对我来说都是新的。发送帮助。
我建议您使用Gradle Shadow JAR plugin,而不要创建自己具有所有传递依赖项的JAR。他们甚至有the exact example for this case in the docs:
// Shadowing Test Sources and Dependencies
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
task testJar(type: ShadowJar) {
classifier = 'tests'
from sourceSets.test.output
configurations = [project.configurations.testRuntime]
}
这样,您将100%确保最终的JAR将具有运行所需的所有依赖项,而不会麻烦collect
。
并且不要忘记指定主类,如here所述。