We have created a gradle plugin which contains a simple task to check project structure and throw an exception if it doesn’t contain readme file.
I would like to fail/abort the build when this task throws an exception. I tried working with multiple exceptions that gradle provides. Though an exception is thrown, gradle still makes the build successful. Could anyone please
help me how to make my task the primordial one?
Can you provide more details on how you implemented that task? Usually just throwing a GradleException should cause your build to fail. This could look like this:
class ReadmeValidationPlugin implements Plugin<Project> {
void apply(Project project) {
project.tasks.create("checkForReadme") {
doLast {
if(!project.file("readme.md").exists()){
throw new GradleException("project must contain a readme file")
}
}
}
}
}
Thanks for your reply. I relooked at the stuff. Here’s what I’ve implemented.
class StructureTestTask extends DefaultTask {
@TaskAction
public void testProjStructure() {
String projRoot = project.path.toString()
def rootDir = new File(projRoot)
String[] findTheseFiles = ['README.md','CHANGENOTES.md']
String[] files = rootDir.list(new MyFileFilter())
if(!files.grep().containsAll(findTheseFiles)) {
throw new GradleException('Files' + findTheseFiles + ' were not found in the root directory of ' + project.name)
}
println 'Structure Test Pass!'
}
private class MyFileFilter implements FilenameFilter {
public boolean accept(File f, String filename) {
return filename.endsWith(".md")
}
}
}
My root project contains 3 sub-projects, api, client and services.
When I do a gradle build structureTest - ‘services’ is the last sub-project that gets built and my task is running n the sub-project but not the root project. How do I make sure that my task always runs on the root project where the build.gradle resides?
Problem was this - I’d wrapped this task inside a plugin. The plugin was applied on -services subProject inside my root project’s build.gradle file. I fixed it by making a reference to project.rootProject