我从DefaultLogger
到org.apache.tools.ant.DefaultLogger
:创建了一个子类
public class ExtendedLogger extends DefaultLogger {
// My stuff
}
我在这里获取了信息。
所以我重载了DefaultLogger
:中的一些方法
@Override
public void targetStarted(BuildEvent event){
System.out.println("Logger : "+"[TARGET STARTED]n");
}
@Override
public void targetFinished(BuildEvent event){
System.out.println("Logger : "+"[TARGET FINISHED]n");
}
效果很好(我可以看到输出)。
这些方法也起作用:
public void taskStarted(BuildEvent event);
public void taskFinished(BuildEvent event);
但我有问题与:
@Override
public void buildFinished(BuildEvent event) {
System.out.println("Logger : "+"[BUILD FINISHED]n");
}
@Override
public void buildStarted(BuildEvent event) {
System.out.println("Logger : "+"[BUILD STARTED]n");
}
用这些方法是不可能的!
我哪里错了?
谢谢。
2016年1月12日编辑:
我刚刚尝试了一个简单的testBuild.xml,比如@Vaspar说:
<?xml version="1.0" encoding="UTF-8"?>
<project name="MyProject" default="Init" basedir=".">
<target name="Init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<echo message="INIT-ING"/>
</target>
<!-- targets used in my project -->
<target name="Clean" description="Clean output directory.">
<echo>Clean</echo>
</target>
<target name="CleanAutogenerated" description="Clean auto-generated folder.">
<echo>CleanAutogenerated</echo>
</target>
<target name="AutoGenerateTopics" description="Manual specific targets.">
<echo>AutoGenerateTopics</echo>
</target>
<target name="BuildDocument" description="Build full documentation">
<echo>BuildDocument</echo>
</target>
</project>
输出为:
Logger : [TARGET STARTED] org.apache.tools.ant.BuildEvent[source=Clean]
Logger : [TASK STARTED] org.apache.tools.ant.BuildEvent[source=org.apache.tools.ant.UnknownElement@12f33e1]
[echo] Clean
Logger : [TASK FINISHED] org.apache.tools.ant.BuildEvent[source=org.apache.tools.ant.UnknownElement@12f33e1]
Logger : [TARGET FINISHED] org.apache.tools.ant.BuildEvent[source=Clean]
CleanDocumentTask DONE
Logger : [TARGET STARTED] org.apache.tools.ant.BuildEvent[source=CleanAutogenerated]
Logger : [TASK STARTED] org.apache.tools.ant.BuildEvent[source=org.apache.tools.ant.UnknownElement@1e2862f]
[echo] CleanAutogenerated
Logger : [TASK FINISHED] org.apache.tools.ant.BuildEvent[source=org.apache.tools.ant.UnknownElement@1e2862f]
Logger : [TARGET FINISHED] org.apache.tools.ant.BuildEvent[source=CleanAutogenerated]
CleanAutoGenerateTask DONE
Logger : [TARGET STARTED] org.apache.tools.ant.BuildEvent[source=AutoGenerateTopics]
Logger : [TASK STARTED] org.apache.tools.ant.BuildEvent[source=org.apache.tools.ant.UnknownElement@19bf1ed]
[echo] AutoGenerateTopics
Logger : [TASK FINISHED] org.apache.tools.ant.BuildEvent[source=org.apache.tools.ant.UnknownElement@19bf1ed]
Logger : [TARGET FINISHED] org.apache.tools.ant.BuildEvent[source=AutoGenerateTopics]
AutoGenerateTask DONE
Logger : [TARGET STARTED] org.apache.tools.ant.BuildEvent[source=BuildDocument]
Logger : [TASK STARTED] org.apache.tools.ant.BuildEvent[source=org.apache.tools.ant.UnknownElement@10b85cb]
[echo] BuildDocument
Logger : [TASK FINISHED] org.apache.tools.ant.BuildEvent[source=org.apache.tools.ant.UnknownElement@10b85cb]
Logger : [TARGET FINISHED] org.apache.tools.ant.BuildEvent[source=BuildDocument]
DocumentTask DONE
请注意,CleanDocumentTask DONE
、DocumentTask DONE
等…都是来自我的项目(而非ant)的纯Java输出。
正如您所看到的,targetStarted
或targetFinished
:/仍然没有输出
编辑12/01/2016(2):
我的ExtendedLogger.java:的内容
import javax.swing.SwingUtilities;
import org.apache.tools.ant.BuildEvent;
import org.apache.tools.ant.DefaultLogger;
/*
* This class has been wrote to deals with ant build outputs.
*
* However, buildFinished and buildStarted seems not to work.
* See details at :
*
* http://api.dpml.net/ant/1.7.0/org/apache/tools/ant/DefaultLogger.html
*
*/
public class ExtendedLogger extends DefaultLogger {
/*
* public class ColorPane extends JTextPane
*
* (personal class : JTextPane + Ansi coloring)
*/
ColorPane colorPane = null;
public ExtendedLogger(ColorPane c){
super();
if (c != null){
colorPane = c;
} else {
System.out.println("ColorPane given to constructor is null. Exiting program.");
System.exit(0);
}
}
@Override
public void buildFinished(BuildEvent event) {
ColorPane.appendInEDT("[BUILD FINISHED]n", ColorPane.B_Yellow, colorPane);
System.out.println("Logger : "+"[BUILD FINISHED]n");
}
@Override
public void buildStarted(BuildEvent event) {
ColorPane.appendInEDT("[BUILD STARTED]n", ColorPane.B_Yellow, colorPane);
System.out.println("Logger : "+"[BUILD STARTED]n");
}
@Override
public void log(String message){
appendInEDT(message);
}
private void appendInEDT(String s){
/*
* Detach the graphical modifications in the Event Dispatch Thread (EDT)
*/
final String newLine = s;
Thread t = new Thread(new Runnable(){
public void run(){
colorPane.append(ColorPane.B_White, newLine+"n");
}
});
if (SwingUtilities.isEventDispatchThread()) {
t.start();
} else {
SwingUtilities.invokeLater(t);
}
}
}
令人惊讶。我使用了你的代码示例,我也可以获得buildStarted/buildFinished日志。
自定义记录器
import org.apache.tools.ant.BuildEvent;
import org.apache.tools.ant.DefaultLogger;
public class CustomLogger extends DefaultLogger{
@Override
public void targetStarted(BuildEvent event){
System.out.println("Logger : "+"[TARGET STARTED] " + event);
}
@Override
public void targetFinished(BuildEvent event){
System.out.println("Logger : "+"[TARGET FINISHED] " + event);
}
@Override
public void taskStarted(BuildEvent event) {
System.out.println("Logger : "+"[TASK FINISHED] " + event);
}
@Override
public void taskFinished(BuildEvent event) {
System.out.println("Logger : "+"[TASK FINISHED] " + event);
}
@Override
public void buildFinished(BuildEvent event) {
System.out.println("Logger : "+"[BUILD FINISHED] " + event);
}
@Override
public void buildStarted(BuildEvent event) {
System.out.println("Logger : "+"[BUILD STARTED] " + event);
}
}
build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="MyProject" default="init" basedir=".">
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<echo message="INIT-ING"/>
</target>
</project>
下面是输出。
输出
/DummyTest> ant -logger CustomLogger
Buildfile: /DummyTest/build.xml
Logger : [BUILD STARTED] org.apache.tools.ant.BuildEvent[source=org.apache.tools.ant.Project@1318bd3c]
Logger : [TARGET STARTED] org.apache.tools.ant.BuildEvent[source=init]
[tstamp] Logger : [TASK FINISHED] org.apache.tools.ant.BuildEvent[source=org.apache.tools.ant.UnknownElement@31623389]
Logger : [TASK FINISHED] org.apache.tools.ant.BuildEvent[source=org.apache.tools.ant.UnknownElement@31623389]
[echo] Logger : [TASK FINISHED] org.apache.tools.ant.BuildEvent[source=org.apache.tools.ant.UnknownElement@636c7a8f]
[echo] INIT-ING
Logger : [TASK FINISHED] org.apache.tools.ant.BuildEvent[source=org.apache.tools.ant.UnknownElement@636c7a8f]
Logger : [TARGET FINISHED] org.apache.tools.ant.BuildEvent[source=init]
Logger : [BUILD FINISHED] org.apache.tools.ant.BuildEvent[source=org.apache.tools.ant.Project@1318bd3c]