From e3349e9a653c8e27c0afa5c80230d46a157b30e7 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Thu, 7 Nov 2024 18:47:11 +0100 Subject: [PATCH 1/2] Use Maven 4 model --- .../org/mvndaemon/mvnd/it/MvndTestUtil.java | 17 +++++++++-------- .../mvndaemon/mvnd/it/SingleModuleNativeIT.java | 10 +++++----- .../org/mvndaemon/mvnd/it/SingleModuleTest.java | 8 ++++---- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/integration-tests/src/test/java/org/mvndaemon/mvnd/it/MvndTestUtil.java b/integration-tests/src/test/java/org/mvndaemon/mvnd/it/MvndTestUtil.java index 05e9f5376..6e523d5fb 100644 --- a/integration-tests/src/test/java/org/mvndaemon/mvnd/it/MvndTestUtil.java +++ b/integration-tests/src/test/java/org/mvndaemon/mvnd/it/MvndTestUtil.java @@ -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 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); } } diff --git a/integration-tests/src/test/java/org/mvndaemon/mvnd/it/SingleModuleNativeIT.java b/integration-tests/src/test/java/org/mvndaemon/mvnd/it/SingleModuleNativeIT.java index fa98b88cf..c4ddfab41 100644 --- a/integration-tests/src/test/java/org/mvndaemon/mvnd/it/SingleModuleNativeIT.java +++ b/integration-tests/src/test/java/org/mvndaemon/mvnd/it/SingleModuleNativeIT.java @@ -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; @@ -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 props = MvndTestUtil.properties(parameters.multiModuleProjectDirectory().resolve("pom.xml")); final List messages = o.getMessages().stream() @@ -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 props) { /* implemented in the subclass */ } - String mojoStartedLogMessage(Properties props, String pluginArtifactId, String mojo, String executionId) { + String mojoStartedLogMessage(Map 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"; } } diff --git a/integration-tests/src/test/java/org/mvndaemon/mvnd/it/SingleModuleTest.java b/integration-tests/src/test/java/org/mvndaemon/mvnd/it/SingleModuleTest.java index d2b66cacf..a678353c1 100644 --- a/integration-tests/src/test/java/org/mvndaemon/mvnd/it/SingleModuleTest.java +++ b/integration-tests/src/test/java/org/mvndaemon/mvnd/it/SingleModuleTest.java @@ -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; @@ -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 props) { final List filteredMessages = o.getMessages().stream() .filter(m -> m.getType() == Message.MOJO_STARTED) .map(Object::toString) @@ -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 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() From 0dd6267b36a261514ce1f519fabc2952afa95cfd Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Thu, 7 Nov 2024 19:10:14 +0100 Subject: [PATCH 2/2] Restore ModelCacheFactory --- daemon/pom.xml | 5 ++++ .../maven/project/SnapshotModelCache.java | 9 ++++++-- .../project/SnapshotModelCacheFactory.java | 23 ++++++++----------- pom.xml | 5 ++++ 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/daemon/pom.xml b/daemon/pom.xml index 7c07b30dc..959e14705 100644 --- a/daemon/pom.xml +++ b/daemon/pom.xml @@ -33,6 +33,7 @@ + full @@ -60,6 +61,10 @@ org.apache.maven maven-cli + + org.apache.maven + maven-api-di + org.codehaus.plexus plexus-interactivity-api diff --git a/daemon/src/main/java/org/apache/maven/project/SnapshotModelCache.java b/daemon/src/main/java/org/apache/maven/project/SnapshotModelCache.java index e27bf76e8..0ab483af4 100644 --- a/daemon/src/main/java/org/apache/maven/project/SnapshotModelCache.java +++ b/daemon/src/main/java/org/apache/maven/project/SnapshotModelCache.java @@ -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 { @@ -44,6 +44,11 @@ public T computeIfAbsent(Source path, String tag, Supplier 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; } diff --git a/daemon/src/main/java/org/apache/maven/project/SnapshotModelCacheFactory.java b/daemon/src/main/java/org/apache/maven/project/SnapshotModelCacheFactory.java index fe285af69..d276439f7 100644 --- a/daemon/src/main/java/org/apache/maven/project/SnapshotModelCacheFactory.java +++ b/daemon/src/main/java/org/apache/maven/project/SnapshotModelCacheFactory.java @@ -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; @@ -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); } diff --git a/pom.xml b/pom.xml index 9d47d2a2a..e95718430 100644 --- a/pom.xml +++ b/pom.xml @@ -178,6 +178,11 @@ maven-cli ${maven.version} + + org.apache.maven + maven-api-di + ${maven.version} + org.apache.maven maven-jline