1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| @Component @Slf4j public class GradleBuildManagerImpl implements GradleBuildManager {
@Override public void compiler(CompileDTO dto) { log.info("开始编译Gradle项目,编译工具路径: {},代码路径: {}, 编译参数: {}", dto.getBuildToolPath(), dto.getCodePath(), dto.getCommands()); long startTime = System.currentTimeMillis(); ByteArrayOutputStream errorStream = new ByteArrayOutputStream(); PrintStream originalErrStream = System.err; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); PrintStream originalOutStream = System.out; try (ProjectConnection connection = GradleConnector.newConnector() .forProjectDirectory(new File(dto.getCodePath())) .useGradleUserHomeDir(CoverageConstant.DEFAULT_GRADLE_USER_HOME) .useInstallation(new File(dto.getBuildToolPath())) .connect()) { BuildLauncher build = connection.newBuild(); build.setJavaHome(new File(dto.getJdkPath())); System.setErr(new PrintStream(errorStream)); System.setOut(new PrintStream(outputStream)); build.forTasks(dto.getCommands().toArray(new String[0])) .setStandardOutput(System.out) .setStandardError(System.err) .withArguments(GradleCommand.EXCLUDE, GradleCommand.TEST) .setColorOutput(false) .setJvmArguments("-Xmx512m"); build.run(); log.info("编译日志:\n {}", outputStream); } catch (Exception e) { log.error("代码: {} 编译失败, 异常详情: {}", dto.getCodePath(), ExceptionUtils.getRootCauseMessage(e)); log.error("编译异常日志:\n {}", errorStream); throw new ServiceException("编译失败"); } finally { System.setErr(originalErrStream); System.setOut(originalOutStream); } log.info("结束编译Gradle项目,编译耗时: {} s", (System.currentTimeMillis() - startTime) / 1000); }
@Override public List<String> modules(String jdkPath, String gradlePath, String codePath) { String originJavaHome = System.getProperty(SystemPropertiesConstant.JAVA_HOME); System.setProperty(SystemPropertiesConstant.JAVA_HOME, jdkPath); log.info("开始扫描Gradle项目模块,编译工具路径: {},代码路径: {}", gradlePath, codePath); try (ProjectConnection connection = GradleConnector.newConnector() .forProjectDirectory(new File(codePath)) .useInstallation(new File(gradlePath)) .connect()) { GradleProject model = connection.getModel(GradleProject.class); return model.getChildren().stream().map(GradleProject::getName).collect(Collectors.toList()); } catch (Exception e) { log.error("代码: {} 模块扫描失败, 异常详情: {}", codePath, ExceptionUtils.getRootCauseMessage(e)); throw new ServiceException("模块扫描失败"); } finally { System.setProperty(SystemPropertiesConstant.JAVA_HOME, originJavaHome); } } }
|