Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2013 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.arquillian.maven;
  
 import java.io.File;
 import java.util.List;
 import java.util.Map;
 
 
 public class ProjectHelper
 {
    private final MavenContainer mavenContainer;
 
    public ProjectHelper()
    {
       this. = new MavenContainer();
    }
 
    public Model loadPomFromFile(File pomFileString... profiles)
    {
       Settings settings = .getSettings();
       DefaultRepositorySystemSession session = .setupRepoSession(systemsettings);
       final DefaultModelBuildingRequest request = new DefaultModelBuildingRequest()
                .setSystemProperties(System.getProperties())
                .setPomFile(pomFile)
                .setActiveProfileIds(settings.getActiveProfiles());
       ModelBuilder builder = new DefaultModelBuilderFactory().newInstance();
       ModelBuildingResult result;
       try
       {
          request.setModelResolver(new MavenModelResolver(systemsession
                   .getEnabledRepositoriesFromProfile(settings)));
          result = builder.build(request);
       }
       // wrap exception message
       catch (ModelBuildingException e)
       {
          String pomPath = request.getPomFile().getAbsolutePath();
          StringBuilder sb = new StringBuilder("Found ").append(e.getProblems().size())
                   .append(" problems while building POM model from ").append(pomPath).append("\n");
 
          int counter = 1;
          for (ModelProblem problem : e.getProblems())
          {
             sb.append(counter++).append("/ ").append(problem).append("\n");
          }
 
          throw new RuntimeException(sb.toString());
       }
       return result.getEffectiveModel();
    }
 
    public List<DependencyresolveDependenciesFromPOM(File pomFilethrows Exception
    {
       PlexusContainer plexus = new PlexusContainer();
       List<Dependencyresult;
       try
       {
          ProjectBuildingRequest request = getBuildingRequest(plexus);
          request.setResolveDependencies(true);
          ProjectBuilder builder = plexus.lookup(ProjectBuilder.class);
          ProjectBuildingResult build = builder.build(pomFilerequest);
          result = build.getDependencyResolutionResult().getDependencies();
      }
      finally
      {
         plexus.shutdown();
      }
      return result;
   }
   {
      if (this. == null)
      {
         ClassLoader cl = Thread.currentThread().getContextClassLoader();
         try
         {
            Settings settings = .getSettings();
            // TODO this needs to be configurable via .forge
            // TODO this reference to the M2_REPO should probably be centralized
            MavenExecutionRequest executionRequest = new DefaultMavenExecutionRequest();
            RepositorySystem repositorySystem = plexus.lookup(RepositorySystem.class);
            MavenExecutionRequestPopulator requestPopulator = plexus.lookup(MavenExecutionRequestPopulator.class);
            requestPopulator.populateFromSettings(executionRequestsettings);
            requestPopulator.populateDefaults(executionRequest);
            ProjectBuildingRequest request = executionRequest.getProjectBuildingRequest();
            org.apache.maven.artifact.repository.ArtifactRepository localRepository = RepositoryUtils
                     .toArtifactRepository("local",
                              new File(settings.getLocalRepository()).toURI().toURL().toString(), nulltruetrue);
            request.setLocalRepository(localRepository);
                     request.getRemoteRepositories());
            List<StringactiveProfiles = settings.getActiveProfiles();
            Map<StringProfileprofiles = settings.getProfilesAsMap();
            for (String id : activeProfiles)
            {
               Profile profile = profiles.get(id);
               if (profile != null)
               {
                  List<Repositoryrepositories = profile.getRepositories();
                  for (Repository repository : repositories)
                  {
                     settingsRepos.add(RepositoryUtils.convertFromMavenSettingsRepository(repository));
                  }
               }
            }
            request.setRemoteRepositories(settingsRepos);
            request.setSystemProperties(System.getProperties());
            DefaultRepositorySystemSession repositorySession = MavenRepositorySystemUtils.newSession();
            Proxy activeProxy = settings.getActiveProxy();
            if (activeProxy != null)
            {
               DefaultProxySelector dps = new DefaultProxySelector();
               dps.add(RepositoryUtils.convertFromMavenProxy(activeProxy), activeProxy.getNonProxyHosts());
               repositorySession.setProxySelector(dps);
            }
            LocalRepository localRepo = new LocalRepository(settings.getLocalRepository());
            repositorySession.setLocalRepositoryManager(repositorySystem.newLocalRepositoryManager(repositorySession,
                     localRepo));
            repositorySession.setOffline(settings.isOffline());
            List<Mirrormirrors = executionRequest.getMirrors();
            DefaultMirrorSelector mirrorSelector = new DefaultMirrorSelector();
            if (mirrors != null)
            {
               for (Mirror mirror : mirrors)
               {
                  mirrorSelector.add(mirror.getId(), mirror.getUrl(), mirror.getLayout(), falsemirror.getMirrorOf(),
                           mirror.getMirrorOfLayouts());
               }
            }
            repositorySession.setMirrorSelector(mirrorSelector);
            
            LazyAuthenticationSelector authSelector = new LazyAuthenticationSelector(mirrorSelector);
            for (Server server : settings.getServers())
            {
               authSelector.add(
                        server.getId(),
                        new AuthenticationBuilder().addUsername(server.getUsername()).addPassword(server.getPassword())
                                 .addPrivateKey(server.getPrivateKey(), server.getPassphrase()).build());
            }
            repositorySession.setAuthenticationSelector(authSelector);
            request.setRepositorySession(repositorySession);
            request.setProcessPlugins(false);
            request.setResolveDependencies(false);
            this. = request;
         }
         catch (RuntimeException e)
         {
            throw e;
         }
         catch (Exception e)
         {
            throw new RuntimeException(
                     "Could not create Maven project building request"e);
         }
         finally
         {
            /*
             * We reset the classloader to prevent potential modules bugs if Classwords container changes classloaders
             * on us
             */
            Thread.currentThread().setContextClassLoader(cl);
         }
      }
      return ;
   }
New to GrepCode? Check out our FAQ X