Code review comment: Let's switch these conditions so that we can avoid iterating the modules every time.

Code before review:
public void execute() {
if (this.helpFlag) {
String commandUsageInfo = BLauncherCmd.getCommandUsageInfo(BUILD_COMMAND);
this.errStream.println(commandUsageInfo);
return;
}
Project project;
if (FileUtils.hasExtension(this.projectPath)) {
if (coverage != null && coverage) {
this.outStream.println("Code coverage is not yet supported with single bal files. Ignoring the flag " +
"and continuing the test run...\n");
}
coverage = false;
}
BuildOptions buildOptions = constructBuildOptions();
boolean isSingleFileBuild = false;
if (FileUtils.hasExtension(this.projectPath)) {
if (this.compile) {
CommandUtil.printError(this.errStream,
"'-c' or '--compile' can only be used with a Ballerina package.", null, false);
CommandUtil.exitError(this.exitWhenFinish);
return;
}
try {
project = SingleFileProject.load(this.projectPath, buildOptions);
} catch (ProjectException e) {
CommandUtil.printError(this.errStream, e.getMessage(), null, false);
CommandUtil.exitError(this.exitWhenFinish);
return;
}
isSingleFileBuild = true;
} else {
if (null != this.output) {
CommandUtil.printError(this.errStream,
"'-o' and '--output' are only supported when building a single Ballerina " +
"file.",
"bal build -o <output-file> <ballerina-file> ",
true);
CommandUtil.exitError(this.exitWhenFinish);
return;
}
try {
project = BuildProject.load(this.projectPath, buildOptions);
} catch (ProjectException e) {
CommandUtil.printError(this.errStream, e.getMessage(), null, false);
CommandUtil.exitError(this.exitWhenFinish);
return;
}
}
if (isProjectEmpty(project)) {
if (!(this.compile && project.currentPackage().compilerPluginToml().isPresent())) {
CommandUtil.printError(this.errStream, "package is empty. please add at least one .bal file", null,
false);
CommandUtil.exitError(this.exitWhenFinish);
return;
}
}
if (this.compile && project.currentPackage().ballerinaToml().get().tomlDocument().toml()
.getTable("package").isEmpty()) {
CommandUtil.printError(this.errStream,
"'package' information not found in " + ProjectConstants.BALLERINA_TOML,
null,
true);
CommandUtil.exitError(this.exitWhenFinish);
return;
}
if (!project.buildOptions().skipTests() && this.debugPort != null) {
System.setProperty(SYSTEM_PROP_BAL_DEBUG, this.debugPort);
}
if (project.buildOptions().codeCoverage()) {
if (coverageFormat != null) {
if (!coverageFormat.equals(JACOCO_XML_FORMAT)) {
String errMsg = "unsupported coverage report format '" + coverageFormat + "' found. Only '" +
JACOCO_XML_FORMAT + "' format is supported.";
CommandUtil.printError(this.errStream, errMsg, null, false);
CommandUtil.exitError(this.exitWhenFinish);
return;
}
}
} else {
if (includes != null) {
this.outStream.println("warning: ignoring --includes flag since code coverage is not enabled");
}
if (coverageFormat != null) {
this.outStream.println("warning: ignoring --coverage-format flag since code coverage is not " +
"enabled");
}
}
try {
readSettings();
} catch (SettingsTomlException e) {
this.outStream.println("warning: " + e.getMessage());
}
TaskExecutor taskExecutor = new TaskExecutor.TaskBuilder()
.addTask(new CleanTargetDirTask(), isSingleFileBuild)
.addTask(new ResolveMavenDependenciesTask(outStream))
.addTask(new CompileTask(outStream, errStream))
.addTask(new RunTestsTask(outStream, errStream, includes, coverageFormat),
project.buildOptions().skipTests() || isSingleFileBuild)
.addTask(new CreateBalaTask(outStream), isSingleFileBuild || !this.compile)
.addTask(new CreateExecutableTask(outStream, this.output), this.compile)
.build();
taskExecutor.executeTasks(project);
if (this.exitWhenFinish) {
Runtime.getRuntime().exit(0);
}
}

