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。
我該怎麼做?