diff --git a/impl/maven-core/src/main/java/org/apache/maven/plugin/DefaultExtensionRealmCache.java b/impl/maven-core/src/main/java/org/apache/maven/plugin/DefaultExtensionRealmCache.java index b20211525854..ce3cb5135f3e 100644 --- a/impl/maven-core/src/main/java/org/apache/maven/plugin/DefaultExtensionRealmCache.java +++ b/impl/maven-core/src/main/java/org/apache/maven/plugin/DefaultExtensionRealmCache.java @@ -149,6 +149,6 @@ public void register(MavenProject project, Key key, CacheRecord record) { @Override public void dispose() { - flush(); + cache.clear(); } } diff --git a/impl/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginRealmCache.java b/impl/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginRealmCache.java index 681955f21db3..1e822a2ccb0c 100644 --- a/impl/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginRealmCache.java +++ b/impl/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginRealmCache.java @@ -215,6 +215,6 @@ public void register(MavenProject project, Key key, CacheRecord record) { @Override public void dispose() { - flush(); + cache.clear(); } } diff --git a/impl/maven-core/src/main/java/org/apache/maven/project/DefaultProjectRealmCache.java b/impl/maven-core/src/main/java/org/apache/maven/project/DefaultProjectRealmCache.java index 82a1a814c1b0..9111177c3489 100644 --- a/impl/maven-core/src/main/java/org/apache/maven/project/DefaultProjectRealmCache.java +++ b/impl/maven-core/src/main/java/org/apache/maven/project/DefaultProjectRealmCache.java @@ -125,6 +125,6 @@ public void register(MavenProject project, Key key, CacheRecord record) { @Override public void dispose() { - flush(); + cache.clear(); } } diff --git a/impl/maven-core/src/test/java/org/apache/maven/plugin/DefaultRealmCacheDisposeTest.java b/impl/maven-core/src/test/java/org/apache/maven/plugin/DefaultRealmCacheDisposeTest.java new file mode 100644 index 000000000000..4edf41a61a89 --- /dev/null +++ b/impl/maven-core/src/test/java/org/apache/maven/plugin/DefaultRealmCacheDisposeTest.java @@ -0,0 +1,85 @@ +/* + * 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.apache.maven.plugin; + +import java.util.List; + +import org.apache.maven.artifact.Artifact; +import org.codehaus.plexus.classworlds.ClassWorld; +import org.codehaus.plexus.classworlds.realm.ClassRealm; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Verifies that dispose() does not dispose ClassRealms prematurely. + *
+ * Plexus Disposable.dispose() runs before Sisu's @PreDestroy callbacks.
+ * If dispose() disposes ClassRealms, beans loaded from those realms will
+ * get ClassNotFoundException when their @PreDestroy methods execute.
+ * dispose() should only clear the cache map; flush() should dispose realms.
+ *
+ * @see MNG-8572
+ */
+class DefaultRealmCacheDisposeTest {
+
+ @Test
+ void disposeDoesNotDisposeClassRealms() throws Exception {
+ ClassWorld world = new ClassWorld();
+ ClassRealm realm = world.newRealm("test-plugin-realm");
+
+ DefaultPluginRealmCache cache = new DefaultPluginRealmCache();
+ PluginRealmCache.CacheRecord record = new PluginRealmCache.CacheRecord(realm, List.