ProjectConnection connection = GradleConnector.newConnector()
.forProjectDirectory(buildFile.getParentFile()).connect();
try {
BuildLauncher build = connection.newBuild();
// select tasks to run:
build.forTasks(taskName);
List<String> buildArgs = new ArrayList<String>();
// buildArgs.add("-b");
// buildArgs.add(buildFilePath);
buildArgs.add("-stacktrace");
buildArgs.add("-info");
if (params.length > 0) {
for (int i = 0; i < params.length; i++) {
buildArgs.add("-P" + params[i]);
}
}
logger.info(Arrays.toString(buildArgs.toArray()));
build.withArguments(buildArgs.toArray(new String[] {}));
// if you want to listen to the progress events:
ProgressListener listener = null; // use your implementation
// build.addProgressListener(listener);
// kick the build off:
build.run();
} finally {
connection.close();
}
}