use of org.gradle.tooling.ModelBuilder in project liferay-ide by liferay.
the class GradleTooling method getModel.
public static <T> T getModel(Class<T> modelClass, File cacheDir, File projectDir) throws Exception {
T retval = null;
GradleConnector connector = GradleConnector.newConnector().forProjectDirectory(projectDir);
ProjectConnection connection = null;
try {
connection = connector.connect();
ModelBuilder<T> modelBuilder = (ModelBuilder<T>) connection.model(modelClass);
File depsDir = new File(cacheDir, "deps");
depsDir.mkdirs();
String path = depsDir.getAbsolutePath();
path = path.replaceAll("\\\\", "/");
_extractJar(depsDir, "com.liferay.blade.gradle.model");
_extractJar(depsDir, "com.liferay.blade.gradle.plugin");
String initScriptTemplate = CoreUtil.readStreamToString(GradleTooling.class.getResourceAsStream("init.gradle"));
String initScriptContents = initScriptTemplate.replaceFirst("%deps%", path);
File scriptFile = new File(cacheDir, "init.gradle");
if (FileUtil.notExists(scriptFile)) {
scriptFile.createNewFile();
}
FileUtil.writeFileFromStream(scriptFile, new ByteArrayInputStream(initScriptContents.getBytes()));
ModelBuilder<T> builder = modelBuilder.withArguments("--init-script", scriptFile.getAbsolutePath());
retval = builder.get();
} catch (Exception e) {
GradleCore.logError("get gradle custom model error", e);
} finally {
if (connection != null) {
connection.close();
}
}
return retval;
}
Aggregations