From e0938e7797cede18132502bc428459608f12d7df Mon Sep 17 00:00:00 2001 From: Stefan Oehme Date: Thu, 5 Jan 2023 14:12:45 +0100 Subject: [PATCH 1/2] Fix core export provider Since https://github.com/apache/maven/pull/616, the default CoreExportProvider no longer uses the provided CoreExports, but instead tries (and fails) to discover them itself. This change fixes that by providing our own custom instance of CoreExportProvider. This allows core extension to contribute exported artifacts and exported packages again, like it used to do before the Maven 4.x upgrade. --- daemon/src/main/java/org/apache/maven/cli/DaemonMavenCli.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/daemon/src/main/java/org/apache/maven/cli/DaemonMavenCli.java b/daemon/src/main/java/org/apache/maven/cli/DaemonMavenCli.java index fd961e38f..765430a7f 100644 --- a/daemon/src/main/java/org/apache/maven/cli/DaemonMavenCli.java +++ b/daemon/src/main/java/org/apache/maven/cli/DaemonMavenCli.java @@ -64,6 +64,7 @@ import org.apache.maven.execution.*; import org.apache.maven.execution.scope.internal.MojoExecutionScopeModule; import org.apache.maven.extension.internal.CoreExports; +import org.apache.maven.extension.internal.CoreExportsProvider; import org.apache.maven.extension.internal.CoreExtensionEntry; import org.apache.maven.lifecycle.LifecycleExecutionException; import org.apache.maven.model.building.ModelProcessor; @@ -518,6 +519,7 @@ DefaultPlexusContainer container() throws Exception { protected void configure() { bind(ILoggerFactory.class).toInstance(slf4jLoggerFactory); bind(CoreExports.class).toInstance(exports); + bind(CoreExportsProvider.class).toInstance(new CoreExportsProvider(exports)); bind(ExtensionRealmCache.class).to(InvalidatingExtensionRealmCache.class); bind(PluginArtifactsCache.class).to(InvalidatingPluginArtifactsCache.class); bind(PluginRealmCache.class).to(InvalidatingPluginRealmCache.class); From 11cf2bb28f55b4be7deae662ec4610e654a54ecd Mon Sep 17 00:00:00 2001 From: Stefan Oehme Date: Wed, 11 Jan 2023 19:40:37 +0100 Subject: [PATCH 2/2] Add integration tests for API-providing extensions --- .../mvnd/it/ExtensionWithApiTest.java | 52 ++++++++++++++ .../extension-with-api/.mvn/extensions.xml | 25 +++++++ .../extension-with-api/.mvn/maven.config | 3 + .../extension/.mvn/maven.config | 3 + .../extension-with-api/extension/pom.xml | 71 +++++++++++++++++++ .../extension/with/api/extension/TheApi.java | 21 ++++++ .../extension/with/api/extension/TheImpl.java | 44 ++++++++++++ .../resources/META-INF/maven/extension.xml | 26 +++++++ .../plugin/.mvn/maven.config | 3 + .../extension-with-api/plugin/pom.xml | 63 ++++++++++++++++ .../extension/with/api/plugin/TheMojo.java | 35 +++++++++ .../test/projects/extension-with-api/pom.xml | 37 ++++++++++ 12 files changed, 383 insertions(+) create mode 100644 integration-tests/src/test/java/org/mvndaemon/mvnd/it/ExtensionWithApiTest.java create mode 100644 integration-tests/src/test/projects/extension-with-api/.mvn/extensions.xml create mode 100644 integration-tests/src/test/projects/extension-with-api/.mvn/maven.config create mode 100644 integration-tests/src/test/projects/extension-with-api/extension/.mvn/maven.config create mode 100644 integration-tests/src/test/projects/extension-with-api/extension/pom.xml create mode 100644 integration-tests/src/test/projects/extension-with-api/extension/src/main/java/org/mvndaemon/mvnd/test/extension/with/api/extension/TheApi.java create mode 100644 integration-tests/src/test/projects/extension-with-api/extension/src/main/java/org/mvndaemon/mvnd/test/extension/with/api/extension/TheImpl.java create mode 100644 integration-tests/src/test/projects/extension-with-api/extension/src/main/resources/META-INF/maven/extension.xml create mode 100644 integration-tests/src/test/projects/extension-with-api/plugin/.mvn/maven.config create mode 100644 integration-tests/src/test/projects/extension-with-api/plugin/pom.xml create mode 100644 integration-tests/src/test/projects/extension-with-api/plugin/src/main/java/org/mvndaemon/mvnd/test/extension/with/api/plugin/TheMojo.java create mode 100644 integration-tests/src/test/projects/extension-with-api/pom.xml diff --git a/integration-tests/src/test/java/org/mvndaemon/mvnd/it/ExtensionWithApiTest.java b/integration-tests/src/test/java/org/mvndaemon/mvnd/it/ExtensionWithApiTest.java new file mode 100644 index 000000000..8fe0fb497 --- /dev/null +++ b/integration-tests/src/test/java/org/mvndaemon/mvnd/it/ExtensionWithApiTest.java @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.mvndaemon.mvnd.it; + +import javax.inject.Inject; + +import org.junit.jupiter.api.Test; +import org.mvndaemon.mvnd.assertj.TestClientOutput; +import org.mvndaemon.mvnd.client.Client; +import org.mvndaemon.mvnd.client.DaemonParameters; +import org.mvndaemon.mvnd.junit.MvndNativeTest; + +@MvndNativeTest(projectDir = "src/test/projects/extension-with-api") +public class ExtensionWithApiTest { + + @Inject + Client client; + + @Inject + DaemonParameters parameters; + + @Test + void pluginCanAccessExtensionApi() throws InterruptedException { + install("extension"); + install("plugin"); + TestClientOutput o = new TestClientOutput(); + client.execute(o, "org.mvndaemon.mvnd.test.extension.with.api:plugin:mojo") + .assertSuccess(); + o.assertContainsMatchingSubsequence("Hello World!"); + } + + private void install(String project) throws InterruptedException { + TestClientOutput o = new TestClientOutput(); + client.execute(o, "-f", project, "install").assertSuccess(); + } +} diff --git a/integration-tests/src/test/projects/extension-with-api/.mvn/extensions.xml b/integration-tests/src/test/projects/extension-with-api/.mvn/extensions.xml new file mode 100644 index 000000000..b19da1861 --- /dev/null +++ b/integration-tests/src/test/projects/extension-with-api/.mvn/extensions.xml @@ -0,0 +1,25 @@ + + + + + org.mvndaemon.mvnd.test.extension.with.api + extension + 0.0.1-SNAPSHOT + + diff --git a/integration-tests/src/test/projects/extension-with-api/.mvn/maven.config b/integration-tests/src/test/projects/extension-with-api/.mvn/maven.config new file mode 100644 index 000000000..4230c2417 --- /dev/null +++ b/integration-tests/src/test/projects/extension-with-api/.mvn/maven.config @@ -0,0 +1,3 @@ +-Dmaven.wagon.httpconnectionManager.ttlSeconds=120 +-Dmaven.wagon.http.retryHandler.requestSentEnabled=true +-Dmaven.wagon.http.retryHandler.count=10 diff --git a/integration-tests/src/test/projects/extension-with-api/extension/.mvn/maven.config b/integration-tests/src/test/projects/extension-with-api/extension/.mvn/maven.config new file mode 100644 index 000000000..4230c2417 --- /dev/null +++ b/integration-tests/src/test/projects/extension-with-api/extension/.mvn/maven.config @@ -0,0 +1,3 @@ +-Dmaven.wagon.httpconnectionManager.ttlSeconds=120 +-Dmaven.wagon.http.retryHandler.requestSentEnabled=true +-Dmaven.wagon.http.retryHandler.count=10 diff --git a/integration-tests/src/test/projects/extension-with-api/extension/pom.xml b/integration-tests/src/test/projects/extension-with-api/extension/pom.xml new file mode 100644 index 000000000..98c4a079b --- /dev/null +++ b/integration-tests/src/test/projects/extension-with-api/extension/pom.xml @@ -0,0 +1,71 @@ + + + 4.0.0 + + org.mvndaemon.mvnd.test.extension.with.api + extension + 0.0.1-SNAPSHOT + + + UTF-8 + 1.8 + 1.8 + + + + + javax.inject + javax.inject + 1 + provided + + + org.eclipse.sisu + org.eclipse.sisu.inject + 0.3.5 + compile + + + org.apache.maven + maven-core + 3.6.3 + provided + + + + + + + org.eclipse.sisu + sisu-maven-plugin + 0.3.5 + + + generate-index + + main-index + + + + + + + + \ No newline at end of file diff --git a/integration-tests/src/test/projects/extension-with-api/extension/src/main/java/org/mvndaemon/mvnd/test/extension/with/api/extension/TheApi.java b/integration-tests/src/test/projects/extension-with-api/extension/src/main/java/org/mvndaemon/mvnd/test/extension/with/api/extension/TheApi.java new file mode 100644 index 000000000..a872f9deb --- /dev/null +++ b/integration-tests/src/test/projects/extension-with-api/extension/src/main/java/org/mvndaemon/mvnd/test/extension/with/api/extension/TheApi.java @@ -0,0 +1,21 @@ +/* + * Copyright 2021 the original author or authors. + * + * 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 org.mvndaemon.mvnd.test.extension.with.api.extension; + + +public interface TheApi { + void sayHello(); +} \ No newline at end of file diff --git a/integration-tests/src/test/projects/extension-with-api/extension/src/main/java/org/mvndaemon/mvnd/test/extension/with/api/extension/TheImpl.java b/integration-tests/src/test/projects/extension-with-api/extension/src/main/java/org/mvndaemon/mvnd/test/extension/with/api/extension/TheImpl.java new file mode 100644 index 000000000..463ed4496 --- /dev/null +++ b/integration-tests/src/test/projects/extension-with-api/extension/src/main/java/org/mvndaemon/mvnd/test/extension/with/api/extension/TheImpl.java @@ -0,0 +1,44 @@ +/* + * Copyright 2021 the original author or authors. + * + * 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 org.mvndaemon.mvnd.test.extension.with.api.extension; + +import org.apache.maven.eventspy.EventSpy; +import org.apache.maven.eventspy.AbstractEventSpy; +import javax.inject.Named; +import javax.inject.Singleton; +import org.eclipse.sisu.Typed; + +@Named +@Typed({EventSpy.class, TheApi.class}) +@Singleton +public class TheImpl extends AbstractEventSpy implements TheApi { + private boolean init; + + @Override + public void init(Context context) throws Exception { + System.out.println("Api extension is initialized"); + init = true; + } + + @Override + public void sayHello() { + if (init) { + System.out.println("Hello World!"); + } else { + throw new IllegalStateException("Not initialized"); + } + } +} \ No newline at end of file diff --git a/integration-tests/src/test/projects/extension-with-api/extension/src/main/resources/META-INF/maven/extension.xml b/integration-tests/src/test/projects/extension-with-api/extension/src/main/resources/META-INF/maven/extension.xml new file mode 100644 index 000000000..6ec1302aa --- /dev/null +++ b/integration-tests/src/test/projects/extension-with-api/extension/src/main/resources/META-INF/maven/extension.xml @@ -0,0 +1,26 @@ + + + + + org.mvndaemon.mvnd.test.extension.with.api.extension + + + org.mvndaemon.mvnd.test.extension.with.api:extension + + \ No newline at end of file diff --git a/integration-tests/src/test/projects/extension-with-api/plugin/.mvn/maven.config b/integration-tests/src/test/projects/extension-with-api/plugin/.mvn/maven.config new file mode 100644 index 000000000..4230c2417 --- /dev/null +++ b/integration-tests/src/test/projects/extension-with-api/plugin/.mvn/maven.config @@ -0,0 +1,3 @@ +-Dmaven.wagon.httpconnectionManager.ttlSeconds=120 +-Dmaven.wagon.http.retryHandler.requestSentEnabled=true +-Dmaven.wagon.http.retryHandler.count=10 diff --git a/integration-tests/src/test/projects/extension-with-api/plugin/pom.xml b/integration-tests/src/test/projects/extension-with-api/plugin/pom.xml new file mode 100644 index 000000000..0b702ef12 --- /dev/null +++ b/integration-tests/src/test/projects/extension-with-api/plugin/pom.xml @@ -0,0 +1,63 @@ + + + 4.0.0 + + org.mvndaemon.mvnd.test.extension.with.api + plugin + 0.0.1-SNAPSHOT + maven-plugin + + + UTF-8 + 1.8 + 1.8 + + + + + org.mvndaemon.mvnd.test.extension.with.api + extension + 0.0.1-SNAPSHOT + + + + + org.apache.maven + maven-plugin-api + 3.6.3 + provided + + + + org.apache.maven.plugin-tools + maven-plugin-annotations + 3.6.0 + provided + + + + javax.inject + javax.inject + 1 + provided + + + \ No newline at end of file diff --git a/integration-tests/src/test/projects/extension-with-api/plugin/src/main/java/org/mvndaemon/mvnd/test/extension/with/api/plugin/TheMojo.java b/integration-tests/src/test/projects/extension-with-api/plugin/src/main/java/org/mvndaemon/mvnd/test/extension/with/api/plugin/TheMojo.java new file mode 100644 index 000000000..d1f4430e1 --- /dev/null +++ b/integration-tests/src/test/projects/extension-with-api/plugin/src/main/java/org/mvndaemon/mvnd/test/extension/with/api/plugin/TheMojo.java @@ -0,0 +1,35 @@ +/* + * Copyright 2021 the original author or authors. + * + * 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 org.mvndaemon.mvnd.test.extension.with.api.plugin; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugins.annotations.Mojo; + +import javax.inject.Inject; + +import org.mvndaemon.mvnd.test.extension.with.api.extension.TheApi; + +@Mojo( name = "mojo") +public class TheMojo extends AbstractMojo { + @Inject + TheApi api; + + public void execute() throws MojoExecutionException { + api.sayHello(); + } +} \ No newline at end of file diff --git a/integration-tests/src/test/projects/extension-with-api/pom.xml b/integration-tests/src/test/projects/extension-with-api/pom.xml new file mode 100644 index 000000000..d0bd43a5d --- /dev/null +++ b/integration-tests/src/test/projects/extension-with-api/pom.xml @@ -0,0 +1,37 @@ + + + 4.0.0 + + org.mvndaemon.mvnd.test.extension.with.api + plugin-user + 0.0.1-SNAPSHOT + pom + + + + + org.mvndaemon.mvnd.test.extension.with.api + plugin + 0.0.1-SNAPSHOT + + + + + \ No newline at end of file