Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into beta-6
Browse files Browse the repository at this point in the history
  • Loading branch information
cstamas committed Nov 13, 2024
2 parents a19b154 + c059186 commit 739fd5f
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 62 deletions.
5 changes: 5 additions & 0 deletions daemon/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<properties>
<!-- If using release, sun.misc is not reachable (see SignalHelper) -->
<!-- <maven.compiler.release />-->
<maven.compiler.proc>full</maven.compiler.proc>
</properties>

<dependencies>
Expand Down Expand Up @@ -60,6 +61,10 @@
<groupId>org.apache.maven</groupId>
<artifactId>maven-cli</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-api-di</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-interactivity-api</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion daemon/src/main/java/org/apache/maven/cli/DaemonCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
/**
* Simple interface to bridge maven 3.9.x and 4.0.x CLI
*/
public interface DaemonCli {
public interface DaemonCli extends AutoCloseable {
int main(
List<String> args,
String workingDir,
Expand Down
52 changes: 34 additions & 18 deletions daemon/src/main/java/org/apache/maven/cli/DaemonMavenCling.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,30 @@
import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.mvndaemon.mvnd.cli.EnvHelper;

/**
* The main Daemon entry point: it shares lifecycle with daemon invoker (subclass of resident invoker) that keeps Maven
* resident, as long as this class is used. Once not needed, proper shut down using {@link #close()} method
* is required.
* <p>
* While daemon invoker is stateful (keeps Maven object graph), daemon parser is stateless and reusable, no need to
* create instance per incoming call.
*/
public class DaemonMavenCling implements DaemonCli {
private final DaemonMavenParser parser;
private final DaemonMavenInvoker invoker;

public DaemonMavenCling() {
this.parser = new DaemonMavenParser();
this.invoker = new DaemonMavenInvoker(ProtoLookup.builder()
.addMapping(
ClassWorld.class, ((ClassRealm) Thread.currentThread().getContextClassLoader()).getWorld())
.build());
}

@Override
public void close() {
invoker.close();
}

@Override
public int main(
Expand All @@ -49,24 +72,17 @@ public int main(
EnvHelper.environment(workingDir, env);
System.setProperty("maven.multiModuleProjectDirectory", projectDir);

try (DaemonMavenInvoker invoker = new DaemonMavenInvoker(ProtoLookup.builder()
.addMapping(
ClassWorld.class, ((ClassRealm) Thread.currentThread().getContextClassLoader()).getWorld())
.addMapping(BuildEventListener.class, buildEventListener)
.build())) {
DaemonMavenParser parser = new DaemonMavenParser();
return invoker.invoke(parser.parse(ParserRequest.builder(
"mvnd", "Maven Daemon", args, new ProtoLogger(), new DaemonMessageBuilderFactory())
.cwd(Paths.get(workingDir))
.in(in)
.out(out)
.err(err)
.lookup(ProtoLookup.builder()
.addMapping(Environment.class, () -> env)
.addMapping(BuildEventListener.class, buildEventListener)
.build())
.build()));
}
return invoker.invoke(parser.parse(ParserRequest.builder(
"mvnd", "Maven Daemon", args, new ProtoLogger(), new DaemonMessageBuilderFactory())
.cwd(Paths.get(workingDir))
.in(in)
.out(out)
.err(err)
.lookup(ProtoLookup.builder()
.addMapping(Environment.class, () -> env)
.addMapping(BuildEventListener.class, buildEventListener)
.build())
.build()));
}

/**
Expand Down
18 changes: 14 additions & 4 deletions daemon/src/main/java/org/apache/maven/cli/DaemonMavenInvoker.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,19 @@ public DaemonMavenInvoker(ProtoLookup protoLookup) {
super(protoLookup);
}

@Override
protected void configureLogging(LocalContext context) throws Exception {
super.configureLogging(context);
// TODO: this is a hack, and fixes issue in DefaultResidentMavenInvoker that does not copy TCCL
private ClassLoader tccl;

protected int doInvoke(LocalContext context) throws Exception {
try {
if (tccl != null) {
context.currentThreadContextClassLoader = tccl;
Thread.currentThread().setContextClassLoader(context.currentThreadContextClassLoader);
}
return super.doInvoke(context);
} finally {
this.tccl = context.currentThreadContextClassLoader;
}
}

@Override
Expand All @@ -60,7 +70,7 @@ protected Terminal createTerminal(LocalContext context) {

@Override
protected org.apache.maven.logging.BuildEventListener doDetermineBuildEventListener(LocalContext context) {
return protoLookup.lookup(BuildEventListener.class);
return context.invokerRequest.lookup().lookup(BuildEventListener.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import java.util.Objects;
import java.util.function.Supplier;

import org.apache.maven.building.Source;
import org.apache.maven.model.building.ModelCache;
import org.apache.maven.api.services.Source;
import org.apache.maven.api.services.model.ModelCache;

public class SnapshotModelCache implements ModelCache {

Expand All @@ -44,6 +44,11 @@ public <T> T computeIfAbsent(Source path, String tag, Supplier<T> data) {
return reactorCache.computeIfAbsent(path, tag, data);
}

@Override
public void clear() {
reactorCache.clear();
}

private ModelCache getDelegate(String version) {
return version.contains("SNAPSHOT") || version.contains("${") ? reactorCache : globalCache;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@
*/
package org.apache.maven.project;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import org.apache.maven.model.building.ModelCache;
import org.apache.maven.repository.internal.DefaultModelCacheFactory;
import org.apache.maven.repository.internal.ModelCacheFactory;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.sisu.Priority;
import org.apache.maven.api.di.Inject;
import org.apache.maven.api.di.Named;
import org.apache.maven.api.di.Priority;
import org.apache.maven.api.di.Singleton;
import org.apache.maven.api.services.model.ModelCache;
import org.apache.maven.api.services.model.ModelCacheFactory;
import org.apache.maven.internal.impl.model.DefaultModelCacheFactory;

import static org.mvndaemon.mvnd.common.Environment.MVND_NO_MODEL_CACHE;

Expand All @@ -42,14 +39,14 @@ public class SnapshotModelCacheFactory implements ModelCacheFactory {
@Inject
public SnapshotModelCacheFactory(DefaultModelCacheFactory factory) {
this.factory = factory;
this.globalCache = factory.createCache(new DefaultRepositorySystemSession());
this.globalCache = factory.newInstance();
}

@Override
public ModelCache createCache(RepositorySystemSession session) {
public ModelCache newInstance() {
boolean noModelCache =
Boolean.parseBoolean(MVND_NO_MODEL_CACHE.asOptional().orElse(MVND_NO_MODEL_CACHE.getDefault()));
ModelCache reactorCache = factory.createCache(session);
ModelCache reactorCache = factory.newInstance();
ModelCache globalCache = noModelCache ? reactorCache : this.globalCache;
return new SnapshotModelCache(globalCache, reactorCache);
}
Expand Down
6 changes: 5 additions & 1 deletion daemon/src/main/java/org/mvndaemon/mvnd/daemon/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,11 @@ public void close() {
try {
registry.close();
} finally {
socket.close();
try {
socket.close();
} finally {
cli.close();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,35 @@
*/
package org.mvndaemon.mvnd.it;

import javax.xml.stream.XMLStreamException;

import java.io.IOException;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;
import java.util.Map;

import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.apache.maven.model.v4.MavenStaxReader;

public class MvndTestUtil {

private MvndTestUtil() {}

public static Properties properties(Path pomXmlPath) {
public static Map<String, String> properties(Path pomXmlPath) {
try (Reader runtimeReader = Files.newBufferedReader(pomXmlPath, StandardCharsets.UTF_8)) {
final MavenXpp3Reader rxppReader = new MavenXpp3Reader();
final MavenStaxReader rxppReader = new MavenStaxReader();
return rxppReader.read(runtimeReader).getProperties();
} catch (IOException | XmlPullParserException e) {
} catch (IOException | XMLStreamException e) {
throw new RuntimeException("Could not read or parse " + pomXmlPath);
}
}

public static String version(Path pomXmlPath) {
try (Reader runtimeReader = Files.newBufferedReader(pomXmlPath, StandardCharsets.UTF_8)) {
final MavenXpp3Reader rxppReader = new MavenXpp3Reader();
final MavenStaxReader rxppReader = new MavenStaxReader();
return rxppReader.read(runtimeReader).getVersion();
} catch (IOException | XmlPullParserException e) {
} catch (IOException | XMLStreamException e) {
throw new RuntimeException("Could not read or parse " + pomXmlPath);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Properties;
import java.util.Map;
import java.util.stream.Collectors;

import org.assertj.core.api.Assertions;
Expand Down Expand Up @@ -59,7 +59,7 @@ void cleanInstall() throws IOException, InterruptedException {

final TestClientOutput o = new TestClientOutput();
client.execute(o, "clean", "install", "-e", "-B").assertSuccess();
final Properties props =
final Map<String, String> props =
MvndTestUtil.properties(parameters.multiModuleProjectDirectory().resolve("pom.xml"));

final List<String> messages = o.getMessages().stream()
Expand Down Expand Up @@ -87,14 +87,14 @@ void cleanInstall() throws IOException, InterruptedException {
Assertions.assertThat(installedJar).exists();
}

protected void assertJVM(TestClientOutput o, Properties props) {
protected void assertJVM(TestClientOutput o, Map<String, String> props) {
/* implemented in the subclass */
}

String mojoStartedLogMessage(Properties props, String pluginArtifactId, String mojo, String executionId) {
String mojoStartedLogMessage(Map<String, String> props, String pluginArtifactId, String mojo, String executionId) {
return "\\Q--- "
+ pluginArtifactId.replace("maven-", "").replace("-plugin", "")
+ ":" + props.getProperty(pluginArtifactId + ".version") + ":" + mojo + " ("
+ ":" + props.get(pluginArtifactId + ".version") + ":" + mojo + " ("
+ executionId + ") @ single-module ---\\E";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package org.mvndaemon.mvnd.it;

import java.util.List;
import java.util.Properties;
import java.util.Map;
import java.util.stream.Collectors;

import org.assertj.core.api.Assertions;
Expand All @@ -31,7 +31,7 @@
@MvndTest(projectDir = "src/test/projects/single-module")
class SingleModuleTest extends SingleModuleNativeIT {

protected void assertJVM(TestClientOutput o, Properties props) {
protected void assertJVM(TestClientOutput o, Map<String, String> props) {
final List<String> filteredMessages = o.getMessages().stream()
.filter(m -> m.getType() == Message.MOJO_STARTED)
.map(Object::toString)
Expand All @@ -48,14 +48,14 @@ protected void assertJVM(TestClientOutput o, Properties props) {
mojoStarted(props, "maven-install-plugin", "install", "default-install")));
}

String mojoStarted(Properties props, String pluginArtifactId, String mojo, String executionId) {
String mojoStarted(Map<String, String> props, String pluginArtifactId, String mojo, String executionId) {
return "\\Q"
+ Message.mojoStarted(
"single-module",
"org.apache.maven.plugins",
pluginArtifactId,
pluginArtifactId.replace("maven-", "").replace("-plugin", ""),
props.getProperty(pluginArtifactId + ".version"),
props.get(pluginArtifactId + ".version"),
mojo,
executionId)
.toString()
Expand Down
17 changes: 11 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
<maven.resolver.version>2.0.2</maven.resolver.version>
<slf4j.version>2.0.16</slf4j.version>
<sisu.version>0.9.0.M3</sisu.version>
<maven.plugin-tools.version>3.15.0</maven.plugin-tools.version>
<maven.plugin-tools.version>3.15.1</maven.plugin-tools.version>
<version.plexus-utils>4.0.2</version.plexus-utils>
<version.plexus-xml>4.0.4</version.plexus-xml>
<jakarta.inject.version>2.0.1</jakarta.inject.version>
Expand All @@ -106,8 +106,8 @@
<plexus-interactivity-api.version>1.3</plexus-interactivity-api.version>
<roaster.version>2.29.0.Final</roaster.version>
<takari-smart-builder.version>1.0.2</takari-smart-builder.version>
<testcontainers.version>1.20.2</testcontainers.version>
<xstream.version>1.4.20</xstream.version>
<testcontainers.version>1.20.3</testcontainers.version>
<xstream.version>1.4.21</xstream.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -178,6 +178,11 @@
<artifactId>maven-cli</artifactId>
<version>${maven.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-api-di</artifactId>
<version>${maven.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-jline</artifactId>
Expand Down Expand Up @@ -472,7 +477,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.8.0</version>
<version>3.8.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down Expand Up @@ -500,7 +505,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.20.0</version>
<version>3.21.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -516,7 +521,7 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.4.1</version>
<version>3.5.0</version>
</plugin>
</plugins>
</pluginManagement>
Expand Down

0 comments on commit 739fd5f

Please sign in to comment.