From c15750b17cf8c2c50f39aa84911d411cd1a4462e Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Tue, 28 Sep 2021 16:11:13 +0200 Subject: [PATCH 1/7] Fix G level metadata handling Just make Resolver handle it. This is TBD, as actual prefix still needs somehow to get there. --- ...venSessionPluginsMetadataInfoProvider.java | 102 ++++++++++++++ .../MavenPluginLifecycleMappingProvider.java | 6 +- .../repository/internal/PluginsMetadata.java | 112 +++++++++++++++ .../internal/PluginsMetadataGenerator.java | 130 ++++++++++++++++++ .../PluginsMetadataGeneratorFactory.java | 67 +++++++++ .../internal/PluginsMetadataInfoProvider.java | 44 ++++++ 6 files changed, 457 insertions(+), 4 deletions(-) create mode 100644 maven-core/src/main/java/org/apache/maven/execution/MavenSessionPluginsMetadataInfoProvider.java create mode 100644 maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadata.java create mode 100644 maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadataGenerator.java create mode 100644 maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadataGeneratorFactory.java create mode 100644 maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadataInfoProvider.java diff --git a/maven-core/src/main/java/org/apache/maven/execution/MavenSessionPluginsMetadataInfoProvider.java b/maven-core/src/main/java/org/apache/maven/execution/MavenSessionPluginsMetadataInfoProvider.java new file mode 100644 index 000000000000..2aa965e090ee --- /dev/null +++ b/maven-core/src/main/java/org/apache/maven/execution/MavenSessionPluginsMetadataInfoProvider.java @@ -0,0 +1,102 @@ +package org.apache.maven.execution; + +/* + * 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. + */ + +import java.util.concurrent.atomic.AtomicReference; + +import javax.inject.Named; +import javax.inject.Singleton; + +import org.apache.maven.AbstractMavenLifecycleParticipant; +import org.apache.maven.project.MavenProject; +import org.apache.maven.repository.internal.PluginsMetadataInfoProvider; +import org.eclipse.aether.artifact.Artifact; + +/** + * Default implementation of {@link PluginsMetadataInfoProvider}. + */ +@Named +@Singleton +public class MavenSessionPluginsMetadataInfoProvider + extends AbstractMavenLifecycleParticipant + implements PluginsMetadataInfoProvider +{ + private final AtomicReference mavenSessionRef; + + public MavenSessionPluginsMetadataInfoProvider() + { + this.mavenSessionRef = new AtomicReference<>( null ); + } + + @Override + public void afterSessionStart( final MavenSession session ) + { + this.mavenSessionRef.set( session ); + } + + @Override + public void afterSessionEnd( final MavenSession session ) + { + this.mavenSessionRef.set( null ); + } + + @Override + public PluginInfo getPluginInfo( final Artifact artifact ) + { + MavenSession mavenSession = mavenSessionRef.get(); + if ( mavenSession != null ) + { + MavenProject currentProject = mavenSession.getCurrentProject(); + if ( "maven-plugin".equals( currentProject.getPackaging() ) ) + { + String pluginPrefix = "blah"; + + return new PluginInfo() + { + @Override + public String getPluginGroupId() + { + return artifact.getGroupId(); + } + + @Override + public String getPluginArtifactId() + { + return artifact.getArtifactId(); + } + + @Override + public String getPluginPrefix() + { + return pluginPrefix; + } + + @Override + public String getPluginName() + { + return currentProject.getName(); + } + }; + } + } + + return null; + } +} diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/providers/MavenPluginLifecycleMappingProvider.java b/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/providers/MavenPluginLifecycleMappingProvider.java index c01afc75c5a0..8ab088396fb8 100644 --- a/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/providers/MavenPluginLifecycleMappingProvider.java +++ b/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/providers/MavenPluginLifecycleMappingProvider.java @@ -74,13 +74,11 @@ public MavenPluginLifecycleMappingProvider() ); lifecyclePhases.put( "install", - // TODO: MNG-6556: Do not upgrade to 3.0.0-M1 is does not install the plugin prefix metadata - new LifecyclePhase( "org.apache.maven.plugins:maven-install-plugin:2.5.2:install" ) + new LifecyclePhase( "org.apache.maven.plugins:maven-install-plugin:3.0.0-M1:install" ) ); lifecyclePhases.put( "deploy", - // TODO: MNG-6556: Do not upgrade to 3.0.0-M1 is does not install the plugin prefix metadata - new LifecyclePhase( "org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy" ) + new LifecyclePhase( "org.apache.maven.plugins:maven-deploy-plugin:3.0.0-M1:deploy" ) ); Lifecycle lifecycle = new Lifecycle(); diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadata.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadata.java new file mode 100644 index 000000000000..7c7ab0eafb1f --- /dev/null +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadata.java @@ -0,0 +1,112 @@ +package org.apache.maven.repository.internal; + +/* + * 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. + */ + +import java.io.File; +import java.util.ArrayList; +import java.util.Date; +import java.util.LinkedHashMap; +import java.util.List; + +import org.apache.maven.artifact.repository.metadata.Metadata; +import org.apache.maven.artifact.repository.metadata.Plugin; +import org.apache.maven.repository.internal.PluginsMetadataInfoProvider.PluginInfo; +import org.eclipse.aether.artifact.Artifact; + +/** + * Plugin G level metadata. + */ +final class PluginsMetadata + extends MavenMetadata +{ + private final PluginInfo pluginInfo; + + PluginsMetadata( PluginInfo pluginInfo, Date timestamp ) + { + super( createRepositoryMetadata( pluginInfo ), null, timestamp ); + this.pluginInfo = pluginInfo; + } + + PluginsMetadata( PluginInfo pluginInfo, File file, Date timestamp ) + { + super( createRepositoryMetadata( pluginInfo ), file, timestamp ); + this.pluginInfo = pluginInfo; + } + + private static Metadata createRepositoryMetadata( PluginInfo pluginInfo ) + { + Metadata result = new Metadata(); + Plugin plugin = new Plugin(); + plugin.setPrefix( pluginInfo.getPluginPrefix() ); + plugin.setArtifactId( pluginInfo.getPluginArtifactId() ); + plugin.setName( pluginInfo.getPluginName() ); + result.getPlugins().add( plugin ); + return result; + } + + @Override + protected void merge( Metadata recessive ) + { + List recessivePlugins = recessive.getPlugins(); + List plugins = metadata.getPlugins(); + if ( !plugins.isEmpty() ) + { + LinkedHashMap mergedPlugins = new LinkedHashMap<>(); + recessivePlugins.forEach( p -> mergedPlugins.put( p.getPrefix(), p ) ); + plugins.forEach( p -> mergedPlugins.put( p.getPrefix(), p ) ); + metadata.setPlugins( new ArrayList<>( mergedPlugins.values() ) ); + } + } + + public Object getKey() + { + return getGroupId(); + } + + public static Object getKey( Artifact artifact ) + { + return artifact.getGroupId(); + } + + public MavenMetadata setFile( File file ) + { + return new PluginsMetadata( pluginInfo, file, timestamp ); + } + + public String getGroupId() + { + return pluginInfo.getPluginGroupId(); + } + + public String getArtifactId() + { + return ""; + } + + public String getVersion() + { + return ""; + } + + public Nature getNature() + { + return Nature.RELEASE_OR_SNAPSHOT; + } +} diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadataGenerator.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadataGenerator.java new file mode 100644 index 000000000000..4f355686517c --- /dev/null +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadataGenerator.java @@ -0,0 +1,130 @@ +package org.apache.maven.repository.internal; + +/* + * 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. + */ + +import java.util.Collection; +import java.util.Collections; +import java.util.Date; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.apache.maven.repository.internal.PluginsMetadataInfoProvider.PluginInfo; +import org.eclipse.aether.RepositorySystemSession; +import org.eclipse.aether.artifact.Artifact; +import org.eclipse.aether.deployment.DeployRequest; +import org.eclipse.aether.impl.MetadataGenerator; +import org.eclipse.aether.installation.InstallRequest; +import org.eclipse.aether.metadata.Metadata; +import org.eclipse.aether.util.ConfigUtils; + +import static java.util.Objects.requireNonNull; + +/** + * Plugin G level metadata. + */ +class PluginsMetadataGenerator + implements MetadataGenerator +{ + private final PluginsMetadataInfoProvider pluginsMetadataInfoProvider; + + private final Map plugins; + + private final Map processedPlugins; + + private final Date timestamp; + + PluginsMetadataGenerator( PluginsMetadataInfoProvider pluginsMetadataInfoProvider, + RepositorySystemSession session, + InstallRequest request ) + { + this( pluginsMetadataInfoProvider, session, request.getMetadata() ); + } + + PluginsMetadataGenerator( PluginsMetadataInfoProvider pluginsMetadataInfoProvider, + RepositorySystemSession session, + DeployRequest request ) + { + this( pluginsMetadataInfoProvider, session, request.getMetadata() ); + } + + private PluginsMetadataGenerator( PluginsMetadataInfoProvider pluginsMetadataInfoProvider, + RepositorySystemSession session, + Collection metadatas ) + { + this.pluginsMetadataInfoProvider = requireNonNull( pluginsMetadataInfoProvider ); + this.plugins = new LinkedHashMap<>(); + this.processedPlugins = new LinkedHashMap<>(); + this.timestamp = (Date) ConfigUtils.getObject( session, new Date(), "maven.startTime" ); + + /* + * NOTE: This should be considered a quirk to support interop with Maven's legacy ArtifactDeployer which + * processes one artifact at a time and hence cannot associate the artifacts from the same project to use the + * same version index. Allowing the caller to pass in metadata from a previous deployment allows to re-establish + * the association between the artifacts of the same project. + */ + for ( Iterator it = metadatas.iterator(); it.hasNext(); ) + { + Metadata metadata = it.next(); + if ( metadata instanceof PluginsMetadata ) + { + it.remove(); + PluginsMetadata pluginMetadata = ( PluginsMetadata ) metadata; + processedPlugins.put( pluginMetadata.getKey(), pluginMetadata ); + } + } + } + + @Override + public Collection prepare( Collection artifacts ) + { + return Collections.emptyList(); + } + + @Override + public Artifact transformArtifact( Artifact artifact ) + { + return artifact; + } + + @Override + public Collection finish( Collection artifacts ) + { + for ( Artifact artifact : artifacts ) + { + PluginInfo pluginInfo = pluginsMetadataInfoProvider.getPluginInfo( artifact ); + if ( pluginInfo != null ) + { + Object key = PluginsMetadata.getKey( artifact ); + if ( processedPlugins.get( key ) == null ) + { + PluginsMetadata pluginMetadata = plugins.get( key ); + if ( pluginMetadata == null ) + { + pluginMetadata = new PluginsMetadata( pluginInfo, timestamp ); + plugins.put( key, pluginMetadata ); + } + } + } + } + + return plugins.values(); + } +} diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadataGeneratorFactory.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadataGeneratorFactory.java new file mode 100644 index 000000000000..19d499b25772 --- /dev/null +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadataGeneratorFactory.java @@ -0,0 +1,67 @@ +package org.apache.maven.repository.internal; + +/* + * 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. + */ + +import javax.inject.Inject; +import javax.inject.Named; +import javax.inject.Singleton; + +import org.eclipse.aether.RepositorySystemSession; +import org.eclipse.aether.deployment.DeployRequest; +import org.eclipse.aether.impl.MetadataGenerator; +import org.eclipse.aether.impl.MetadataGeneratorFactory; +import org.eclipse.aether.installation.InstallRequest; + +import static java.util.Objects.requireNonNull; + +/** + * Plugin G level metadata. + */ +@Named( "plugins" ) +@Singleton +public class PluginsMetadataGeneratorFactory + implements MetadataGeneratorFactory +{ + private final PluginsMetadataInfoProvider pluginsMetadataInfoProvider; + + @Inject + public PluginsMetadataGeneratorFactory( PluginsMetadataInfoProvider pluginsMetadataInfoProvider ) + { + this.pluginsMetadataInfoProvider = requireNonNull( pluginsMetadataInfoProvider ); + } + + @Override + public MetadataGenerator newInstance( RepositorySystemSession session, InstallRequest request ) + { + return new PluginsMetadataGenerator( pluginsMetadataInfoProvider, session, request ); + } + + @Override + public MetadataGenerator newInstance( RepositorySystemSession session, DeployRequest request ) + { + return new PluginsMetadataGenerator( pluginsMetadataInfoProvider, session, request ); + } + + @Override + public float getPriority() + { + return 5; + } +} diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadataInfoProvider.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadataInfoProvider.java new file mode 100644 index 000000000000..629a23f3bea1 --- /dev/null +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadataInfoProvider.java @@ -0,0 +1,44 @@ +package org.apache.maven.repository.internal; + +/* + * 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. + */ + +import org.eclipse.aether.artifact.Artifact; + +/** + * Plugin G level metadata provider. + */ +public interface PluginsMetadataInfoProvider +{ + interface PluginInfo + { + String getPluginGroupId(); + + String getPluginArtifactId(); + + String getPluginPrefix(); + + String getPluginName(); + } + + /** + * Returns {@link PluginInfo} corresponding for passed in {@link Artifact}, or {@code null}. + */ + PluginInfo getPluginInfo( Artifact artifact ); +} From 9b36c890ddd9e249170eb81220c0959d6433eaec Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Tue, 28 Sep 2021 16:15:59 +0200 Subject: [PATCH 2/7] Remove unneeded mojo --- .../mapping/providers/MavenPluginLifecycleMappingProvider.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/providers/MavenPluginLifecycleMappingProvider.java b/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/providers/MavenPluginLifecycleMappingProvider.java index 8ab088396fb8..bc91ab6ed430 100644 --- a/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/providers/MavenPluginLifecycleMappingProvider.java +++ b/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/providers/MavenPluginLifecycleMappingProvider.java @@ -69,8 +69,7 @@ public MavenPluginLifecycleMappingProvider() ); lifecyclePhases.put( "package", - new LifecyclePhase( "org.apache.maven.plugins:maven-jar-plugin:3.2.0:jar," - + "org.apache.maven.plugins:maven-plugin-plugin:3.6.0:addPluginArtifactMetadata" ) + new LifecyclePhase( "org.apache.maven.plugins:maven-jar-plugin:3.2.0:jar" ) ); lifecyclePhases.put( "install", From d2b1100f43e13b7020a14f1dcf552109cad943a8 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Tue, 28 Sep 2021 16:19:31 +0200 Subject: [PATCH 3/7] Tidy --- .../execution/MavenSessionPluginsMetadataInfoProvider.java | 4 +++- .../org/apache/maven/repository/internal/PluginsMetadata.java | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/maven-core/src/main/java/org/apache/maven/execution/MavenSessionPluginsMetadataInfoProvider.java b/maven-core/src/main/java/org/apache/maven/execution/MavenSessionPluginsMetadataInfoProvider.java index 2aa965e090ee..35593a4d1fc3 100644 --- a/maven-core/src/main/java/org/apache/maven/execution/MavenSessionPluginsMetadataInfoProvider.java +++ b/maven-core/src/main/java/org/apache/maven/execution/MavenSessionPluginsMetadataInfoProvider.java @@ -63,10 +63,12 @@ public PluginInfo getPluginInfo( final Artifact artifact ) MavenSession mavenSession = mavenSessionRef.get(); if ( mavenSession != null ) { + // TODO: match or maybe search (not just current) for passed in artifact + // ie. "deploy at end"? MavenProject currentProject = mavenSession.getCurrentProject(); if ( "maven-plugin".equals( currentProject.getPackaging() ) ) { - String pluginPrefix = "blah"; + String pluginPrefix = "blah"; // TODO: get it somehow return new PluginInfo() { diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadata.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadata.java index 7c7ab0eafb1f..04b8beb5995f 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadata.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadata.java @@ -90,21 +90,25 @@ public MavenMetadata setFile( File file ) return new PluginsMetadata( pluginInfo, file, timestamp ); } + @Override public String getGroupId() { return pluginInfo.getPluginGroupId(); } + @Override public String getArtifactId() { return ""; } + @Override public String getVersion() { return ""; } + @Override public Nature getNature() { return Nature.RELEASE_OR_SNAPSHOT; From d1438ce79506e2be2342ce89c8b22dbeee3a93c8 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Tue, 28 Sep 2021 19:19:30 +0200 Subject: [PATCH 4/7] Add default prefix --- .../execution/MavenSessionPluginsMetadataInfoProvider.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/execution/MavenSessionPluginsMetadataInfoProvider.java b/maven-core/src/main/java/org/apache/maven/execution/MavenSessionPluginsMetadataInfoProvider.java index 35593a4d1fc3..8e92efc0f635 100644 --- a/maven-core/src/main/java/org/apache/maven/execution/MavenSessionPluginsMetadataInfoProvider.java +++ b/maven-core/src/main/java/org/apache/maven/execution/MavenSessionPluginsMetadataInfoProvider.java @@ -25,9 +25,11 @@ import javax.inject.Singleton; import org.apache.maven.AbstractMavenLifecycleParticipant; +import org.apache.maven.plugin.descriptor.PluginDescriptor; import org.apache.maven.project.MavenProject; import org.apache.maven.repository.internal.PluginsMetadataInfoProvider; import org.eclipse.aether.artifact.Artifact; +import org.eclipse.aether.artifact.ArtifactProperties; /** * Default implementation of {@link PluginsMetadataInfoProvider}. @@ -66,9 +68,10 @@ public PluginInfo getPluginInfo( final Artifact artifact ) // TODO: match or maybe search (not just current) for passed in artifact // ie. "deploy at end"? MavenProject currentProject = mavenSession.getCurrentProject(); - if ( "maven-plugin".equals( currentProject.getPackaging() ) ) + if ( "maven-plugin".equals( artifact.getProperty( ArtifactProperties.TYPE, "" ) ) ) { - String pluginPrefix = "blah"; // TODO: get it somehow + String pluginPrefix = PluginDescriptor.getGoalPrefixFromArtifactId( artifact.getArtifactId() ); + // TODO: check for goalPrefix if set return new PluginInfo() { From e6c24dfa097d5e797b646f44ce94abdfe6c48fc8 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Thu, 30 Sep 2021 14:07:41 +0200 Subject: [PATCH 5/7] Make it work --- ...venSessionPluginsMetadataInfoProvider.java | 107 ------------- .../DefaultPluginsMetadataInfoProvider.java | 147 ++++++++++++++++++ .../MavenPluginLifecycleMappingProvider.java | 3 +- .../repository/internal/PluginsMetadata.java | 1 + 4 files changed, 150 insertions(+), 108 deletions(-) delete mode 100644 maven-core/src/main/java/org/apache/maven/execution/MavenSessionPluginsMetadataInfoProvider.java create mode 100644 maven-core/src/main/java/org/apache/maven/execution/infoproviders/DefaultPluginsMetadataInfoProvider.java diff --git a/maven-core/src/main/java/org/apache/maven/execution/MavenSessionPluginsMetadataInfoProvider.java b/maven-core/src/main/java/org/apache/maven/execution/MavenSessionPluginsMetadataInfoProvider.java deleted file mode 100644 index 8e92efc0f635..000000000000 --- a/maven-core/src/main/java/org/apache/maven/execution/MavenSessionPluginsMetadataInfoProvider.java +++ /dev/null @@ -1,107 +0,0 @@ -package org.apache.maven.execution; - -/* - * 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. - */ - -import java.util.concurrent.atomic.AtomicReference; - -import javax.inject.Named; -import javax.inject.Singleton; - -import org.apache.maven.AbstractMavenLifecycleParticipant; -import org.apache.maven.plugin.descriptor.PluginDescriptor; -import org.apache.maven.project.MavenProject; -import org.apache.maven.repository.internal.PluginsMetadataInfoProvider; -import org.eclipse.aether.artifact.Artifact; -import org.eclipse.aether.artifact.ArtifactProperties; - -/** - * Default implementation of {@link PluginsMetadataInfoProvider}. - */ -@Named -@Singleton -public class MavenSessionPluginsMetadataInfoProvider - extends AbstractMavenLifecycleParticipant - implements PluginsMetadataInfoProvider -{ - private final AtomicReference mavenSessionRef; - - public MavenSessionPluginsMetadataInfoProvider() - { - this.mavenSessionRef = new AtomicReference<>( null ); - } - - @Override - public void afterSessionStart( final MavenSession session ) - { - this.mavenSessionRef.set( session ); - } - - @Override - public void afterSessionEnd( final MavenSession session ) - { - this.mavenSessionRef.set( null ); - } - - @Override - public PluginInfo getPluginInfo( final Artifact artifact ) - { - MavenSession mavenSession = mavenSessionRef.get(); - if ( mavenSession != null ) - { - // TODO: match or maybe search (not just current) for passed in artifact - // ie. "deploy at end"? - MavenProject currentProject = mavenSession.getCurrentProject(); - if ( "maven-plugin".equals( artifact.getProperty( ArtifactProperties.TYPE, "" ) ) ) - { - String pluginPrefix = PluginDescriptor.getGoalPrefixFromArtifactId( artifact.getArtifactId() ); - // TODO: check for goalPrefix if set - - return new PluginInfo() - { - @Override - public String getPluginGroupId() - { - return artifact.getGroupId(); - } - - @Override - public String getPluginArtifactId() - { - return artifact.getArtifactId(); - } - - @Override - public String getPluginPrefix() - { - return pluginPrefix; - } - - @Override - public String getPluginName() - { - return currentProject.getName(); - } - }; - } - } - - return null; - } -} diff --git a/maven-core/src/main/java/org/apache/maven/execution/infoproviders/DefaultPluginsMetadataInfoProvider.java b/maven-core/src/main/java/org/apache/maven/execution/infoproviders/DefaultPluginsMetadataInfoProvider.java new file mode 100644 index 000000000000..22d535665871 --- /dev/null +++ b/maven-core/src/main/java/org/apache/maven/execution/infoproviders/DefaultPluginsMetadataInfoProvider.java @@ -0,0 +1,147 @@ +package org.apache.maven.execution.infoproviders; + +/* + * 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. + */ + +import java.util.Objects; +import java.util.concurrent.atomic.AtomicReference; + +import javax.inject.Named; +import javax.inject.Singleton; + +import org.apache.maven.AbstractMavenLifecycleParticipant; +import org.apache.maven.artifact.repository.metadata.Metadata; +import org.apache.maven.artifact.repository.metadata.Plugin; +import org.apache.maven.artifact.repository.metadata.RepositoryMetadata; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.project.MavenProject; +import org.apache.maven.repository.internal.PluginsMetadataInfoProvider; +import org.apache.maven.repository.legacy.metadata.ArtifactMetadata; +import org.eclipse.aether.artifact.Artifact; + +/** + * Default implementation of {@link PluginsMetadataInfoProvider}. + */ +@Named +@Singleton +public class DefaultPluginsMetadataInfoProvider + extends AbstractMavenLifecycleParticipant + implements PluginsMetadataInfoProvider +{ + private final AtomicReference mavenSessionRef; + + public DefaultPluginsMetadataInfoProvider() + { + this.mavenSessionRef = new AtomicReference<>( null ); + } + + @Override + public void afterSessionStart( final MavenSession session ) + { + this.mavenSessionRef.set( session ); + } + + @Override + public void afterSessionEnd( final MavenSession session ) + { + this.mavenSessionRef.set( null ); + } + + @Override + public PluginInfo getPluginInfo( final Artifact artifact ) + { + MavenSession mavenSession = mavenSessionRef.get(); + if ( mavenSession != null ) + { + MavenProject mavenProject = searchForProject( mavenSession, artifact ); + if ( mavenProject != null && "maven-plugin".equals( mavenProject.getPackaging() ) ) + { + Plugin plugin = searchForPluginGroupLevelRepositoryMetadata( mavenProject ); + + if ( plugin != null ) + { + return new PluginInfo() + { + @Override + public String getPluginGroupId() + { + return artifact.getGroupId(); + } + + @Override + public String getPluginArtifactId() + { + return artifact.getArtifactId(); + } + + @Override + public String getPluginPrefix() + { + return plugin.getPrefix(); + } + + @Override + public String getPluginName() + { + return plugin.getName(); + } + }; + } + } + } + + return null; + } + + private MavenProject searchForProject( MavenSession mavenSession, Artifact artifact ) + { + for ( MavenProject mavenProject : mavenSession.getProjects() ) + { + if ( mavenProject.getArtifact() != null + && Objects.equals( mavenProject.getGroupId(), artifact.getGroupId() ) + && Objects.equals( mavenProject.getArtifactId(), artifact.getArtifactId() ) ) + { + return mavenProject; + } + } + return null; + } + + private Plugin searchForPluginGroupLevelRepositoryMetadata( MavenProject mavenProject ) + { + org.apache.maven.artifact.Artifact projectArtifact = mavenProject.getArtifact(); + for ( ArtifactMetadata artifactMetadata : projectArtifact.getMetadataList() ) + { + if ( artifactMetadata instanceof RepositoryMetadata ) + { + RepositoryMetadata repositoryMetadata = (RepositoryMetadata) artifactMetadata; + Metadata metadata = repositoryMetadata.getMetadata(); + + for ( Plugin plugin : metadata.getPlugins() ) + { + if ( Objects.equals( plugin.getArtifactId(), mavenProject.getArtifactId() ) ) + { + return plugin; + } + } + } + } + return null; + } +} diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/providers/MavenPluginLifecycleMappingProvider.java b/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/providers/MavenPluginLifecycleMappingProvider.java index bc91ab6ed430..8ab088396fb8 100644 --- a/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/providers/MavenPluginLifecycleMappingProvider.java +++ b/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/providers/MavenPluginLifecycleMappingProvider.java @@ -69,7 +69,8 @@ public MavenPluginLifecycleMappingProvider() ); lifecyclePhases.put( "package", - new LifecyclePhase( "org.apache.maven.plugins:maven-jar-plugin:3.2.0:jar" ) + new LifecyclePhase( "org.apache.maven.plugins:maven-jar-plugin:3.2.0:jar," + + "org.apache.maven.plugins:maven-plugin-plugin:3.6.0:addPluginArtifactMetadata" ) ); lifecyclePhases.put( "install", diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadata.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadata.java index 04b8beb5995f..7a6c45abfe79 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadata.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadata.java @@ -85,6 +85,7 @@ public static Object getKey( Artifact artifact ) return artifact.getGroupId(); } + @Override public MavenMetadata setFile( File file ) { return new PluginsMetadata( pluginInfo, file, timestamp ); From 41bb3e1c917eadeca5912bc1854ee273a4efa2bf Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Fri, 1 Oct 2021 14:04:56 +0200 Subject: [PATCH 6/7] Simplify even more Use provider as SISU Providers obey scope. In UT two affected UTs needs a bit of extra work, enter scope and fake session using Mockito --- maven-compat/pom.xml | 5 ++++ .../deployer/ArtifactDeployerTest.java | 9 ++++++ .../installer/ArtifactInstallerTest.java | 9 ++++++ .../DefaultPluginsMetadataInfoProvider.java | 28 ++++++------------- 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/maven-compat/pom.xml b/maven-compat/pom.xml index ed909475b0a4..7f8d4a3ff048 100644 --- a/maven-compat/pom.xml +++ b/maven-compat/pom.xml @@ -108,6 +108,11 @@ under the License. plexus-testing test + + org.mockito + mockito-core + test + org.apache.maven.wagon wagon-file diff --git a/maven-compat/src/test/java/org/apache/maven/artifact/deployer/ArtifactDeployerTest.java b/maven-compat/src/test/java/org/apache/maven/artifact/deployer/ArtifactDeployerTest.java index 53cc388acd14..e2e24c3e7635 100644 --- a/maven-compat/src/test/java/org/apache/maven/artifact/deployer/ArtifactDeployerTest.java +++ b/maven-compat/src/test/java/org/apache/maven/artifact/deployer/ArtifactDeployerTest.java @@ -24,12 +24,15 @@ import org.apache.maven.artifact.AbstractArtifactComponentTestCase; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.session.scope.internal.SessionScope; import org.codehaus.plexus.util.FileUtils; import org.junit.jupiter.api.Test; import static org.codehaus.plexus.testing.PlexusExtension.getBasedir; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; import javax.inject.Inject; @@ -42,6 +45,9 @@ public class ArtifactDeployerTest @Inject private ArtifactDeployer artifactDeployer; + @Inject + private SessionScope sessionScope; + protected String component() { return "deployer"; @@ -51,6 +57,9 @@ protected String component() public void testArtifactInstallation() throws Exception { + sessionScope.enter(); + sessionScope.seed(MavenSession.class, mock(MavenSession.class)); + String artifactBasedir = new File( getBasedir(), "src/test/resources/artifact-install" ).getAbsolutePath(); Artifact artifact = createArtifact( "artifact", "1.0" ); diff --git a/maven-compat/src/test/java/org/apache/maven/artifact/installer/ArtifactInstallerTest.java b/maven-compat/src/test/java/org/apache/maven/artifact/installer/ArtifactInstallerTest.java index ca99ab2a53cc..7dc1ef8ae88b 100644 --- a/maven-compat/src/test/java/org/apache/maven/artifact/installer/ArtifactInstallerTest.java +++ b/maven-compat/src/test/java/org/apache/maven/artifact/installer/ArtifactInstallerTest.java @@ -23,11 +23,14 @@ import org.apache.maven.artifact.AbstractArtifactComponentTestCase; import org.apache.maven.artifact.Artifact; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.session.scope.internal.SessionScope; import org.junit.jupiter.api.Test; import javax.inject.Inject; import static org.codehaus.plexus.testing.PlexusExtension.getBasedir; +import static org.mockito.Mockito.mock; /** * @author Jason van Zyl @@ -38,6 +41,9 @@ public class ArtifactInstallerTest @Inject private ArtifactInstaller artifactInstaller; + @Inject + private SessionScope sessionScope; + protected String component() { return "installer"; @@ -47,6 +53,9 @@ protected String component() public void testArtifactInstallation() throws Exception { + sessionScope.enter(); + sessionScope.seed(MavenSession.class, mock(MavenSession.class)); + String artifactBasedir = new File( getBasedir(), "src/test/resources/artifact-install" ).getAbsolutePath(); Artifact artifact = createArtifact( "artifact", "1.0" ); diff --git a/maven-core/src/main/java/org/apache/maven/execution/infoproviders/DefaultPluginsMetadataInfoProvider.java b/maven-core/src/main/java/org/apache/maven/execution/infoproviders/DefaultPluginsMetadataInfoProvider.java index 22d535665871..616a3a88a4bf 100644 --- a/maven-core/src/main/java/org/apache/maven/execution/infoproviders/DefaultPluginsMetadataInfoProvider.java +++ b/maven-core/src/main/java/org/apache/maven/execution/infoproviders/DefaultPluginsMetadataInfoProvider.java @@ -20,12 +20,12 @@ */ import java.util.Objects; -import java.util.concurrent.atomic.AtomicReference; +import javax.inject.Inject; import javax.inject.Named; +import javax.inject.Provider; import javax.inject.Singleton; -import org.apache.maven.AbstractMavenLifecycleParticipant; import org.apache.maven.artifact.repository.metadata.Metadata; import org.apache.maven.artifact.repository.metadata.Plugin; import org.apache.maven.artifact.repository.metadata.RepositoryMetadata; @@ -35,38 +35,28 @@ import org.apache.maven.repository.legacy.metadata.ArtifactMetadata; import org.eclipse.aether.artifact.Artifact; +import static java.util.Objects.requireNonNull; + /** * Default implementation of {@link PluginsMetadataInfoProvider}. */ @Named @Singleton public class DefaultPluginsMetadataInfoProvider - extends AbstractMavenLifecycleParticipant implements PluginsMetadataInfoProvider { - private final AtomicReference mavenSessionRef; + private final Provider mavenSessionProvider; - public DefaultPluginsMetadataInfoProvider() - { - this.mavenSessionRef = new AtomicReference<>( null ); - } - - @Override - public void afterSessionStart( final MavenSession session ) - { - this.mavenSessionRef.set( session ); - } - - @Override - public void afterSessionEnd( final MavenSession session ) + @Inject + public DefaultPluginsMetadataInfoProvider( final Provider mavenSessionProvider ) { - this.mavenSessionRef.set( null ); + this.mavenSessionProvider = requireNonNull( mavenSessionProvider ); } @Override public PluginInfo getPluginInfo( final Artifact artifact ) { - MavenSession mavenSession = mavenSessionRef.get(); + MavenSession mavenSession = mavenSessionProvider.get(); if ( mavenSession != null ) { MavenProject mavenProject = searchForProject( mavenSession, artifact ); From 2fea9676953595b65fc132733b1f056e44c618c6 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Fri, 1 Oct 2021 14:16:31 +0200 Subject: [PATCH 7/7] Make UT nicer, exit session --- .../deployer/ArtifactDeployerTest.java | 27 ++++++++++++------- .../installer/ArtifactInstallerTest.java | 19 ++++++++----- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/maven-compat/src/test/java/org/apache/maven/artifact/deployer/ArtifactDeployerTest.java b/maven-compat/src/test/java/org/apache/maven/artifact/deployer/ArtifactDeployerTest.java index e2e24c3e7635..3f33fa1281e3 100644 --- a/maven-compat/src/test/java/org/apache/maven/artifact/deployer/ArtifactDeployerTest.java +++ b/maven-compat/src/test/java/org/apache/maven/artifact/deployer/ArtifactDeployerTest.java @@ -58,20 +58,27 @@ public void testArtifactInstallation() throws Exception { sessionScope.enter(); - sessionScope.seed(MavenSession.class, mock(MavenSession.class)); + try + { + sessionScope.seed(MavenSession.class, mock(MavenSession.class)); - String artifactBasedir = new File( getBasedir(), "src/test/resources/artifact-install" ).getAbsolutePath(); + String artifactBasedir = new File( getBasedir(), "src/test/resources/artifact-install" ).getAbsolutePath(); - Artifact artifact = createArtifact( "artifact", "1.0" ); + Artifact artifact = createArtifact( "artifact", "1.0" ); - File file = new File( artifactBasedir, "artifact-1.0.jar" ); - assertEquals( "dummy", FileUtils.fileRead( file, "UTF-8" ).trim() ); + File file = new File( artifactBasedir, "artifact-1.0.jar" ); + assertEquals( "dummy", FileUtils.fileRead( file, "UTF-8" ).trim() ); - artifactDeployer.deploy( file, artifact, remoteRepository(), localRepository() ); + artifactDeployer.deploy( file, artifact, remoteRepository(), localRepository() ); - ArtifactRepository remoteRepository = remoteRepository(); - File deployedFile = new File( remoteRepository.getBasedir(), remoteRepository.pathOf( artifact ) ); - assertTrue( deployedFile.exists() ); - assertEquals( "dummy", FileUtils.fileRead( deployedFile, "UTF-8" ).trim() ); + ArtifactRepository remoteRepository = remoteRepository(); + File deployedFile = new File( remoteRepository.getBasedir(), remoteRepository.pathOf( artifact ) ); + assertTrue( deployedFile.exists() ); + assertEquals( "dummy", FileUtils.fileRead( deployedFile, "UTF-8" ).trim() ); + } + finally + { + sessionScope.exit(); + } } } \ No newline at end of file diff --git a/maven-compat/src/test/java/org/apache/maven/artifact/installer/ArtifactInstallerTest.java b/maven-compat/src/test/java/org/apache/maven/artifact/installer/ArtifactInstallerTest.java index 7dc1ef8ae88b..afa276106b7c 100644 --- a/maven-compat/src/test/java/org/apache/maven/artifact/installer/ArtifactInstallerTest.java +++ b/maven-compat/src/test/java/org/apache/maven/artifact/installer/ArtifactInstallerTest.java @@ -54,16 +54,23 @@ public void testArtifactInstallation() throws Exception { sessionScope.enter(); - sessionScope.seed(MavenSession.class, mock(MavenSession.class)); + try + { + sessionScope.seed(MavenSession.class, mock(MavenSession.class)); - String artifactBasedir = new File( getBasedir(), "src/test/resources/artifact-install" ).getAbsolutePath(); + String artifactBasedir = new File( getBasedir(), "src/test/resources/artifact-install" ).getAbsolutePath(); - Artifact artifact = createArtifact( "artifact", "1.0" ); + Artifact artifact = createArtifact( "artifact", "1.0" ); - File source = new File( artifactBasedir, "artifact-1.0.jar" ); + File source = new File( artifactBasedir, "artifact-1.0.jar" ); - artifactInstaller.install( source, artifact, localRepository() ); + artifactInstaller.install( source, artifact, localRepository() ); - assertLocalArtifactPresent( artifact ); + assertLocalArtifactPresent( artifact ); + } + finally + { + sessionScope.exit(); + } } } \ No newline at end of file