From bbf06ea4736842536b58f0bc3c33a386fc0a9982 Mon Sep 17 00:00:00 2001 From: Matt Bowersox Date: Wed, 8 Nov 2023 14:13:38 -0600 Subject: [PATCH] Use ant task for container builds --- pom.xml | 5 + .../common/plugins/util/AntTaskFactory.java | 94 +++++++++++++++++++ .../tools/common/plugins/util/DevUtil.java | 25 ++++- 3 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 src/main/java/io/openliberty/tools/common/plugins/util/AntTaskFactory.java diff --git a/pom.xml b/pom.xml index 3136b7c1..0c40f407 100644 --- a/pom.xml +++ b/pom.xml @@ -65,5 +65,10 @@ jackson-core 2.15.2 + + org.slf4j + slf4j-api + 2.0.9 + diff --git a/src/main/java/io/openliberty/tools/common/plugins/util/AntTaskFactory.java b/src/main/java/io/openliberty/tools/common/plugins/util/AntTaskFactory.java new file mode 100644 index 00000000..e49ef18b --- /dev/null +++ b/src/main/java/io/openliberty/tools/common/plugins/util/AntTaskFactory.java @@ -0,0 +1,94 @@ +/** + * (C) Copyright IBM Corporation 2023. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.openliberty.tools.common.plugins.util; + +import java.io.File; +import java.io.PrintStream; +import java.util.Objects; + +import org.apache.tools.ant.DefaultLogger; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.Task; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class AntTaskFactory { + private static final Logger LOG = LoggerFactory.getLogger(AntTaskFactory.class); + + private final Project ant; + + public AntTaskFactory(File projectDir) { + this.ant = new Project(); + ant.setBaseDir(projectDir); + + initAntLogger(ant); + + ant.init(); + } + + protected void initAntLogger(final Project ant) { + Slf4jLoggingBuildListener antLogger = new Slf4jLoggingBuildListener(LOG); + antLogger.setEmacsMode(true); + antLogger.setOutputPrintStream(System.out); + antLogger.setErrorPrintStream(System.err); + + if (LOG.isDebugEnabled()) { + antLogger.setMessageOutputLevel(Project.MSG_VERBOSE); + } else { + antLogger.setMessageOutputLevel(Project.MSG_INFO); + } + + ant.addBuildListener(antLogger); + } + + @SuppressWarnings("unchecked") + public T createTask(String taskName) { + return (T) this.ant.createTask(taskName); + } + + static class Slf4jLoggingBuildListener extends DefaultLogger { + + private final Logger logger; + + public Slf4jLoggingBuildListener(Logger logger) { + super(); + this.logger = Objects.requireNonNull(logger); + } + + @Override + protected void printMessage(String message, PrintStream stream, int priority) { + switch (priority) { + case Project.MSG_ERR: + logger.error(message); + break; + case Project.MSG_WARN: + logger.warn(message); + break; + case Project.MSG_INFO: + logger.info(message); + break; + case Project.MSG_DEBUG: + logger.debug(message); + break; + case Project.MSG_VERBOSE: + logger.trace(message); + break; + default: + throw new UnsupportedOperationException("Unknown logging level: " + priority); + } + } + } +} diff --git a/src/main/java/io/openliberty/tools/common/plugins/util/DevUtil.java b/src/main/java/io/openliberty/tools/common/plugins/util/DevUtil.java index f2d3ca97..a6e95af4 100644 --- a/src/main/java/io/openliberty/tools/common/plugins/util/DevUtil.java +++ b/src/main/java/io/openliberty/tools/common/plugins/util/DevUtil.java @@ -80,6 +80,8 @@ import com.sun.nio.file.SensitivityWatchEventModifier; +import io.openliberty.tools.ant.ServerTask; + import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; @@ -91,13 +93,13 @@ import org.apache.commons.io.monitor.FileAlterationListener; import org.apache.commons.io.monitor.FileAlterationListenerAdaptor; import org.apache.commons.io.monitor.FileAlterationObserver; +import org.apache.tools.ant.taskdefs.ExecTask; +import org.apache.tools.ant.types.Commandline; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; -import io.openliberty.tools.ant.ServerTask; - /** * Utility class for dev mode. */ @@ -1280,7 +1282,12 @@ private void buildContainerImage(File tempContainerfile, File userContainerfile, info("The RUN features.sh command is detected in the Containerfile and extra time may be necessary when installing features."); } long startTime = System.currentTimeMillis(); - execContainerCmdAndLog(getRunProcess(buildCmd), containerBuildTimeout); + //Run build command with an Ant task if using Podman on Windows, see https://github.com/OpenLiberty/ci.maven/issues/1746 + if (!isDocker && OSUtil.isWindows()) { + containerAntRun(buildCmd); + } else { + execContainerCmdAndLog(getRunProcess(buildCmd), containerBuildTimeout); + } checkContainerBuildTime(startTime, buildContext); info("Completed building container image."); } catch (IllegalThreadStateException e) { @@ -5689,6 +5696,18 @@ private Set getGeneratedFeatures() { return genFeatSet; } + public void containerAntRun(String command) throws PluginExecutionException { + try { + AntTaskFactory ant = new AntTaskFactory(projectDirectory); + ExecTask antExecTask = (ExecTask) ant.createTask("exec"); + + antExecTask.setCommand(new Commandline(command)); + antExecTask.execute(); + } catch (Exception e) { + throw new PluginExecutionException("Error building container image: " + e.getMessage()); + } + } + /** * Create metadata when running devc mode and containers * Language server then uses metadata file to connect