java - 如何在没有源文件但使用 fat jar 的情况下运行 gradle JUnit 测试作业?

标签 java gradle junit fatjar

我对 JVM 世界非常陌生,不知道如何解决以下问题:

我有一个 gradle 项目,它创建一个测试 uber jar(使用 shadowJar 插件构建),并以 JUnit 测试作为其输出。我可以使用类似这样的东西在同一个项目中运行这个 uber jar:

task runFatJar(type: Test) {
    dependsOn shadowJar
    classpath = project.files( "$buildDir/libs/fatjar.jar", configurations.runtime )
    outputs.upToDateWhen { false }
}

但是我想要的是创建一个非常小的 gradle.build 文件来使用已经预构建的 jar 来运行相同的作业。 详细说明一下:我的项目 A 创建了这个 fat jar,我想要一个项目 B,它只有 runFatJar 任务并且没有源。

我尝试在我的项目 B 中做这样的事情:

apply plugin: 'java'

buildscript {
    repositories {
        jcenter()
    }
}

repositories {
    jcenter()
}

dependencies {
   testRuntime("org.junit.vintage:junit-vintage-engine:5.4.1")
}

tasks.withType(Test) {
    systemProperties = System.getProperties()
    systemProperties.remove("java.endorsed.dirs")
}

configurations {
  itestCompile.extendsFrom testCompile
  itestRuntime.extendsFrom testRuntime
}

task runFatJar(type: Test) {
    classpath = project.files( "$buildDir/fatjar.jar", configurations.runtime )
    outputs.upToDateWhen { false }
}

我的文件夹结构如下所示:

├───build
└───src
    └───test
        └───resources
            └───features

在我运行gradle runFatJar之后,它变成了这样:

├───.gradle
│   ├───5.2.1
│   │   ├───executionHistory
│   │   ├───fileChanges
│   │   ├───fileContent
│   │   ├───fileHashes
│   │   └───vcsMetadata-1
│   ├───buildOutputCleanup
│   └───vcs-1
├───build
│   └───resources
│       └───test
│           └───features
└───src
    └───test
        └───resources
            └───features

但是 gradle 输出并没有真正执行任何操作:

> gradle runFatJar --info
Initialized native services in: C:\Users\derwasp\.gradle\native
The client will now receive all logging from the daemon (pid: 6960). The daemon log file: C:\Users\derwasp\.gradle\daemon\5.2.1\daemon-6960.out.log
Starting 3rd build in daemon [uptime: 49.78 secs, performance: 97%, no major garbage collections]
Using 8 worker leases.
Starting Build
Settings evaluated using settings file 'D:\Temp\!deleteme\settings.gradle'.
Projects loaded. Root project using build file 'D:\Temp\!deleteme\build.gradle'.
Included projects: [root project '!deleteme']

> Configure project :
Evaluating root project '!deleteme' using build file 'D:\Temp\!deleteme\build.gradle'.
All projects evaluated.
Selected primary task 'runFatJar' from project :
Tasks to be executed: [task ':compileJava', task ':processResources', task ':classes', task ':compileTestJava', task ':processTestResources', task ':testClasses', task ':runFatJar']
:compileJava (Thread[Execution worker for ':',5,main]) started.

> Task :compileJava NO-SOURCE
file or directory 'D:\Temp\!deleteme\src\main\java', not found
Skipping task ':compileJava' as it has no source files and no previous output files.
:compileJava (Thread[Execution worker for ':',5,main]) completed. Took 0.007 secs.
:processResources (Thread[Execution worker for ':',5,main]) started.

> Task :processResources NO-SOURCE
file or directory 'D:\Temp\!deleteme\src\main\resources', not found
Skipping task ':processResources' as it has no source files and no previous output files.
:processResources (Thread[Execution worker for ':',5,main]) completed. Took 0.001 secs.
:classes (Thread[Execution worker for ':',5,main]) started.

> Task :classes UP-TO-DATE
Skipping task ':classes' as it has no actions.
:classes (Thread[Execution worker for ':',5,main]) completed. Took 0.0 secs.
:compileTestJava (Thread[Execution worker for ':',5,main]) started.

> Task :compileTestJava NO-SOURCE
file or directory 'D:\Temp\!deleteme\src\test\java', not found
Skipping task ':compileTestJava' as it has no source files and no previous output files.
:compileTestJava (Thread[Execution worker for ':',5,main]) completed. Took 0.001 secs.
:processTestResources (Thread[Execution worker for ':',5,main]) started.

> Task :processTestResources UP-TO-DATE
Skipping task ':processTestResources' as it is up-to-date.
:processTestResources (Thread[Execution worker for ':',5,main]) completed. Took 0.011 secs.
:testClasses (Thread[Execution worker for ':',5,main]) started.

> Task :testClasses UP-TO-DATE
Skipping task ':testClasses' as it has no actions.
:testClasses (Thread[Execution worker for ':',5,main]) completed. Took 0.0 secs.
:runFatJar (Thread[Execution worker for ':',5,main]) started.

> Task :runFatJar NO-SOURCE
Skipping task ':runFatJar' as it has no source files and no previous output files.
:runFatJar (Thread[Execution worker for ':',5,main]) completed. Took 0.001 secs.

我现在都不知道为什么要这么做。有没有办法可以在没有实际源代码文件的情况下强制启动此作业?

最佳答案

我花了一段时间才弄清楚,但这是最终的 gradle 文件:

apply plugin: 'java'

compileJava.options.encoding = 'UTF-8'

tasks.withType(Test) {
    systemProperties = System.getProperties()
    systemProperties.remove("java.endorsed.dirs")
}

task runBinaryTests(type: Test) {
    testClassesDirs = project.files( "$projectDir/unzipped", configurations.runtime )
    classpath = project.files( "$projectDir/fatjar.jar", configurations.runtime )
    outputs.upToDateWhen { false }
}

唯一的先决条件是在调用 runBinaryTests 之前执行 unzip -qq fatjar.jar -d unzipped。虽然 gradle 能够使用 zip 树,但它在处理 UTF-8( cucumber 文件名所具有的)方面相当糟糕。如果有人知道如何解决这个问题,这里有一个 gradle 文件,您可以使用它而无需手动解压缩 jar:

apply plugin: 'java'

compileJava.options.encoding = 'UTF-8'

tasks.withType(Test) {
    systemProperties = System.getProperties()
    systemProperties.remove("java.endorsed.dirs")
}

task runBinaryTests(type: Test) {
    testClassesDirs += zipTree($projectDir/fatjar.jar)
    classpath = project.files( "$projectDir/fatjar.jar", configurations.runtime )
    outputs.upToDateWhen { false }
}

关于java - 如何在没有源文件但使用 fat jar 的情况下运行 gradle JUnit 测试作业?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55629672/

相关文章:

java - 使用 Kundera 在 hbase 中使用通用对象类型

java - 如何将IntelliJ IDEA 2017.1.2连接到Postgresql

Java 正则表达式 emptyString 仅数字和 .或 , 需要的数字

android - git克隆后的Gradle错误

java - 内部依赖类限定名称冲突

java - 无法解析方法getSupportFragmentManager()

java - 从 JUnit 测试中的 for 循环获得用户输入,现在如何实际测试它的输出?

spring-boot - 尽管存在 `@Transactional` ,为什么这些数据库修改没有回滚?

java - 在每个给定字符后插入一个空格 - java

spring-mvc - Spring MockMvc和异步 Controller 的HTTP状态代码