0

I want to stop the gradle build from my program when certain resources are not found.

For example, My program searches for abc.properties. Currently when it is not found, all the possible error messages are displayed where the values of the property file are required. Instead I just want to display one line error message like "The file abc.properties is missing" , and then stop the build without displaying other lengthy messages. I have tried System.exit(), but is does not help.

try {
   properties.load(PropertyFactory.class.classLoader.getResourceAsStream("abc.properties") );
} catch(Exception e) {
   System.out.println("Please put a valid properties file in the src/main/resources folder named abc.properties");
   System.exit(1);//something that will stop the build and prevent other error messages
}
Opal
  • 81,889
  • 28
  • 189
  • 210
Rajan
  • 1,501
  • 4
  • 21
  • 37

3 Answers3

2

In gradle you can just put this in your buildscript:

if(!file("src/main/resources/abc.properties").exists()){
    throw new GradleException("Please put a valid properties file in the src/main/resources folder named abc.properties")
}
Rene Groeschke
  • 27,999
  • 10
  • 69
  • 78
1

Another alternative in Gradle would be declaring that file as a dependency. If it can't be resolved, build will fail. It's somewhat of a workaround, but it should work.

Sean
  • 169
  • 3
  • I would appreciate an explanation for this down-vote. Thanks. – Sean Oct 02 '14 at 10:15
  • I was not the downvote, but this doesn't work for me. I'm trying to have a dependency only for a buildType "release" and using "releaseCompile" dependency for my file never gets an error even when it's missing. – Colin M. Nov 18 '14 at 20:22
  • Are you sure your dependency is actually being resolved? – Sean Nov 19 '14 at 12:20
  • Nope, but for this to be a solution it should allow me to depend on it for a release, which isn't working. Unless I have the "releaseCompile" wrong? – Colin M. Nov 20 '14 at 02:05
0

You may throw an exception but this will result in a verbose output with the whole stacktrace included. Another way is to use ant.fail construct.

For instance:

if(1 != 2) {
   ant.fail('1 is not equal to 2')
}

There was also a similar question.

Community
  • 1
  • 1
Opal
  • 81,889
  • 28
  • 189
  • 210