0

I need to generate a jar file in IntelliJ where my test scenarios can be executed. I tried searching in google but all the information I found is deprecated and not working of fitting what I need. This is my project structure:

enter image description here

And this is my build.gradle file

plugins {
    id 'java'
    id 'application'
    id "com.github.johnrengelman.shadow" version "7.1.2"
}

mainClassName = 'TestRunner'

version '0.1.0'

repositories {
    mavenCentral()
}

configurations {
    cucumberRuntime {
        extendsFrom testImplementation
    }
}

dependencies {

    // https://mvnrepository.com/artifact/io.cucumber/cucumber-java
    implementation group: 'io.cucumber', name: 'cucumber-java', version: '7.2.3'

    // https://mvnrepository.com/artifact/io.cucumber/cucumber-testng
    implementation group: 'io.cucumber', name: 'cucumber-testng', version: '6.10.2'


}

distZip.shouldRunAfter(build)

jar {
    // Keep jar clean:
    exclude 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA', 'META-INF/*.MF'

    manifest {
        attributes 'Main-Class': 'TestRunner',
                'Class-Path': configurations.runtimeClasspath.files.collect { it.getName() }.join(' ')
    }
    // How-to add class path:
    //     https://stackoverflow.com/questions/22659463/add-classpath-in-manifest-using-gradle
    //     https://gist.github.com/simon04/6865179
}

def tags = (findProperty('tags') == null) ? 'not @Ignore' : findProperty('tags') + ' and not @Ignore'

task cucumber() {
    dependsOn assemble, testClasses
    doLast {
        javaexec {
            main = "io.cucumber.core.cli.Main"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = ['--plugin', 'pretty', '--glue', 'stepDefinitions', '--tags', "${tags}", 'src/test/resources/features']
        }
    }
}

And this is my TestRunner.java file

public class TestRunner {
    public static void main(String[] args) throws Throwable {
        io.cucumber.core.cli.Main.main(args);
    }
}

I tried with many ways, trying to generate the FatJar but I don't get anything working. Now I'm trying to use it with shadowJar but I don't get it. I don't have very much expertise in this.

By now I can execute by CLI using the command, gradle cucumber -Ptags="@Simple" and I want to get something like this but with the jar file generated.

Can anyone help me with this?? Thanks!

Juan
  • 11
  • 2
  • You mean this https://maven.apache.org/plugins/maven-jar-plugin/examples/create-test-jar.html? – hfontanez Feb 10 '22 at 14:19
  • @hfontanez this can be done using gradle instead of maven? It's what I'm looking for – Juan Feb 10 '22 at 14:51
  • I am not a gradle person, but I am sure it can be done. You'll have to read thru Gradle's documentation. – hfontanez Feb 10 '22 at 14:53
  • You'll have to look at what isn't working to know what you're doing wrong. I'm guessing your jar file doesn't contain the step definitions and features because they're part of your test resources. – M.P. Korstanje Feb 10 '22 at 17:48
  • @M.P.Korstanje Maybe could be that. I think I have to put some extra code in the testRunner because when I try to execute it I got this... WARNING: No features found at classpath:/ – Juan Feb 10 '22 at 17:54
  • You can open the jar file and check. If you can't open it rename it to `.zip`. – M.P. Korstanje Feb 10 '22 at 18:07
  • I have the stepDefinitions and the features within, but I don't know how to execute them using java -jar ... – Juan Feb 11 '22 at 15:31

1 Answers1

0

Ok, I finally got the jar with the features and the step definitions within enter image description here

Only have to add in the build.gradle

import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
    task testJar(type: ShadowJar) {
        archiveClassifier.set("tests")
        from sourceSets.test.output
        configurations = [project.configurations.runtimeClasspath]
    }

I think the only problem is related with the TestRunner Class enter image description here

I have to be capable to configure in someway the same that is configured in the build.gradle file for the cucumber task, and I really dont know having this jar generated, how to execute the "@Simple" feature

Juan
  • 11
  • 2