This is an automated email from the ASF dual-hosted git repository. cstamas pushed a commit to branch fix-group-metadata-deploy in repository https://gitbox.apache.org/repos/asf/maven.git
commit c15750b17cf8c2c50f39aa84911d411cd1a4462e Author: Tamas Cservenak <[email protected]> AuthorDate: Tue Sep 28 16:11:13 2021 +0200 Fix G level metadata handling Just make Resolver handle it. This is TBD, as actual prefix still needs somehow to get there. --- .../MavenSessionPluginsMetadataInfoProvider.java | 102 ++++++++++++++++ .../MavenPluginLifecycleMappingProvider.java | 6 +- .../maven/repository/internal/PluginsMetadata.java | 112 ++++++++++++++++++ .../internal/PluginsMetadataGenerator.java | 130 +++++++++++++++++++++ .../internal/PluginsMetadataGeneratorFactory.java | 67 +++++++++++ .../internal/PluginsMetadataInfoProvider.java | 44 +++++++ 6 files changed, 457 insertions(+), 4 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 new file mode 100644 index 0000000..2aa965e --- /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<MavenSession> 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 c01afc7..8ab0883 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 final class 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 0000000..7c7ab0e --- /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<Plugin> recessivePlugins = recessive.getPlugins(); + List<Plugin> plugins = metadata.getPlugins(); + if ( !plugins.isEmpty() ) + { + LinkedHashMap<String, Plugin> 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 0000000..4f35568 --- /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<Object, PluginsMetadata> plugins; + + private final Map<Object, PluginsMetadata> 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<? extends Metadata> 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<? extends Metadata> 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<? extends Metadata> prepare( Collection<? extends Artifact> artifacts ) + { + return Collections.emptyList(); + } + + @Override + public Artifact transformArtifact( Artifact artifact ) + { + return artifact; + } + + @Override + public Collection<? extends Metadata> finish( Collection<? extends Artifact> 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 0000000..19d499b --- /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 0000000..629a23f --- /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 ); +}
