Rajan
Rajan

Reputation: 1501

How to stop Gradle or Maven build under certain condition

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
}

Upvotes: 0

Views: 743

Answers (3)

Sean
Sean

Reputation: 179

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.

Upvotes: 1

Rene Groeschke
Rene Groeschke

Reputation: 28653

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")
}

Upvotes: 2

Opal
Opal

Reputation: 84776

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.

Upvotes: 0

Related Questions