gpt4 book ai didi

java - 执行Ant任务时如何持续读取日志?

转载 作者:行者123 更新时间:2023-12-01 19:04:54 27 4
gpt4 key购买 nike

我有以下代码用于从 Java 执行 Ant 任务:

public class AntExecutor {
/**
* To execute a target specified in the Ant build.xml file
*
* @param buildXmlFileFullPath
* @param target
*/
public static boolean executeAntTask(String buildXmlFileFullPath, String target, Map<String, String> propertiesMap) {
boolean success = false;
DefaultLogger consoleLogger = getConsoleLogger();

// Prepare Ant project
Project project = new Project();
File buildFile = new File(buildXmlFileFullPath);
project.setUserProperty("ant.file", buildFile.getAbsolutePath());

if(propertiesMap != null && !propertiesMap.isEmpty()) {
for (Map.Entry<String, String> entrySet : propertiesMap.entrySet()) {
project.setProperty(entrySet.getKey(), entrySet.getValue());
}
}

project.addBuildListener(consoleLogger);

// Capture event for Ant script build start / stop / failure
try {
project.fireBuildStarted();
project.init();
ProjectHelper projectHelper = ProjectHelper.getProjectHelper();
project.addReference("ant.projectHelper", projectHelper);
projectHelper.parse(project, buildFile);

// If no target specified then default target will be executed.
String targetToExecute = (target != null && target.trim().length() > 0) ? target.trim() : project.getDefaultTarget();
project.executeTarget(targetToExecute);
project.fireBuildFinished(null);
success = true;
} catch (BuildException buildException) {
project.fireBuildFinished(buildException);
throw new RuntimeException("!!! Unable to restart the IEHS App !!!", buildException);
}

return success;
}

/**
* Logger to log output generated while executing ant script in console
*
* @return
*/
private static DefaultLogger getConsoleLogger() {
DefaultLogger consoleLogger = new DefaultLogger();
consoleLogger.setErrorPrintStream(System.err);
consoleLogger.setOutputPrintStream(System.out);
consoleLogger.setMessageOutputLevel(Project.MSG_INFO);

return consoleLogger;
}

}

并从某种方法调用它:

AntExecutor.executeAntTask(buildFile.getPath(), "target name", propertiesMap);

上述任务开始在控制台中显示来自Ant的输出日志。如何读取在控制台中打印的控制台日志,或保存作为 ant 任务的一部分打印的输出日志?

最佳答案

我没有使用 getConsoleLogger,而是使用 BuildListener,如下所示:

    List<String> consoleLogs = new ArrayList<>();
project.addBuildListener(new BuildListener() {
@Override
public void buildStarted(BuildEvent buildEvent) {

}

@Override
public void buildFinished(BuildEvent buildEvent) {
if(buildEvent.getException() != null && StringUtils.hasText(buildEvent.getException().getMessage())){
consoleLogs.add(buildEvent.getException().getMessage());
}
}

@Override
public void targetStarted(BuildEvent buildEvent) {

}

@Override
public void targetFinished(BuildEvent buildEvent) {

}

@Override
public void taskStarted(BuildEvent buildEvent) {

}

@Override
public void taskFinished(BuildEvent buildEvent) {

}

@Override
public void messageLogged(BuildEvent buildEvent) {
if(StringUtils.hasText(buildEvent.getMessage())) {
consoleLogs.add(buildEvent.getMessage());
}
}
});

之后该方法返回consoleLogs

关于java - 执行Ant任务时如何持续读取日志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59578444/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com