执行Ant任务时如何保持读取日志?
How to keep reading logs when executing Ant task?
我有以下代码用于从 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);
上述任务开始显示 console
中 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());
}
}
});
之后的方法returnsconsoleLogs
。
我有以下代码用于从 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);
上述任务开始显示 console
中 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());
}
}
});
之后的方法returnsconsoleLogs
。