0

So what I would like to do is run my Java Application using Gradle but also pass an .csv & .xml filepaths to my main class in order to use them in my bufferedReader & I have no idea how it should be done. My application is not build to jar, it's supposed to be run with gradle run task. I want the user to be able to specify their own xml and csv file to use( in the command line) and then run the Java app, not being asked about paths to the files inside the app. So for example let's take this line of code: Reader reader = Files.newBufferedReader(Paths.get("Here is the place I want gradle to include the path to csv"));

flp wgda
  • 1
  • 3

1 Answers1

-1
// sample gradle task to execute a java jar
task runFromGradle() {
    print("java -jar myJar.jar path/to/some.csv".execute().text)
}

// some sample class with a main that accepts args. first arg is the csv path
class Scratch {
    public static void main(String[] args) {
        String csvPath = args[0];
        Reader reader = Files.newBufferedReader(Paths.get(csvPath));
        // do whatever with reader...
    }
}

# from command line
$ gradle runFromGradle

To @lance-java's comment of course there are other ways to accomplish the same task. He alluded to another the JavaExec task you can extend. Here is an example of that if you wish to use that path.

// note the `JavaExec` task is only available from the java plugin,
// this may or may not what your app needs
// reference: https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/JavaExec.html
apply plugin: 'java'

task runFromGradle(type: JavaExec) {
  // here gradle has access to the raw sourcesets
  // so we can directly use them for the runtime
  classpath = sourceSets.main.runtimeClasspath

  main = 'com.me.Main'

  // arguments to pass to the application
  args 'path/to/some.csv'
}

# from command line
$ gradle runFromGradle

# java class is the same
JBirdVegas
  • 10,855
  • 2
  • 44
  • 50
  • 1
    Thank you very much, this is exactly it! Turns out I was lacking some fundamental Java knowledge as this is the first time ever, I'm gonna use main method arguments and I couldn't figure out how 'args' actually work in gradle – flp wgda Oct 10 '18 at 14:48
  • The `GString` (gradle's string) is capable of executing as a shell process making the args exactly as they would be from the command line. Glad to help here is another reference if you would like to read up on gradle's string extensions: http://docs.groovy-lang.org/latest/html/groovy-jdk/java/lang/String.html More on java args: https://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html Good Luck! – JBirdVegas Oct 10 '18 at 15:06
  • This is most definitely not recommended practice!! Please see [JavaExec](https://docs.gradle.org/current/dsl/org.gradle.api.tasks.JavaExec.html) task – lance-java Oct 10 '18 at 15:26
  • Updated to include an example of `JavaExec`. The only problem is that comes from the `java` plugin which could conflict with other plugins depending on the project. OP doesn't specify that the gradle is only used for java. The first answer is more flexible between all plugins but your correct if they are using the `java` plugin anyways then sure `JavaExec` is the obvious answer. – JBirdVegas Oct 10 '18 at 16:09
  • Your first task is awful! It runs the process in Gradle's configuration phase, not the execution phase so it will run for every build (regardless of what tasks were specified by the user). JavaExec does NOT require the java plugin to be applied to the project – lance-java Oct 10 '18 at 23:06