Run No arg main class
Java
package examples;
public class TestMain {
public static void main(String[] params) {
System.out.println("hello");
}
}
build.gradle
apply plugin: 'java'
apply plugin: 'application'
mainClassName= 'examples.TestMain'
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:4.12'
}
Run
$ gradle run
> Task :run
hello
Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.3/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 952ms
3 actionable tasks: 3 executed
Run main class with hard code arg
Java
package examples;
public class TestMain {
public static void main(String[] params) {
System.out.println("hello " + params[0]);
}
}
build.gradle
apply plugin: 'java'
apply plugin: 'application'
mainClassName= ‘examples.TestMain'
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:4.12'
}
run.args = ['abc']
Run
apply plugin: 'java'
apply plugin: 'application'
mainClassName= 'examples.TestMain'
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:4.12'
}
run.args = ['abc']
Run main class with command line arg
Java
package examples;
public class TestMain {
public static void main(String[] params) {
System.out.println("hello " + params[0]);
}
}
build.gradle
apply plugin: 'java'
apply plugin: 'application'
mainClassName= 'examples.TestMain'
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:4.12'
}
run.args = [project.args] //simply args can work as well
Run
$ gradle clean run -Pargs=ABC
> Task :run
hello ABC
Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.3/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 760ms
4 actionable tasks: 4 executed
沒有留言:
張貼留言