Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012 Red Hat, Inc. and/or its affiliates.
   *
   * Licensed under the Eclipse Public License version 1.0, available at
   * http://www.eclipse.org/legal/epl-v10.html
   */
  package org.jboss.forge.maven.dependencies;
  
  import java.io.File;
 import java.util.List;
 
 
 import  org.jboss.forge.parser.java.util.Strings;

Author(s):
Lincoln Baxter, III
 
 public class RepositoryLookup implements DependencyResolverProvider
 {
    private MavenContainer container;
    private ResourceFactory factory;
    private ForgeEnvironment environment;
 
    public RepositoryLookup()
    {
    }
 
    @Inject
    public RepositoryLookup(final MavenContainer containerfinal ResourceFactory factory,
             final ForgeEnvironment environment)
    {
       this. = container;
       this. = factory;
       this. = environment;
    }
 
    @Override
    public List<DependencyResourceresolveArtifacts(final Dependency query)
    {
       return resolveArtifacts(querynew ArrayList<DependencyRepository>());
    }
 
    @Override
    public List<DependencyResourceresolveArtifacts(final Dependency queryfinal DependencyRepository repository)
    {
       return resolveArtifacts(query, Arrays.asList(repository));
    }
 
    @Override
    public List<DependencyResourceresolveArtifacts(final Dependency depfinal List<DependencyRepositoryrepositories)
    {
       List<DependencyResourceresult = new ArrayList<DependencyResource>();
 
       RepositorySystem system = .lookup(RepositorySystem.class);

      
First try resolving the artifact directly from the local repository - then fall back to aether. This may be a bad practice but we can revisit if problems arise.
      if (dep.getVersion() != null)
      {
                  .getLocalRepository()));
         if ((dir != null) && dir.exists())
         {
            List<Stringsegments = new ArrayList<String>();
            segments.addAll(Arrays.asList((dep.getGroupId() + "." + dep.getArtifactId()).split("\\.")));
            segments.add(dep.getVersion());
            for (String seg : segments)
            {
               dir = dir.getChildDirectory(seg);
               if (!dir.isDirectory())
               {
                  break;
               }
            }
            if (dir.isDirectory())
            {
               Resource<?> jar = dir.getChild(dep.getArtifactId() + "-" + dep.getVersion() + "."
                        + dep.getPackagingType());
               if (jar.exists())
               {
                  FileResource<?> jarFile = jar.reify(FileResource.class);
                  result.add(new DependencyResource(jarFile.getResourceFactory(), jarFile
                           .getUnderlyingResourceObject(), dep));
               }
            }
         }
      }
      if (result.isEmpty())
      {
         MavenRepositorySystemSession session = setupRepoSession(system);
         session.setIgnoreInvalidArtifactDescriptor(true);
         session.setIgnoreMissingArtifactDescriptor(true);
         VersionRangeResult versions = getVersions(depconvertToMavenRepos(repositories));
         VERSION: for (Version version : versions.getVersions())
         {
            ArtifactRepository ar = versions.getRepository(version);
            DependencyBuilder currentVersion = DependencyBuilder.create(dep).setVersion(version.toString());
            Artifact artifact = dependencyToMavenArtifact(currentVersion);
            if (ar instanceof LocalRepository)
            {
               LocalArtifactRequest request = new LocalArtifactRequest(artifactnullnull);
               LocalArtifactResult a = session.getLocalRepositoryManager().find(sessionrequest);
               File file = a.getFile();
               DependencyResource resource = new DependencyResource(filecurrentVersion);
               if (!result.contains(resource))
               {
                  result.add(resource);
                  continue VERSION;
               }
            }
            if (ar instanceof RemoteRepository)
            {
               ArtifactRequest request = new ArtifactRequest();
               RemoteRepository remoteRepo = new RemoteRepository(ar.getId(), ar.getContentType(),
                        ((RemoteRepositoryar).getUrl());
               request.addRepository(remoteRepo);
               request.setArtifact(artifact);
               try
               {
                  ArtifactResult a = system.resolveArtifact(sessionrequest);
                  File file = a.getArtifact().getFile();
                  DependencyResource resource = new DependencyResource(filecurrentVersion);
                  if (!result.contains(resource))
                  {
                     result.add(resource);
                     continue VERSION;
                  }
               }
               catch (ArtifactResolutionException e)
               {
                  ..println(e.getMessage());
               }
            }
         }
      }
      return result;
   }
   {
      return resolveDependencies(querynew ArrayList<DependencyRepository>());
   }
   public List<DependencyResourceresolveDependencies(final Dependency queryfinal DependencyRepository repository)
   {
      return resolveDependencies(query, Arrays.asList(repository));
   }
   {
      List<DependencyResourceresult = new ArrayList<DependencyResource>();
      try
      {
         if (Strings.isNullOrEmpty(dep.getVersion()))
         {
            dep = DependencyBuilder.create(dep).setVersion("[,)");
         }
         RepositorySystem system = .lookup(RepositorySystem.class);
         MavenRepositorySystemSession session = setupRepoSession(system);
         Artifact artifact = dependencyToMavenArtifact(dep);
         CollectRequest collectRequest = new CollectRequest(new org.sonatype.aether.graph.Dependency(artifactnull),
                  convertToMavenRepos(repositories));
         DependencyRequest request = new DependencyRequest(collectRequestnull);
         DependencyResult artifacts = system.resolveDependencies(sessionrequest);
         for (ArtifactResult a : artifacts.getArtifactResults())
         {
            File file = a.getArtifact().getFile();
            Dependency d = DependencyBuilder.create().setArtifactId(a.getArtifact().getArtifactId())
                     .setGroupId(a.getArtifact().getGroupId()).setVersion(a.getArtifact().getBaseVersion())
                     .setPackagingType(a.getArtifact().getExtension());
            DependencyResource resource = new DependencyResource(filed);
            result.add(resource);
         }
         return result;
      }
      catch (Exception e)
      {
         throw new ProjectModelException("Unable to resolve an artifact"e);
      }
   }
   {
      return resolveDependencyMetadata(querynew ArrayList<DependencyRepository>());
   }
   public DependencyMetadata resolveDependencyMetadata(final Dependency queryfinal DependencyRepository repository)
   {
      return resolveDependencyMetadata(query, Arrays.asList(repository));
   }
   {
      try
      {
         if (Strings.isNullOrEmpty(query.getVersion()))
         {
            query = DependencyBuilder.create(query).setVersion("[,)");
         }
         RepositorySystem system = .lookup(RepositorySystem.class);
         MavenRepositorySystemSession session = setupRepoSession(system);
         Artifact artifact = dependencyToMavenArtifact(query);
         ArtifactDescriptorRequest ar = new ArtifactDescriptorRequest(artifactconvertToMavenRepos(repositories), null);
         ArtifactDescriptorResult results = system.readArtifactDescriptor(sessionar);
         Artifact a = results.getArtifact();
         Dependency d = DependencyBuilder.create().setArtifactId(a.getArtifactId()).setGroupId(a.getGroupId())
                  .setVersion(a.getBaseVersion());
         return new DependencyMetadataImpl(dresults);
      }
      catch (Exception e)
      {
         throw new ProjectModelException("Unable to resolve any artifacts for query [" + query + "]"e);
      }
   }
   public List<DependencyresolveVersions(final Dependency query)
   {
      return resolveVersions(querynew ArrayList<DependencyRepository>());
   }
   public List<DependencyresolveVersions(final Dependency queryfinal DependencyRepository repository)
   {
      return resolveVersions(query, Arrays.asList(repository));
   }
   public List<DependencyresolveVersions(final Dependency depfinal List<DependencyRepositoryrepositories)
   {
      List<Dependencyresult = new ArrayList<Dependency>();
      List<RemoteRepositoryremoteRepos = convertToMavenRepos(repositories);
      VersionRangeResult r = getVersions(depremoteRepos);
      for (Version v : r.getVersions())
      {
         result.add(DependencyBuilder.create(dep).setVersion(v.toString()));
      }
      return result;
   }
   {
      session.setOffline(!.isOnline());
      Settings settings = .getSettings();
      LocalRepository localRepo = new LocalRepository(new File(settings.getLocalRepository()), "");
      session.setLocalRepositoryManager(repoSystem.newLocalRepositoryManager(localRepo));
      session.setTransferErrorCachingEnabled(false);
      session.setNotFoundCachingEnabled(false);
      return session;
   }
   {
      RemoteRepository remoteRepository = new RemoteRepository(repo.getId(), "default"repo.getUrl());
      Settings settings = .getSettings();
      Proxy activeProxy = settings.getActiveProxy();
      if (activeProxy != null)
      {
         remoteRepository.setProxy(RepositoryUtils.convertFromMavenProxy(activeProxy));
      }
      return remoteRepository;
   }
   private List<RemoteRepositoryconvertToMavenRepos(final List<DependencyRepositoryrepositories)
   {
      List<RemoteRepositoryremoteRepos = new ArrayList<RemoteRepository>();
      if (repositories == null || repositories.size() == 0)
      {
         List<org.apache.maven.artifact.repository.ArtifactRepositoryremoteRepositories = .getRequest()
                  .getRemoteRepositories();
         remoteRepos.addAll(org.apache.maven.RepositoryUtils.toRepos(remoteRepositories));
      }
      else
      {
         temp.addAll(repositories);
         for (DependencyRepository deprep : temp)
         {
            remoteRepos.add(convertToMavenRepo(deprep));
         }
      }
      return remoteRepos;
   }
   private VersionRangeResult getVersions(Dependency depfinal List<RemoteRepositoryrepositories)
   {
      try
      {
         String version = dep.getVersion();
         if (Strings.isNullOrEmpty(version))
         {
            dep = DependencyBuilder.create(dep).setVersion("[,)");
         }
         else if (!version.matches("(\\(|\\[).*?(\\)|\\])"))
         {
            dep = DependencyBuilder.create(dep).setVersion("[" + version + "]");
         }
         RepositorySystem maven = .lookup(RepositorySystem.class);
         MavenRepositorySystemSession session = setupRepoSession(maven);
         Artifact artifact = dependencyToMavenArtifact(dep);
         VersionRangeRequest rangeRequest = new VersionRangeRequest(artifactrepositoriesnull);
         VersionRangeResult rangeResult = maven.resolveVersionRange(sessionrangeRequest);
         return rangeResult;
      }
      catch (Exception e)
      {
         throw new ProjectModelException("Failed to look up versions for [" + dep + "]"e);
      }
   }
   {
      Artifact artifact = new DefaultArtifact(dep.getGroupId(), dep.getArtifactId(), dep.getClassifier(),
               dep.getPackagingType() == null ? "jar" : dep.getPackagingType(), dep.getVersion());
      return artifact;
   }
New to GrepCode? Check out our FAQ X