2

I try to pass arguments from command line to gradle.

My build.gradle is:

task execute(type:JavaExec) {
   main = mainClass
   classpath = sourceSets.main.output
}

When I do:

gradle -PmainClass=Hello execute

I get this:

FAILURE: Build failed with an exception.

* Where:
Build file '/home/example2/build.gradle' line: 7

* What went wrong:
A problem occurred evaluating root project 'example2'.
> Could not find property 'sourceSets' on task ':execute'.

My question is that what should I provide for main? name of any java file package? any specific path? What should actually be sourceSet?

user3409650
  • 505
  • 3
  • 12
  • 25
  • BTW I am following this: http://stackoverflow.com/questions/21358466/gradle-to-execute-java-class-without-modifying-build-gradle If it works there why not for me? – user3409650 Sep 10 '14 at 09:06
  • if your project structure follows Gradle standards, then you don't need to define sourceSet { ...main { .. } test { .. } .. blahTest { .. } .. } section. If your project doesn't follows the default structure, then you have to define sourceSets section in your build.gradle file. You can see one build.gradle file at following link to get an idea, you will also have to apply plugin: 'java' or etc: http://stackoverflow.com/questions/18879250/jacoco-unit-and-integration-tests-coverage-individual-and-overall – AKS Sep 11 '14 at 15:19
  • One reason it worked for him could be, what if his structure matches the default Gradle project structure for sourceSets (in that case you don't need to define sourceSets section for main, test, etc). Other can be, if sourceSet section is defined at a Global Gradle level file i.e. any .gradle file inside $GRADLE_HOME/init.d/common.gradle file, then you don't need to specify the sourceSet section in any project's build.gradle which uses that Gradle. -OR he just didn't show that piece in his post (assuming it was not important to the scope of his question in his post). – AKS Sep 11 '14 at 15:23

1 Answers1

0

Your minimal build.gradle file should look like this:

apply plugin: 'java'

task execute(type:JavaExec) {
   main = mainClass
   classpath = sourceSets.main.output
}

And you need a java class located in src/main/java/Hello.java like this:

public class Hello {

    public static void main(String[] a) {}

}
tkruse
  • 10,222
  • 7
  • 53
  • 80