Skip to content

Commit

Permalink
Should support core extensions apache#114
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Nov 2, 2020
1 parent ed23329 commit dd4cf53
Show file tree
Hide file tree
Showing 13 changed files with 382 additions and 28 deletions.
4 changes: 4 additions & 0 deletions client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
<groupId>org.jboss.fuse.mvnd</groupId>
<artifactId>mvnd-common</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-embedder</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ private DaemonClientConnection findConnection(List<DaemonInfo> compatibleDaemons

public DaemonClientConnection startDaemon(DaemonCompatibilitySpec constraint) {
final String daemon = UUID.randomUUID().toString();
final Process process = startDaemon(daemon);
final Process process = startDaemon(daemon, constraint.getOptions());
LOGGER.debug("Started Maven daemon {}", daemon);
long start = System.currentTimeMillis();
do {
Expand All @@ -241,7 +241,7 @@ public DaemonClientConnection startDaemon(DaemonCompatibilitySpec constraint) {
throw new DaemonException.ConnectException("Timeout waiting to connect to the Maven daemon.\n" + diag.describe());
}

private Process startDaemon(String uid) {
private Process startDaemon(String uid, List<String> opts) {
final Path mavenHome = layout.mavenHome();
final Path workingDir = layout.userDir();
String command = "";
Expand All @@ -266,6 +266,7 @@ private Process startDaemon(String uid) {
args.add("-Xmx4g");
args.add(Environment.DAEMON_IDLE_TIMEOUT_MS.asCommandLineProperty(Integer.toString(layout.getIdleTimeoutMs())));
args.add(Environment.DAEMON_KEEP_ALIVE_MS.asCommandLineProperty(Integer.toString(layout.getKeepAliveMs())));
args.addAll(opts);
args.add(MavenDaemon.class.getName());
command = String.join(" ", args);

Expand Down
69 changes: 67 additions & 2 deletions client/src/main/java/org/jboss/fuse/mvnd/client/DefaultClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,25 @@
*/
package org.jboss.fuse.mvnd.client;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.apache.maven.cli.internal.extension.model.CoreExtension;
import org.apache.maven.cli.internal.extension.model.io.xpp3.CoreExtensionsXpp3Reader;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.fusesource.jansi.Ansi;
import org.jboss.fuse.mvnd.common.BuildProperties;
import org.jboss.fuse.mvnd.common.DaemonCompatibilitySpec;
Expand All @@ -44,9 +55,13 @@

public class DefaultClient implements Client {

private static final Logger LOGGER = LoggerFactory.getLogger(DefaultClient.class);
public static final int DEFAULT_PERIODIC_CHECK_INTERVAL_MILLIS = 10 * 1000;
public static final int CANCEL_TIMEOUT = 10 * 1000;

private static final Logger LOGGER = LoggerFactory.getLogger(DefaultClient.class);
private static final String EXT_CLASS_PATH = "maven.ext.class.path";
private static final String EXTENSIONS_FILENAME = ".mvn/extensions.xml";

private final Supplier<ClientLayout> lazyLayout;
private final BuildProperties buildProperties;

Expand Down Expand Up @@ -198,8 +213,8 @@ public ExecutionResult execute(ClientOutput output, List<String> argv) {
args.add("-Dmaven.repo.local=" + localMavenRepository.toString());
}

List<String> opts = getDaemonOpts(layout);
final DaemonConnector connector = new DaemonConnector(layout, registry, buildProperties);
List<String> opts = new ArrayList<>();
try (DaemonClientConnection daemon = connector.connect(new DaemonCompatibilitySpec(javaHome, opts), output)) {
output.buildStatus("Connected to daemon");

Expand Down Expand Up @@ -245,6 +260,56 @@ public ExecutionResult execute(ClientOutput output, List<String> argv) {
}
}

private List<String> getDaemonOpts(ClientLayout layout) {
List<String> options = new ArrayList<>();
// Classpath
List<Path> jars = parseExtClasspath(layout);
if (!jars.isEmpty()) {
options.add(Environment.DAEMON_EXT_CLASSPATH.asCommandLineProperty(
jars.stream().map(Path::toString).collect(Collectors.joining(","))));
}
// Extensions
try {
List<CoreExtension> extensions = readCoreExtensionsDescriptor(layout);
if (!extensions.isEmpty()) {
options.add(Environment.DAEMON_CORE_EXTENSIONS.asCommandLineProperty(
extensions.stream().map(e -> e.getGroupId() + ":" + e.getArtifactId() + ":" + e.getVersion())
.collect(Collectors.joining(","))));
}
} catch (IOException | XmlPullParserException e) {
throw new RuntimeException("Unable to parse core extensions", e);
}
return options;
}

private List<Path> parseExtClasspath(ClientLayout layout) {
String extClassPath = System.getProperty(EXT_CLASS_PATH);
List<Path> jars = new ArrayList<>();
if (StringUtils.isNotEmpty(extClassPath)) {
for (String jar : StringUtils.split(extClassPath, File.pathSeparator)) {
Path path = layout.userDir().resolve(jar).toAbsolutePath();
jars.add(path);
}
}
return jars;
}

private List<CoreExtension> readCoreExtensionsDescriptor(ClientLayout layout)
throws IOException, XmlPullParserException {
Path multiModuleProjectDirectory = layout.multiModuleProjectDirectory();
if (multiModuleProjectDirectory == null) {
return Collections.emptyList();
}
Path extensionsFile = multiModuleProjectDirectory.resolve(EXTENSIONS_FILENAME);
if (!Files.exists(extensionsFile)) {
return Collections.emptyList();
}
CoreExtensionsXpp3Reader parser = new CoreExtensionsXpp3Reader();
try (InputStream is = Files.newInputStream(extensionsFile)) {
return parser.read(is).getExtensions();
}
}

static void setDefaultArgs(List<String> args, ClientLayout layout) {
if (args.stream().noneMatch(arg -> arg.startsWith("-T") || arg.equals("--threads"))) {
args.add("-T" + layout.getThreads());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public DaemonCompatibilitySpec(Path javaHome, List<String> options) {
this.options = Objects.requireNonNull(options, "options");
}

public Path getJavaHome() {
return javaHome;
}

public List<String> getOptions() {
return options;
}

public Result isSatisfiedBy(DaemonInfo daemon) {
if (!javaHomeMatches(daemon)) {
return new Result(false, () -> "Java home is different.\n" + diff(daemon));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,15 @@ public enum Environment {
* line.
*/
MVND_THREADS("mvnd.threads", null),
DAEMON_UID("daemon.uid", null);
DAEMON_UID("daemon.uid", null),
/**
* Internal option to specify the maven extension classpath
*/
DAEMON_EXT_CLASSPATH("daemon.ext.classpath", null),
/**
* Internal option to specify the list of maven extension to register
*/
DAEMON_CORE_EXTENSIONS("daemon.core.extensions", null);

public static final int DEFAULT_IDLE_TIMEOUT = (int) TimeUnit.HOURS.toMillis(3);

Expand Down
Loading

0 comments on commit dd4cf53

Please sign in to comment.