Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, by Red Hat.
   * Copyright 2010, Red Hat, Inc., and individual contributors
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.jboss.forge.maven.facets;
 
 import java.util.List;
 import java.util.Map;
 
 

Author(s):
Lincoln Baxter, III
 
 @Alias("forge.maven.MavenDependencyFacet")
 public class MavenDependencyFacet extends BaseFacet implements DependencyFacetFacet
 {
    private final DependencyResolver resolver;
 
    private final EventBus bus;
 
    @Inject
    public MavenDependencyFacet(final DependencyResolver resolverfinal EventBus bus)
    {
       this. = resolver;
       this. = bus;
    }
 
    @Override
    public boolean isInstalled()
    {
       try
       {
          .getFacet(MavenCoreFacet.class);
          return true;
       }
       catch (FacetNotFoundException e)
       {
          return false;
       }
    }
 
    @Override
    public boolean install()
    {
       return true;
    }
 
    @Override
    public void addDependency(final Dependency dep)
    {
       MavenCoreFacet maven = .getFacet(MavenCoreFacet.class);
       if (!hasDependency(dep))
      {
         Model pom = maven.getPOM();
         List<Dependencydependencies = MavenDependencyAdapter.fromMavenList(pom.getDependencies());
         dependencies.add(dep);
         pom.setDependencies(MavenDependencyAdapter.toMavenList(dependencies));
         maven.setPOM(pom);
         .enqueue(new AddedDependencies(dep));
      }
   }
   public boolean hasDependency(final Dependency dep)
   {
      MavenCoreFacet maven = .getFacet(MavenCoreFacet.class);
      List<Dependencydependencies = MavenDependencyAdapter.fromMavenList(maven.getPartialProjectBuildingResult()
               .getProject().getDependencies());
      for (Dependency dependency : dependencies)
      {
         if (DependencyBuilder.areEquivalent(dependencydep))
         {
            return true;
         }
      }
      return false;
   }
   public boolean hasDirectDependency(final Dependency dependency)
   {
      MavenCoreFacet maven = .getFacet(MavenCoreFacet.class);
      Model pom = maven.getPOM();
      List<Dependencydependencies = MavenDependencyAdapter.fromMavenList(pom.getDependencies());
      for (Dependency dep : dependencies)
      {
         if (DependencyBuilder.areEquivalent(dependencydep))
         {
            return true;
         }
      }
      return false;
   }
   public void removeDependency(final Dependency dep)
   {
      MavenCoreFacet maven = .getFacet(MavenCoreFacet.class);
      Model pom = maven.getPOM();
      List<Dependencydependencies = MavenDependencyAdapter.fromMavenList(pom.getDependencies());
      List<DependencytoBeRemoved = new ArrayList<Dependency>();
      for (Dependency dependency : dependencies)
      {
         if (DependencyBuilder.areEquivalent(dependencydep))
         {
            toBeRemoved.add(dependency);
         }
      }
      dependencies.removeAll(toBeRemoved);
      pom.setDependencies(MavenDependencyAdapter.toMavenList(dependencies));
      maven.setPOM(pom);
      .enqueue(new RemovedDependencies(toBeRemoved));
   }
   {
      MavenCoreFacet maven = .getFacet(MavenCoreFacet.class);
      Model pom = maven.getPOM();
      List<Dependencydependencies = MavenDependencyAdapter.fromMavenList(pom.getDependencies());
      List<Dependencyresult = new ArrayList<Dependency>();
      for (Dependency dependency : dependencies) {
         result.add(resolveProperties(dependency));
      }
      return result;
   }
   public Dependency getDependency(final Dependency dep)
   {
      MavenCoreFacet maven = .getFacet(MavenCoreFacet.class);
      List<Dependencydependencies = MavenDependencyAdapter.fromMavenList(maven.getPartialProjectBuildingResult()
               .getProject().getDependencies());
      for (Dependency dependency : dependencies)
      {
         if (DependencyBuilder.areEquivalent(dependencydep))
         {
            return resolveProperties(dependency);
         }
      }
      return null;
   }
   public boolean hasEffectiveDependency(final Dependency dependency)
   {
      return getEffectiveDependency(dependency) != null;
   }
   public Dependency getEffectiveDependency(final Dependency manDep)
   {
      for (Dependency dependency : getEffectiveDependencies())
      {
         if (DependencyBuilder.areEquivalent(dependencymanDep))
         {
            return resolveProperties(dependency);
         }
      }
      return null;
   }
   {
      MavenCoreFacet maven = .getFacet(MavenCoreFacet.class);
      List<Dependencydeps = MavenDependencyAdapter.fromAetherList(maven.getFullProjectBuildingResult()
               .getDependencyResolutionResult().getDependencies());
      List<Dependencyresult = new ArrayList<Dependency>();
      for (Dependency dependency : deps) {
         result.add(resolveProperties(dependency));
      }
      return result;
   }
   public void addManagedDependency(final Dependency manDep)
   {
      if (!hasEffectiveManagedDependency(manDep))
      {
         MavenCoreFacet maven = .getFacet(MavenCoreFacet.class);
         Model pom = maven.getPOM();
         DependencyManagement depMan = pom.getDependencyManagement();
         depMan = depMan != null ? depMan : new DependencyManagement();
         List<DependencymanagedDependencies = MavenDependencyAdapter.fromMavenList(depMan.getDependencies());
         managedDependencies.add(manDep);
         depMan.setDependencies(MavenDependencyAdapter.toMavenList(managedDependencies));
         pom.setDependencyManagement(depMan);
         maven.setPOM(pom);
      }
   }
   public boolean hasEffectiveManagedDependency(final Dependency manDep)
   {
      return (getEffectiveManagedDependency(manDep) != null);
   }
   {
      MavenCoreFacet maven = .getFacet(MavenCoreFacet.class);
      List<DependencymanagedDependencies = (depMan != null ? MavenDependencyAdapter.fromMavenList(depMan
               .getDependencies()) : new ArrayList<Dependency>());
      for (Dependency managedDependency : managedDependencies)
      {
         if (DependencyBuilder.areEquivalent(managedDependencymanDep))
         {
            return resolveProperties(managedDependency);
         }
      }
      return null;
   }
   public boolean hasManagedDependency(final Dependency managedDependency)
   {
      MavenCoreFacet maven = .getFacet(MavenCoreFacet.class);
      Model pom = maven.getPOM();
      DependencyManagement depMan = pom.getDependencyManagement();
      List<DependencymanagedDependencies = depMan != null ? MavenDependencyAdapter.fromMavenList(depMan
               .getDependencies()) : new ArrayList<Dependency>();
      for (Dependency manDep : managedDependencies)
      {
         if (DependencyBuilder.areEquivalent(managedDependencymanDep))
         {
            return true;
         }
      }
      return false;
   }
   public void removeManagedDependency(final Dependency manDep)
   {
      MavenCoreFacet maven = .getFacet(MavenCoreFacet.class);
      Model pom = maven.getPOM();
      DependencyManagement depMan = pom.getDependencyManagement();
      depMan = depMan != null ? depMan : new DependencyManagement();
      List<DependencymanagedDependencies = MavenDependencyAdapter.fromMavenList(depMan.getDependencies());
      List<DependencytoBeRemoved = new ArrayList<Dependency>();
      for (Dependency managedDependency : managedDependencies)
      {
         if (DependencyBuilder.areEquivalent(managedDependencymanDep))
         {
            toBeRemoved.add(managedDependency);
         }
      }
      managedDependencies.removeAll(toBeRemoved);
      depMan.setDependencies(MavenDependencyAdapter.toMavenList(managedDependencies));
      pom.setDependencyManagement(depMan);
      maven.setPOM(pom);
   }
   {
      MavenCoreFacet maven = .getFacet(MavenCoreFacet.class);
      Model pom = maven.getPOM();
      DependencyManagement depMan = pom.getDependencyManagement();
      List<DependencymanagedDependencies = null;
      if (depMan != null)
         managedDependencies = MavenDependencyAdapter.fromMavenList(depMan.getDependencies());
      else
         managedDependencies = new ArrayList<Dependency>();
      List<Dependencyresult = new ArrayList<Dependency>();
      for (Dependency dependency : managedDependencies) {
         managedDependencies.add(resolveProperties(dependency));
      }
      return result;
   }
   public Dependency getManagedDependency(final Dependency manDep)
   {
      MavenCoreFacet maven = .getFacet(MavenCoreFacet.class);
      Model pom = maven.getPOM();
      DependencyManagement depMan = pom.getDependencyManagement();
      List<DependencymanagedDependencies = depMan != null ? MavenDependencyAdapter.fromMavenList(depMan
               .getDependencies()) : new ArrayList<Dependency>();
      for (Dependency managedDependency : managedDependencies)
      {
         if (DependencyBuilder.areEquivalent(managedDependencymanDep))
         {
            return resolveProperties(managedDependency);
         }
      }
      return null;
   }
   public Map<StringStringgetProperties()
   {
      MavenCoreFacet maven = .getFacet(MavenCoreFacet.class);
      Model pom = maven.getPOM();
      Properties properties = pom.getProperties();
      Map<StringStringresult = new HashMap<StringString>();
      for (Entry<ObjectObjecto : properties.entrySet())
      {
         result.put((Stringo.getKey(), (Stringo.getValue());
      }
      return result;
   }
   public void setProperty(final String namefinal String value)
   {
      MavenCoreFacet maven = .getFacet(MavenCoreFacet.class);
      Model pom = maven.getPOM();
      Properties properties = pom.getProperties();
      properties.put(namevalue);
      maven.setPOM(pom);
   }
   public String getProperty(final String name)
   {
      MavenCoreFacet maven = .getFacet(MavenCoreFacet.class);
      Model pom = maven.getPOM();
      Properties properties = pom.getProperties();
      maven.setPOM(pom);
      return (Stringproperties.get(name);
   }
   public Dependency resolveProperties(final Dependency dependency)
   {
      MavenCoreFacet mvn = .getFacet(MavenCoreFacet.class);
      DependencyBuilder builder = DependencyBuilder.create(dependency);
      for (Entry<ObjectObjecte : properties.entrySet())
      {
         String key = "\\$\\{" + e.getKey().toString() + "\\}";
         Object value = e.getValue();
         if (dependency.getGroupId() != null)
            builder.setGroupId(dependency.getGroupId().replaceAll(keyvalue.toString()));
         if (dependency.getArtifactId() != null)
            builder.setArtifactId(dependency.getArtifactId().replaceAll(keyvalue.toString()));
         if (dependency.getVersion() != null)
            builder.setVersion(dependency.getVersion().replaceAll(keyvalue.toString()));
         if (dependency.getClassifier() != null)
            builder.setClassifier(dependency.getClassifier().replaceAll(keyvalue.toString()));
         if (dependency.getPackagingType() != null)
            builder.setPackagingType(dependency.getPackagingType().replaceAll(key,
                     value.toString()));
         if (dependency.getScopeType() != null)
            builder.setScopeType(dependency.getScopeType().replaceAll(keyvalue.toString()));
      }
      return builder;
   }
   public String removeProperty(final String name)
   {
      MavenCoreFacet maven = .getFacet(MavenCoreFacet.class);
      Model pom = maven.getPOM();
      Properties properties = pom.getProperties();
      String result = (Stringproperties.remove(name);
      maven.setPOM(pom);
      return result;
   }
   public List<DependencyresolveAvailableVersions(final String gavs)
   {
      return resolveAvailableVersions(DependencyBuilder.create(gavs));
   }
   {
      List<Dependencyversions = .resolveVersions(depgetRepositories());
      return versions;
   }
   public void addRepository(final String namefinal String url)
   {
      if (!hasRepository(url))
      {
         MavenCoreFacet maven = .getFacet(MavenCoreFacet.class);
         Model pom = maven.getPOM();
         Repository repo = new Repository();
         repo.setId(name);
         repo.setUrl(url);
         pom.getRepositories().add(repo);
         maven.setPOM(pom);
      }
   }
   public void addRepository(final KnownRepository repository)
   {
      addRepository(repository.name(), repository.getUrl());
   }
   {
      List<DependencyRepositoryresults = new ArrayList<DependencyRepository>();
      MavenCoreFacet maven = .getFacet(MavenCoreFacet.class);
      Model pom = maven.getPOM();
      List<Repositoryrepos = pom.getRepositories();
      for (Repository repo : repos)
      {
         results.add(new DependencyRepositoryImpl(repo.getId(), repo.getUrl()));
      }
      return Collections.unmodifiableList(results);
   }
   public boolean hasRepository(final String url)
   {
      if (url != null)
      {
         MavenCoreFacet maven = .getFacet(MavenCoreFacet.class);
         Model pom = maven.getPOM();
         List<Repositoryrepositories = pom.getRepositories();
         for (Repository repo : repositories)
         {
            if (repo.getUrl().trim().equals(url.trim()))
            {
               repositories.remove(repo);
               maven.setPOM(pom);
               return true;
            }
         }
      }
      return false;
   }
   public boolean hasRepository(final KnownRepository repository)
   {
      return hasRepository(repository.getUrl());
   }
   public DependencyRepository removeRepository(final String url)
   {
      if (url != null)
      {
         MavenCoreFacet maven = .getFacet(MavenCoreFacet.class);
         Model pom = maven.getPOM();
         List<Repositoryrepos = pom.getRepositories();
         for (Repository repo : repos)
         {
            if (repo.getUrl().equals(url.trim()))
            {
               repos.remove(repo);
               maven.setPOM(pom);
               return new DependencyRepositoryImpl(repo.getId(), repo.getUrl());
            }
         }
      }
      return null;
   }
   public List<DependencygetDependenciesInScopes(final ScopeType... scopes)
   {
      List<Dependencyresult = new ArrayList<Dependency>();
      List<Dependencydependencies = getDependencies();
      for (Dependency dependency : dependencies)
      {
         for (ScopeType scope : scopes)
         {
            if ((dependency.getScopeTypeEnum() == null) || dependency.getScopeTypeEnum().equals(scope))
            {
               dependency = resolveProperties(dependency);
               result.add(dependency);
               break;
            }
         }
      }
      return result;
   }
   {
      List<Dependencyresult = new ArrayList<Dependency>();
      List<Dependencydependencies = getEffectiveDependencies();
      for (Dependency dependency : dependencies)
      {
         for (ScopeType scope : scopes)
         {
            if ((dependency.getScopeTypeEnum() == null) || dependency.getScopeTypeEnum().equals(scope))
            {
               dependency = resolveProperties(dependency);
               result.add(dependency);
               break;
            }
         }
      }
      return result;
   }
New to GrepCode? Check out our FAQ X