1
我想通过我的Java代码运行Maven编译。因此,我使用了Maven Embedder解释here的使用示例。为Maven Embedder执行设置记录器
这工作得很好,除了我想将Maven Embedder写的所有日志重定向到我自己的Logger。所以,我创建了自己MavenEmbedderLogger
(out
是我的PrintStream
):
class MvnLogger extends AbstractMavenEmbedderLogger {
public void error(String s, Throwable throwable) {
out.println("[error] " + s);
print(throwable);
}
public void info(String s, Throwable throwable) {
out.println("[info] " + s);
print(throwable);
}
...
public void close() {
}
private void print(Throwable t) {
if (t != null) {
t.printStackTrace(out);
}
}
}
,然后,我设置这个记录器嵌入器:
Configuration config = new DefaultConfiguration();
config.setUserSettingsFile(new File("..."));
config.setClassLoader(Thread.currentThread().getContextClassLoader());
ConfigurationValidationResult validationResult = MavenEmbedder.validateConfiguration(config);
if (validationResult.isValid()) {
try {
MavenEmbedder embedder = new MavenEmbedder(config);
// SET THE LOGGER
embedder.setLogger(new MvnLogger());
MavenExecutionRequest request = new DefaultMavenExecutionRequest();
request.setBaseDirectory(path);
request.setGoals(Arrays.asList(new String[] { "clean", "install" }));
MavenExecutionResult result = embedder.execute(request);
...
然而,当我执行该代码,Maven的所有日志都显示在默认的Logger中(在我的情况下,是System.out
),而不是我的Logger。
我该怎么做?