Line under review: if (!(this.compile && project.currentPackage().compilerPluginToml().isPresent())) {

Code after addressing the review:
public void execute() {
if (this.helpFlag) {
String commandUsageInfo = BLauncherCmd.getCommandUsageInfo(BUILD_COMMAND);
this.errStream.println(commandUsageInfo);
return;
}
Project project;
if (FileUtils.hasExtension(this.projectPath)) {
if (coverage != null && coverage) {
this.outStream.println("Code coverage is not yet supported with single bal files. Ignoring the flag " +
"and continuing the test run...\n");
}
coverage = false;
}
BuildOptions buildOptions = constructBuildOptions();
boolean isSingleFileBuild = false;
if (FileUtils.hasExtension(this.projectPath)) {
if (this.compile) {
CommandUtil.printError(this.errStream,
"'-c' or '--compile' can only be used with a Ballerina package.", null, false);
CommandUtil.exitError(this.exitWhenFinish);
return;
}
try {
project = SingleFileProject.load(this.projectPath, buildOptions);
} catch (ProjectException e) {
CommandUtil.printError(this.errStream, e.getMessage(), null, false);
CommandUtil.exitError(this.exitWhenFinish);
return;
}
isSingleFileBuild = true;
} else {
if (null != this.output) {
CommandUtil.printError(this.errStream,
"'-o' and '--output' are only supported when building a single Ballerina " +
"file.",
"bal build -o <output-file> <ballerina-file> ",
true);
CommandUtil.exitError(this.exitWhenFinish);
return;
}
try {
project = BuildProject.load(this.projectPath, buildOptions);
} catch (ProjectException e) {
CommandUtil.printError(this.errStream, e.getMessage(), null, false);
CommandUtil.exitError(this.exitWhenFinish);
return;
}
}
if (!(this.compile && project.currentPackage().compilerPluginToml().isPresent())) {
if (isProjectEmpty(project)) {
CommandUtil.printError(this.errStream, "package is empty. please add at least one .bal file", null,
false);
CommandUtil.exitError(this.exitWhenFinish);
return;
}
}
if (this.compile && project.currentPackage().ballerinaToml().get().tomlDocument().toml()
.getTable("package").isEmpty()) {
CommandUtil.printError(this.errStream,
"'package' information not found in " + ProjectConstants.BALLERINA_TOML,
null,
true);
CommandUtil.exitError(this.exitWhenFinish);
return;
}
if (!project.buildOptions().skipTests() && this.debugPort != null) {
System.setProperty(SYSTEM_PROP_BAL_DEBUG, this.debugPort);
}
if (project.buildOptions().codeCoverage()) {
if (coverageFormat != null) {
if (!coverageFormat.equals(JACOCO_XML_FORMAT)) {
String errMsg = "unsupported coverage report format '" + coverageFormat + "' found. Only '" +
JACOCO_XML_FORMAT + "' format is supported.";
CommandUtil.printError(this.errStream, errMsg, null, false);
CommandUtil.exitError(this.exitWhenFinish);
return;
}
}
} else {
if (includes != null) {
this.outStream.println("warning: ignoring --includes flag since code coverage is not enabled");
}
if (coverageFormat != null) {
this.outStream.println("warning: ignoring --coverage-format flag since code coverage is not " +
"enabled");
}
}
try {
readSettings();
} catch (SettingsTomlException e) {
this.outStream.println("warning: " + e.getMessage());
}
TaskExecutor taskExecutor = new TaskExecutor.TaskBuilder()
.addTask(new CleanTargetDirTask(), isSingleFileBuild)
.addTask(new ResolveMavenDependenciesTask(outStream))
.addTask(new CompileTask(outStream, errStream))
.addTask(new RunTestsTask(outStream, errStream, includes, coverageFormat),
project.buildOptions().skipTests() || isSingleFileBuild)
.addTask(new CreateBalaTask(outStream), isSingleFileBuild || !this.compile)
.addTask(new CreateExecutableTask(outStream, this.output), this.compile)
.build();
taskExecutor.executeTasks(project);
if (this.exitWhenFinish) {
Runtime.getRuntime().exit(0);
}
}