如何在没有源文件但有胖子的情况下运行gradle JUnit测试作业?

时间:2019-04-11 09:55:14

标签: java gradle junit fatjar

我对JVM领域非常陌生,无法解决如何解决以下问题:

我有一个gradle项目,它使用JUnit测试作为输出来创建一个测试uber jar(使用shadowJar插件构建)。我可以使用以下命令在同一项目中运行该uber jar:

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

但是,我要创建一个很小的gradle.build文件,以使用已经预先构建的jar运行同一作业。 对此进行详细说明:我有一个创建该胖子的项目A,并且我想有一个仅具有runFatJar任务且没有源的项目B。

我尝试对我的项目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.

我什至不知道现在为什么选择它。有没有办法可以强迫我在没有实际源代码文件的情况下开始这项工作?

1 个答案:

答案 0 :(得分:1)

我花了一些时间才弄清楚,但这是完成窍门的最终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 }
}

唯一的前提条件是立即调用unzip -qq fatjar.jar -d unzipped之前执行runBinaryTests。虽然gradle可以使用zip树,但是在处理UTF-8(黄瓜文件名所具有的)方面非常糟糕。如果有人知道如何解决此问题,可以使用以下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 }
}