I have an application in java written in IntelliJ using gradle. Right now I can run tests but I'd like to run main file to check execution by myself. Does anyone know how to run main file?
Looks like you use Java. In this case:
Apply application plugin
Define mainClassName property
The build.gradle.kts fill be like:
plugins {
application
}
application {
mainClassName = "org.gradle.sample.Main"
}
Next you can start application via gradlew run
Related
I'm building a Java command line application using gradle and have it running when I use gradlew run, however I would like to generate a jar -- which I would assume I would then have users download to invoke the CLI.
However, when I run gradlew jar, nothing is produced (build/lib dir doesn't even exist) even though the build runs with no errors and finishes with BUILD_SUCCESSFUL.
Two questions:
Why is no jar being produced?
Is having users download a jar the best way to ship a CLI for Java?
Below is my full build.gradle.kts
plugins {
// Apply the application plugin to add support for building a CLI application in Java.
application
id("com.diffplug.spotless") version "6.12.0"
}
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}
dependencies {
// Use JUnit test framework.
testImplementation("junit:junit:4.13.2")
// This dependency is used by the application.
implementation("com.google.guava:guava:30.1-jre")
implementation("info.picocli:picocli:4.7.0")
annotationProcessor("info.picocli:picocli-codegen:4.7.0")
implementation("io.vavr:vavr:0.10.4")
}
application {
// Define the main class for the application.
mainClass.set("testlauncher.command.Runner")
}
subprojects {
apply {
plugin("com.diffplug.spotless")
}
}
spotless {
java {
importOrder()
removeUnusedImports()
googleJavaFormat()
}
}
project.tasks.findByName("build")?.dependsOn(project.tasks.findByName("spotlessApply"))
I'm dumb.
I thought the jar would be in ./build/libs but it's actually in ./app/build/libs.
I've just set up a simple Hello World project with the following code in Kotlin + TornadoFX with IntelliJ IDEA Community. For some reason, if I type gradlew run at the command line, it manages to run the app by loading the MainApp class directly, but IntelliJ insists "class MainApp has no main() method", refusing to launch the app unless I explicitly write a main() function. Why is this? How can I (or can I at all) get IntelliJ to run the app the same way gradle is managing?
Here's the Kotlin source file
import tornadofx.*
// Define application with main View `Main`
class MainApp : App(Main::class)
// Define the view to display
class Main : View() {
// override the root view with our container with the label within
override val root = hbox {
// use the tornado kotlin dsl to add a label and set the text
label {
text = "Hello World"
}
}
}
And here's build.gradle.kts
plugins {
kotlin("jvm") version "1.3.72"
application
id("org.openjfx.javafxplugin") version "0.0.8"
}
javafx {
// Declare the javafx modules we need to use
modules("javafx.controls")
}
group = "org.example"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation("no.tornado:tornadofx:1.7.20")
}
application {
mainClassName = "MainApp"
}
tasks {
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
}
After downloading your test project there are a couple of things I see:
Your run configuration for "MainApp" is not using gradle but is using an Intellij managed project configured to build and run the application.
MainApp.kt does not have a "main" function for Intellij to run
I'm not an expert on Intellij or gradle so I can't explain why you can launch with gradle without main function but can't do so from Intellij. I expect that gradle can somehow inject a main function that can execute the JavaFX Application class defined by your MainApp (TornadoFX App class extends JavaFX Application class).
To get this working, you can either setup a run configuration to use gradle or add the main function to MyApp.kt and update the MainApp run configuration.
Use Gradle to Run from Intellij
Click the gradle tab on the right hand side of Intellij
Expand the kotlin-gradle configuration for Tasks -> application Note: You may need to click the "refresh" button on the top left of the gradle panel to refresh the list of available tasks.
Double click "run" and the gradle run task will be executed
When you do this, Intellij will automatically add a new configuration "test-project [run]"
Screenshot of Gradle Panel
Note: It is still a good idea to add the main function as described below and update your build.gradle.kts to use "MainAppKt". This will give you some flexibility in launching your application with Java 9+ without requiring module definitions (i.e. non-modular appplication)
Use Intellij to Run
Add the following code to your MainApp.kt
fun main(args: Array<String>) {
launch<MainApp>(args)
}
Edit your existing "MainApp" run configuration and change the Main class to "MainAppKt"
Click Ok to save and you can now run the "MainApp" configuration
Last note: If you are using Java 9+, you need to use a snapshot build of TornadoFX otherwise, you will run into problems.
Add the snapshot repository to your respoitories section with
maven("https://oss.sonatype.org/content/repositories/snapshots")
And update the TornadoFX version to "2.0.0-SNAPSHOT"
implementation("no.tornado:tornadofx:2.0.0-SNAPSHOT")
Old Answer
You are missing the mainClassName setting in your build.gradle.kts:
application {
mainClassName = "com.example.demo.app.MyApp"
}
Some notes:
If you have a main function that launches your app such as below, then you need to append "Kt" to the main class name where the function is located since a new class is created by kotlin to hold the static main function for the JVM. For example, if in the MyApp.kt file I had the main function below, I would need to use "com.example.demo.app.MyAppKt" in order to call the main function to run the application.
fun main(args: Array<String>) {
launch<MyApp>(args)
}
The syntax for the mainClassName is different depending on the gradle version and the language. I normally use groovy so I had to look it up for kts. The current gradle version 6.4 uses a different syntax than 6.3. The syntax I provided is for 6.3. I'm not sure when the change was made so you may need to verify the correct syntax for your gradle version.
I am trying to run Cucumber test scenarios (java project) through Gradle by following reference link-https://docs.cucumber.io/tools/java/#build-tools
This is my Build.gradle file
.output of command gradle build
this is showing scenarios are being recognized but WebDriver is not invoking and reports(build/tests/test/index.html) are showing zero.
I am very new to both gradle and cucumber and stuck in my first gradle project.
help will highly appreciated.
Change the glue parameter in task from "gradle.cucumber" to the classpath where your stepdefinitions are present. Switch to org.projectName.appname.tests and try
gradle.cucumber was the location of the package of stepdefinitions specific to the article.
Note- I got it with second another way is --> when we are using TestNG framework, in the build.gradle file we just need to add the following
test{
useTestNG()
}
I'm writing web service to run tests over junit using web endpoint, for this reason I have to keep all tests in sources.
The problem is that I want run tests dirctly from IDEA and command line, and with last I've stucked.
How can I run tests inside sources folder (src/main/java) through "gradle test"?
I'm not fammiliar with gradle, but I've tried to use
sourceSets {
test {
java.srcDir file('src/main/java')
resources.srcDir file('src/main/resources')
}
}
But it breaks project import using IDEA, my 2016.1.1 sometimes can't create two modules with same content root or expectedly marks sources as tests.
Can I solve this problem with customizing test {}, or somehow load test sources into SpringBoot launcher for web service?
Maybe I can extend gradle test task and customize it?
Try removing the file() calls
sourceSets {
test {
java.srcDir 'src/main/java'
resources.srcDir 'src/main/resources'
}
}
Tested on gradle 5.4.1 but should work on other versions too
I guess this post can help you to leanr how to start the tests inside the main sources:
How to run junit tests with gradle?.
I would specify one subfiolder in source which shall contain all your tests.
And you can specify the output directory, which should be different to the default output directory:
sourceSets {
test {
java.srcDir file('src/main/java/tests') // tests shall contain your tests
resources.srcDir file('src/main/resources')
output.resourcesDir = "build/classes/main"
}
}
I have a project full of tests that we use to query our environments. We run these tests using Gradle. I would like to run these tests from a standalone application to get rid of the Gradle dependency. I am using the gradle 'application' plugin and trying to run the JUnit tests using JUnitCore and everything is fine except I can't access my test classes from main.
I have
--main
--smokeTest
--longRunningTest
When I tell Gradle this it doesn't work.
sourceSets {
main {
java { srcDirs ['src/main/java', 'src/smokeTest/java'] }
}
}
It says "main" is not a recognized function. The java plugin is installed because I already have entries to define smokeTest and longRunningTest.
Not to steal the flame from #david-m-karr, just refining a bit:
sourceSets.main.java.srcDirs = ['src/main/java', 'src/smokeTest/java']
might work