Skip to content

Commit

Permalink
Use ant task for container builds
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbsox committed Nov 10, 2023
1 parent 1172cbb commit bbf06ea
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 3 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,10 @@
<artifactId>jackson-core</artifactId>
<version>2.15.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.9</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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 extends Task> 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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -5689,6 +5696,18 @@ private Set<String> 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
Expand Down

0 comments on commit bbf06ea

Please sign in to comment.