Uploaded image for project: 'Maven'
  1. Maven
  2. MNG-5250

DefaultVersionResolver.readVersions reads snapshot versions from release only repositories

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Incomplete
    • 3.0.4
    • None
    • Dependencies, Embedding
    • None
    • embedded maven

    Description

      with the below repositories configuration in settings.xml [1]
      I get an error that a parent of a project is not found if the parent version is marked as LATEST.
      The actual error is
      "Could not find artifact XXX in snapshots (URL of SNAPSHOT repository)"
      while debugging the reason, I have found that because the DefaultVersionResolver.readVersions() method returns SNAPSHOT versions when reading versions from a release only repository (repository with snapshot policy not enabled).

      The fix I have in place is to add to the DefaultVersionResolver.readVersions() method the following fragment:
      (see the full method below [2]

      /*
       * fix for LATEST - repository who does not support snapshots should not return shapshot versions
       */
      if (versioning != null && repository instanceof RemoteRepository) {
        RemoteRepository remoteRepository = (RemoteRepository)repository;
        if (!remoteRepository.getPolicy(true).isEnabled()) {
          Versioning repaired = new Versioning();
          repaired.setLastUpdated(versioning.getLastUpdated());
          repaired.setRelease(versioning.getRelease());
          repaired.setVersions(versioning.getVersions());
          versioning = repaired;
        }
      }
      

      [1] :

      <repositories>
              <repository>
                <snapshots>
                  <enabled>false</enabled>
                </snapshots>
                <id>central</id>
                <name>libs-releases</name>
                <url>http://repo.dev.wix/artifactory/libs-releases</url>
              </repository>
              <repository>
                <snapshots />
                <id>snapshots</id>
                <name>libs-snapshots</name>
                <url>http://repo.dev.wix/artifactory/libs-snapshots</url>
              </repository>
            </repositories>
      

      [2] :

          private Versioning readVersions( RepositorySystemSession session, RequestTrace trace, Metadata metadata,
                                           ArtifactRepository repository, VersionResult result )
          {
              Versioning versioning = null;
      
              FileInputStream fis = null;
              try
              {
                  if ( metadata != null )
                  {
                      SyncContext syncContext = syncContextFactory.newInstance( session, true );
      
                      try
                      {
                          syncContext.acquire( null, Collections.singleton( metadata ) );
      
                          if ( metadata.getFile() != null && metadata.getFile().exists() )
                          {
                              fis = new FileInputStream( metadata.getFile() );
                              org.apache.maven.artifact.repository.metadata.Metadata m =
                                  new MetadataXpp3Reader().read( fis, false );
                              versioning = m.getVersioning();
      
                              /*
                               * NOTE: Users occasionally misuse the id "local" for remote repos which screws up the metadata
                               * of the local repository. This is especially troublesome during snapshot resolution so we try
                               * to handle that gracefully.
                               */
                              if ( versioning != null && repository instanceof LocalRepository )
                              {
                                  if ( versioning.getSnapshot() != null && versioning.getSnapshot().getBuildNumber() > 0 )
                                  {
                                      Versioning repaired = new Versioning();
                                      repaired.setLastUpdated( versioning.getLastUpdated() );
                                      Snapshot snapshot = new Snapshot();
                                      snapshot.setLocalCopy( true );
                                      repaired.setSnapshot( snapshot );
                                      versioning = repaired;
      
                                      throw new IOException( "Snapshot information corrupted with remote repository data"
                                          + ", please verify that no remote repository uses the id '" + repository.getId()
                                          + "'" );
                                  }
                              }
      
                              /*
                               * fix for LATEST - repository who does not support snapshots should not return shapshot versions
                               */
                              if (versioning != null && repository instanceof RemoteRepository) {
                                  RemoteRepository remoteRepository = (RemoteRepository)repository;
                                  if (!remoteRepository.getPolicy(true).isEnabled()) {
                                      Versioning repaired = new Versioning();
                                      repaired.setLastUpdated(versioning.getLastUpdated());
                                      repaired.setRelease(versioning.getRelease());
                                      repaired.setVersions(versioning.getVersions());
                                      versioning = repaired;
                                  }
                              }
                          }
                      }
                      finally
                      {
                          syncContext.release();
                      }
                  }
              }
              catch ( Exception e )
              {
                  invalidMetadata( session, trace, metadata, repository, e );
                  result.addException( e );
              }
              finally
              {
                  IOUtil.close( fis );
              }
      
              return ( versioning != null ) ? versioning : new Versioning();
          }
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            yoavaa Yoav Abrahami
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: