<commit_before>package org.apache.maven.plugin.version.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.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.metadata.Metadata;
import org.apache.maven.artifact.repository.metadata.io.MetadataReader;
import org.apache.maven.model.Build;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.version.PluginVersionRequest;
import org.apache.maven.plugin.version.PluginVersionResolutionException;
import org.apache.maven.plugin.version.PluginVersionResolver;
import org.apache.maven.plugin.version.PluginVersionResult;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.repository.ArtifactDoesNotExistException;
import org.apache.maven.repository.ArtifactTransferFailedException;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.StringUtils;

/**
 * Resolves a version for a plugin.
 * 
 * @since 3.0-alpha-3
 * @author Benjamin Bentmann
 */
@Component( role = PluginVersionResolver.class )
public class DefaultPluginVersionResolver
    implements PluginVersionResolver
{

    @Requirement
    private Logger logger;

    @Requirement
    private RepositorySystem repositorySystem;

    @Requirement
    private MetadataReader metadataReader;

    public PluginVersionResult resolve( PluginVersionRequest request )
        throws PluginVersionResolutionException
    {
        logger.debug( "Resolving plugin version for " + request.getGroupId() + ":" + request.getArtifactId() );

        PluginVersionResult result = resolveFromProject( request );

        if ( result == null )
        {
            result = resolveFromRepository( request );

            if ( StringUtils.isEmpty( result.getVersion() ) )
            {
                throw new PluginVersionResolutionException( request.getGroupId(), request.getArtifactId(),
                                                            request.getLocalRepository(),
                                                            request.getRemoteRepositories(),
                                                            "Plugin not found in any plugin repository" );
            }
            else if ( logger.isDebugEnabled() )
            {
                logger.debug( "Resolved plugin version for " + request.getGroupId() + ":" + request.getArtifactId()
                    + " to " + result.getVersion() + " from repository "
                    + ( result.getRepository() != null ? result.getRepository().getId() : "null" ) );
            }
        }
        else if ( logger.isDebugEnabled() )
        {
            logger.debug( "Resolved plugin version for " + request.getGroupId() + ":" + request.getArtifactId()
                + " to " + result.getVersion() + " from POM " + request.getPom() );
        }

        return result;
    }

    private PluginVersionResult resolveFromRepository( PluginVersionRequest request )
        throws PluginVersionResolutionException
    {
        DefaultPluginVersionResult result = new DefaultPluginVersionResult();

        Metadata mergedMetadata = new Metadata();

        ArtifactRepository localRepository = request.getLocalRepository();

        // Search in remote repositories for a (released) version.
        //
        // maven-metadata-{central|nexus|...}.xml
        //
        // TODO: we should cycle through the repositories but take the repository which actually satisfied the prefix.
        for ( ArtifactRepository repository : request.getRemoteRepositories() )
        {
            String localPath = getLocalMetadataPath( request, repository );

            File artifactMetadataFile = new File( localRepository.getBasedir(), localPath );

            if ( !request.isOffline() && ( !artifactMetadataFile.exists() /* || user requests snapshot updates */ ) )
            {
                String remotePath = getRemoteMetadataPath( request, repository );

                try
                {
                    repositorySystem.retrieve( repository, artifactMetadataFile, remotePath,
                                               request.getTransferListener() );
                }
                catch ( ArtifactTransferFailedException e )
                {
                    if ( logger.isDebugEnabled() )
                    {
                        logger.warn( "Failed to retrieve " + remotePath + " from " + repository.getId() + ": "
                            + e.getMessage(), e );
                    }
                    else
                    {
                        logger.warn( "Failed to retrieve " + remotePath + " from " + repository.getId() + ": "
                            + e.getMessage() );
                    }

                    continue;
                }
                catch ( ArtifactDoesNotExistException e )
                {
                    continue;
                }
            }

            if ( mergeMetadata( mergedMetadata, artifactMetadataFile ) )
            {
                result.setRepository( repository );
            }
        }

        // Search in the local repositiory for a (development) version
        //
        // maven-metadata-local.xml
        //
        {
            String localPath = getLocalMetadataPath( request, localRepository );

            File artifactMetadataFile = new File( localRepository.getBasedir(), localPath );

            if ( mergeMetadata( mergedMetadata, artifactMetadataFile ) )
            {
                result.setRepository( localRepository );
            }
        }

        if ( mergedMetadata.getVersioning() != null )
        {
            String release = mergedMetadata.getVersioning().getRelease();

            if ( StringUtils.isNotEmpty( release ) )
            {
                result.setVersion( release );
            }
            else
            {
                String latest = mergedMetadata.getVersioning().getLatest();

                if ( StringUtils.isNotEmpty( latest ) )
                {
                    result.setVersion( latest );
                }
            }
        }

        if ( StringUtils.isEmpty( result.getVersion() ) )
        {
            throw new PluginVersionResolutionException( request.getGroupId(), request.getArtifactId(),
                                                        request.getLocalRepository(), request.getRemoteRepositories(),
                                                        "Plugin not found in any plugin repository" );
        }

        return result;
    }

    private String getLocalMetadataPath( PluginVersionRequest request, ArtifactRepository repository )
    {
        return request.getGroupId().replace( '.', '/' ) + '/' + request.getArtifactId() + "/maven-metadata-"
            + repository.getId() + ".xml";<commit_msg>[MNG-4772] [regression] Plugin version resolution ignores enabled flags on repository<commit_after>package org.apache.maven.plugin.version.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.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.metadata.Metadata;
import org.apache.maven.artifact.repository.metadata.io.MetadataReader;
import org.apache.maven.model.Build;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.version.PluginVersionRequest;
import org.apache.maven.plugin.version.PluginVersionResolutionException;
import org.apache.maven.plugin.version.PluginVersionResolver;
import org.apache.maven.plugin.version.PluginVersionResult;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.repository.ArtifactDoesNotExistException;
import org.apache.maven.repository.ArtifactTransferFailedException;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.StringUtils;

/**
 * Resolves a version for a plugin.
 * 
 * @since 3.0-alpha-3
 * @author <NAME>
 */
@Component( role = PluginVersionResolver.class )
public class DefaultPluginVersionResolver
    implements PluginVersionResolver
{

    @Requirement
    private Logger logger;

    @Requirement
    private RepositorySystem repositorySystem;

    @Requirement
    private MetadataReader metadataReader;

    public PluginVersionResult resolve( PluginVersionRequest request )
        throws PluginVersionResolutionException
    {
        logger.debug( "Resolving plugin version for " + request.getGroupId() + ":" + request.getArtifactId() );

        PluginVersionResult result = resolveFromProject( request );

        if ( result == null )
        {
            result = resolveFromRepository( request );

            if ( StringUtils.isEmpty( result.getVersion() ) )
            {
                throw new PluginVersionResolutionException( request.getGroupId(), request.getArtifactId(),
                                                            request.getLocalRepository(),
                                                            request.getRemoteRepositories(),
                                                            "Plugin not found in any plugin repository" );
            }
            else if ( logger.isDebugEnabled() )
            {
                logger.debug( "Resolved plugin version for " + request.getGroupId() + ":" + request.getArtifactId()
                    + " to " + result.getVersion() + " from repository "
                    + ( result.getRepository() != null ? result.getRepository().getId() : "null" ) );
            }
        }
        else if ( logger.isDebugEnabled() )
        {
            logger.debug( "Resolved plugin version for " + request.getGroupId() + ":" + request.getArtifactId()
                + " to " + result.getVersion() + " from POM " + request.getPom() );
        }

        return result;
    }

    private PluginVersionResult resolveFromRepository( PluginVersionRequest request )
        throws PluginVersionResolutionException
    {
        DefaultPluginVersionResult result = new DefaultPluginVersionResult();

        Metadata mergedMetadata = new Metadata();

        ArtifactRepository localRepository = request.getLocalRepository();

        // Search in remote repositories for a (released) version.
        //
        // maven-metadata-{central|nexus|...}.xml
        //
        // TODO: we should cycle through the repositories but take the repository which actually satisfied the prefix.
        for ( ArtifactRepository repository : request.getRemoteRepositories() )
        {
            if ( !isEnabled( repository ) )
            {
                logger.debug( "Skipped plugin version lookup from disabled repository " + repository.getId() );
                continue;
            }

            String localPath = getLocalMetadataPath( request, repository );

            File artifactMetadataFile = new File( localRepository.getBasedir(), localPath );

            if ( !request.isOffline() && ( !artifactMetadataFile.exists() /* || user requests snapshot updates */ ) )
            {
                String remotePath = getRemoteMetadataPath( request, repository );

                try
                {
                    repositorySystem.retrieve( repository, artifactMetadataFile, remotePath,
                                               request.getTransferListener() );
                }
                catch ( ArtifactTransferFailedException e )
                {
                    if ( logger.isDebugEnabled() )
                    {
                        logger.warn( "Failed to retrieve " + remotePath + " from " + repository.getId() + ": "
                            + e.getMessage(), e );
                    }
                    else
                    {
                        logger.warn( "Failed to retrieve " + remotePath + " from " + repository.getId() + ": "
                            + e.getMessage() );
                    }

                    continue;
                }
                catch ( ArtifactDoesNotExistException e )
                {
                    continue;
                }
            }

            if ( mergeMetadata( mergedMetadata, artifactMetadataFile ) )
            {
                result.setRepository( repository );
            }
        }

        // Search in the local repositiory for a (development) version
        //
        // maven-metadata-local.xml
        //
        {
            String localPath = getLocalMetadataPath( request, localRepository );

            File artifactMetadataFile = new File( localRepository.getBasedir(), localPath );

            if ( mergeMetadata( mergedMetadata, artifactMetadataFile ) )
            {
                result.setRepository( localRepository );
            }
        }

        if ( mergedMetadata.getVersioning() != null )
        {
            String release = mergedMetadata.getVersioning().getRelease();

            if ( StringUtils.isNotEmpty( release ) )
            {
                result.setVersion( release );
            }
            else
            {
                String latest = mergedMetadata.getVersioning().getLatest();

                if ( StringUtils.isNotEmpty( latest ) )
                {
                    result.setVersion( latest );
                }
            }
        }

        if ( StringUtils.isEmpty( result.getVersion() ) )
        {
            throw new PluginVersionResolutionException( request.getGroupId(), request.getArtifactId(),
                                                        request.getLocalRepository(), request.getRemoteRepositories(),
                                                        "Plugin not found in any plugin repository" );
        }

        return result;
    }

    private boolean isEnabled( ArtifactRepository repository )
    {
        return repository.getReleases().isEnabled() || repository.getSnapshots().isEnabled();
    }

    private String getLocalMetadataPath( PluginVersionRequest request, ArtifactRepository repository )
    {
        return request.getGroupId().replace( '.', '/' ) + '/' + request.getArtifactId() + "/maven-metadata-"
            + repository.getId() + ".xml";
{'fraction_non_alphanumeric': 0.08806747705284049, 'fraction_numerical': 0.001116348300669809, 'mean_word_length': 1.959258579555882, 'pattern_counts': {'":': 10, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7665698', 'n_tokens_mistral': 3819, 'n_tokens_neox': 3607, 'n_words': 932}