【问题标题】:How to include dependencies from tests classes and main executable class in one jar如何在一个 jar 中包含来自测试类和主要可执行类的依赖项
【发布时间】:2020-05-26 22:50:40
【问题描述】:

我正在尝试创建一个可执行 jar 来启动我的硒化物测试。

TestClass

public class TestClass {

    @Test
    public void openBrowser(){
        System.setProperty("webdriver.chrome.driver", "chrome location");
        open("google.com");
    }
}

测试执行器

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 {
    maven {
        url = 'jfrog artifactory url'
    }
}
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
task testJar(type: ShadowJar) {
    manifest {
        attributes 'Implementation-Title': 'Gradle',
                'Implementation-Version': project.version,
                'Main-Class': 'TestExecutor'
    }
    from sourceSets.test.output
    configurations = [project.configurations.testRuntime]
}

test {
    useJUnitPlatform()
 }



dependencies {
    testCompile  'org.seleniumhq.selenium:selenium-java:3.141.59'
    testCompile   'org.apache.poi:poi:4.1.0'
    testCompile   'org.apache.poi:poi-ooxml:4.1.0'
    testCompile  'org.slf4j:slf4j-simple:1.7.5'
    testCompile   group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.5.2'
    testCompile  group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.5.2'
    testCompile   group: 'com.codeborne', name: 'selenide', version: '5.1.0'
    testCompile   group: 'com.aventstack', name: 'extentreports', version: '4.0.9'
    testCompile  group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.5.2'
    testCompile group: 'org.testng', name: 'testng', version: '6.14.3'

    compile  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 个多星期了。我不知道我真正需要做什么,因为这一切对我来说都是新的。发送帮助。

【问题讨论】:

    标签: java gradle testng selenide


    【解决方案1】:

    我建议您使用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 将具有运行所需的所有依赖项,而无需使用 collects。

    并且不要忘记指定主类,如 here 所述。

    shadowJar {
        manifest {
            attributes 'Main-Class': 'a.b.C' // FQDN!
        }
    }
    

    【讨论】:

    • 我无法使用 Shadowjar。我实际上是从我工作中的私人 url 获取所有依赖项
    • 好吧,我看到你的配置中有repositories { mavenCentral() },所以这听起来很奇怪。另外,我在你的脚本中看到了plugins 块,所以插件很可能是从Gradle Plugin repository 解析的。 除非你的settings.gradle 中有一些黑魔法,我对此表示怀疑。然而,影子插件与你的依赖源及其解析过程无关,它只是创建一个包含所有依赖项的结果工件。
    • 我知道如何使用它,但现在我得到一个 java.lang.NoClassDefFoundError: org/testng/ITestListener
    • 看起来你的胖 JAR(影子 JAR)并没有那么胖并且缺少一些类。检查其内容和配置。如果您使用的是 Shadow JAR 插件,您确定您运行 shadowJar 任务(或 testJar)吗?检查您运行的 JAR 文件——输出中可能有多个工件。
    • 我不得不将所有依赖项切换到 testCompilecompile 因为我发现shadowJar 和新的 gradle 实现存在问题,但我仍然遇到与以前相同的问题。我不能:(
    猜你喜欢
    • 2014-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    • 2014-02-09
    相关资源
    最近更新 更多