How to run a gradle JUnit test job without source files but with a fat jar?

derwasp :

I am very new to the JVM world and can't figure how to solve the following problem:

I have a gradle project that creates an test uber jar (built with the shadowJar plugin) with JUnit tests as its output. I can run this uber jar within the same project using something like this:

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

What I want however is to create a very small gradle.build file to run this same job with the jar pre-built already. To elaborate on that: I have my project A that creates this fat jar and I want to have a project B that only has the runFatJar task and doesn't have sources.

I tried doing something like this with my project 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 }
}

My folder structure looks like this:

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

and after I run gradle runFatJar it becomes this:

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

But the gradle output doesn't do anything really:

> 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.

I don't even know which why to go with it now. Is there a way I can force this job to start without having the actual source code files?

derwasp :

It took me a while to figure it out, but here is the final gradle file that does the trick:

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 }
}

The only prerequisite is to do an unzip -qq fatjar.jar -d unzipped right before calling runBinaryTests. Though gradle is able to work with zip trees, it's pretty bad at handling UTF-8, which cucumber filenames have. If somebody knows how to fix that, here is a gradle file that you can use without unzipping the jar manually:

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 }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=150000&siteId=1