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.facets;
  
 import java.io.File;
 import java.util.List;
 import java.util.Map;
 
 

Author(s):
Lincoln Baxter, III
 
 public class MavenContainer
 {
    private static final String M2_HOME = System.getenv().get("M2_HOME");
 
    private DefaultPlexusContainer container = null;
    private ProjectBuilder builder = null;
 
    @Inject
    private ForgeEnvironment environment;
 
    {
       return getBuildingRequest(.isOnline() == false);
    }
 
    {
       return getBuildingRequest(true);
    }
 
    public ProjectBuildingRequest getBuildingRequest(final boolean offline)
    {
       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();
          lookup(MavenExecutionRequestPopulator.class).populateFromSettings(executionRequestgetSettings());
          lookup(MavenExecutionRequestPopulator.class).populateDefaults(executionRequest);
           = executionRequest.getProjectBuildingRequest();
          ArtifactRepository localRepository = RepositoryUtils.toArtifactRepository("local",
                   new File(settings.getLocalRepository()).toURI().toURL().toString(), nulltruetrue);
          .setLocalRepository(localRepository);
          List<ArtifactRepositorysettingsRepos = new ArrayList<ArtifactRepository>(.getRemoteRepositories());
          List<StringactiveProfiles = settings.getActiveProfiles();
 
          @SuppressWarnings("unchecked")
          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));
               }
            }
         }
         .setRemoteRepositories(settingsRepos);
         .setSystemProperties(System.getProperties());
         MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
         Proxy activeProxy = settings.getActiveProxy();
         if (activeProxy != null)
         {
            DefaultProxySelector dps = new DefaultProxySelector();
            dps.add(RepositoryUtils.convertFromMavenProxy(activeProxy), activeProxy.getNonProxyHosts());
            repositorySession.setProxySelector(dps);
         }
         repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManager(settings.getLocalRepository()));
         repositorySession.setOffline(offline);
         List<Mirrormirrors = executionRequest.getMirrors();
         if (mirrors != null)
         {
            DefaultMirrorSelector mirrorSelector = new DefaultMirrorSelector();
            for (Mirror mirror : mirrors)
            {
               mirrorSelector.add(mirror.getId(), mirror.getUrl(), mirror.getLayout(), falsemirror.getMirrorOf(),
                        mirror.getMirrorOfLayouts());
            }
            repositorySession.setMirrorSelector(mirrorSelector);
         }
         .setRepositorySession(repositorySession);
         .setProcessPlugins(false);
         .setResolveDependencies(false);
         return ;
      }
      catch (Exception e)
      {
         throw new ProjectModelException(
                  "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);
      }
   }
   public Settings getSettings()
   {
      try
      {
         SettingsBuilder settingsBuilder = new DefaultSettingsBuilderFactory().newInstance();
         SettingsBuildingRequest settingsRequest = new DefaultSettingsBuildingRequest();
         settingsRequest
                  .setUserSettingsFile(new File(OSUtils.getUserHomeDir().getAbsolutePath() + "/.m2/settings.xml"));
         if ( != null)
            settingsRequest.setGlobalSettingsFile(new File( + "/conf/settings.xml"));
         SettingsBuildingResult settingsBuildingResult = settingsBuilder.build(settingsRequest);
         Settings effectiveSettings = settingsBuildingResult.getEffectiveSettings();
         if (effectiveSettings.getLocalRepository() == null)
         {
            effectiveSettings.setLocalRepository(OSUtils.getUserHomeDir().getAbsolutePath() + "/.m2/repository");
         }
         return effectiveSettings;
      }
      catch (SettingsBuildingException e)
      {
         throw new ProjectModelException(e);
      }
   }
   public ProjectBuilder getBuilder()
   {
      return ;
   }
   public <T> T lookup(Class<T> type)
   {
      ClassLoader cl = Thread.currentThread().getContextClassLoader();
      try
      {
         return getContainer().lookup(type);
      }
      catch (ComponentLookupException e)
      {
         throw new ProjectModelException("Could not look up component of type [" + type.getName() + "]"e);
      }
      finally
      {
         /*
          * We reset the classloader to prevent potential modules bugs if Classwords container changes classloaders on
          * us
          */
         Thread.currentThread().setContextClassLoader(cl);
      }
   }
   {
      if ( == null)
      {
         ClassLoader cl = Thread.currentThread().getContextClassLoader();
         try
         {
             = new DefaultPlexusContainer();
            ConsoleLoggerManager loggerManager = new ConsoleLoggerManager();
            loggerManager.setThreshold("ERROR");
            getContainer().setLoggerManager(loggerManager);
             = getContainer().lookup(ProjectBuilder.class);
         }
         catch (Exception e)
         {
            throw new ProjectModelException(
                     "Could not initialize Maven"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