經過幾個小時試圖解決這個問題的配置,我咬緊牙關,簡單地寫了我自己的插件,擴展了AntRun插件。
@Mojo(name = "run", threadSafe = true, requiresDependencyResolution = ResolutionScope.TEST)
public class CustomAntMojo
extends AntRunMojo
{
@Component
private PluginDescriptor pluginDescriptor;
public void execute()
throws MojoExecutionException
{
File buildDirectory = new File(getMavenProject().getBuild().getOutputDirectory());
// add the build directory to the classpath for the classloader
try {
ClassRealm realm = pluginDescriptor.getClassRealm();
realm.addURL(buildDirectory.toURI().toURL());
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
// configure the log4j logger to output the ant logs to the maven log
BasicConfigurator.configure(new MavenLoggerLog4jBridge(getLog()));
super.execute();
}
}
隨着MavenLoggerLog4jBridge類被用來從我的Ant任務的Log4j的輸出轉換到Maven記錄器(https://stackoverflow.com/a/6948208/827480):
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.maven.plugin.logging.Log;
public class MavenLoggerLog4jBridge extends AppenderSkeleton {
private Log logger;
public MavenLoggerLog4jBridge(Log logger) {
this.logger = logger;
}
protected void append(LoggingEvent event) {
int level = event.getLevel().toInt();
String msg = event.getMessage().toString();
if (level <= Level.DEBUG_INT) {
this.logger.debug(msg);
} else if (level == Level.INFO_INT) {
this.logger.info(msg);
} else if (level == Level.WARN_INT) {
this.logger.warn(msg);
} else if (level == Level.ERROR_INT || level == Level.FATAL_INT) {
this.logger.error(msg);
}
}
public void close() {
}
public boolean requiresLayout() {
return false;
}
}
希望這可能是一些這是使用Maven 3.2.5完成使用或協助未來某人。
你想做什麼?爲什麼使用maven-antrun-plugin? – khmarbaise
@khmarbaise我試圖將遺留的螞蟻項目轉換爲maven。不幸的是,ant構建的一部分使用了一個已停用的Kodo Ant任務,用於JDO增強實體,因爲我沒有找到與此相同的實體。所以我只是想通過maven-Antrim插件調用ant任務。但正如我所解釋的,該任務使用類加載器在類路徑中查找屬性文件。但由於插件類加載器不包含構建源/輸出,因此無法找到它並且任務失敗。修改螞蟻任務不是一個真正的選擇,所以我正在尋找修改插件加載器的類路徑。 –