All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.marvelution.maven.components.dependency.resolver.DefaultDependencyResolver Maven / Gradle / Ivy

/*
 * Licensed to Marvelution under one or more contributor license 
 * agreements.  See the NOTICE file distributed with this work 
 * for additional information regarding copyright ownership.
 * Marvelution licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.marvelution.maven.components.dependency.resolver;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.model.Dependency;
import org.codehaus.plexus.logging.AbstractLogEnabled;

import com.marvelution.maven.components.dependency.resolver.environment.ResolveEnvironment;
import com.marvelution.maven.components.dependency.resolver.exception.DependencyResolverExecutionException;
import com.marvelution.maven.components.dependency.resolver.exception.DependencyResolverFailureException;
import com.marvelution.maven.components.dependency.resolver.methods.ResolveMethod;
import com.marvelution.maven.components.dependency.resolver.methods.exception.ResolveMethodException;

/**
 * Default Dependency Resolver implementation.
 * 

* Note: If not using Plexus, the following must be set before executing any resolve method: *

    *
  • the {@link Logger} implementation using method {@link #enableLogging(org.codehaus.plexus.logging.Logger)}
  • *
  • the {@link ArtifactResolver} using method {@link #setArtifactResolver(ArtifactResolver)}
  • *
  • the {@link ArtifactFactory} using method {@link #setArtifactFactory(ArtifactFactory)}
  • *
  • the {@link List} resolver methods using method {@link #setResolveMethods(List)}
  • *
*

* * @author Mark Rekveld */ public class DefaultDependencyResolver extends AbstractLogEnabled implements DependencyResolver { /** * List of known {@link ResolveMethod} implementations */ private List resolveMethods; /** * Maven Artifact Resolver component */ private ArtifactResolver artifactResolver; /** * Maven Artifact Factory component */ private ArtifactFactory artifactFactory; /** * Sets the ResolverMethods to use * * @param resolveMethods the {@link List} of {@link ResolveMethod} implementations */ public void setResolveMethods(List resolveMethods) { this.resolveMethods = resolveMethods; } /** * Sets the {@link ArtifactResolver} to use * * @param artifactResolver the {@link ArtifactResolver} implementation */ public void setArtifactResolver(ArtifactResolver artifactResolver) { this.artifactResolver = artifactResolver; } /** * Sets the {@link ArtifactFactory} to use * * @param artifactFactory the {@link ArtifactFactory} implementation */ public void setArtifactFactory(ArtifactFactory artifactFactory) { this.artifactFactory = artifactFactory; } /** * {@inheritDoc} */ public Dependency resolve(final File library, final ResolveEnvironment environment) throws DependencyResolverExecutionException, DependencyResolverFailureException { if (environment.getArtifactResolver() == null) { environment.setArtifactResolver(artifactResolver); } if (environment.getArtifactFactory() == null) { environment.setArtifactFactory(artifactFactory); } Artifact artifact = null, lastKnownArtifact = null; final Iterator iter = resolveMethods.iterator(); while (iter.hasNext() && artifact == null) { final ResolveMethod method = (ResolveMethod) iter.next(); getLogger().debug( "Trying to resolve library '" + library.getName() + "' using ResolveMethod: " + method.getClass().getName()); try { artifact = method.resolve(library, environment); if (artifact != null) { lastKnownArtifact = artifact; environment.getArtifactResolver().resolve(artifact, environment.getRemoteRepositories(), environment.getLocalRepository()); getLogger().info("Resolved library '" + library.getName() + "' to '" + artifact.toString() + "'"); break; } } catch (ResolveMethodException e) { artifact = null; } catch (ArtifactResolutionException e) { artifact = null; } catch (ArtifactNotFoundException e) { artifact = null; } } if (artifact == null && lastKnownArtifact == null) { // Never found a possible Artifact throw new DependencyResolverFailureException("Failed to resolve library: " + library.getName()); } else if (lastKnownArtifact != null && artifact == null) { // Found a possible Artifact but could not be resolved. // Set System path on the Dependency and then return it final Dependency dependency = convertArtifactToDependency(lastKnownArtifact); dependency.setScope(Artifact.SCOPE_SYSTEM); try { dependency.setSystemPath(library.getCanonicalPath()); } catch (IOException e) { dependency.setSystemPath(library.getAbsolutePath()); } return dependency; } else { // Found a good resolvable Artifact... // Converting it to a Dependency return convertArtifactToDependency(artifact); } } /** * {@inheritDoc} */ public Dependency[] resolve(final File[] libraries, final ResolveEnvironment environment) throws DependencyResolverExecutionException, DependencyResolverFailureException { return resolve(libraries, environment); } /** * {@inheritDoc} */ public List resolve(final List libraries, final ResolveEnvironment environment) throws DependencyResolverExecutionException, DependencyResolverFailureException { final List dependencies = new ArrayList(); for (final Iterator iter = libraries.iterator(); iter.hasNext();) { dependencies.add(resolve((File) iter.next(), environment)); } return dependencies; } /** * Convert an {@link Artifact} into a {@link Dependency} * * @param artifact the {@link Artifact} to convert * @return the {@link Dependency} */ private Dependency convertArtifactToDependency(Artifact artifact) { final Dependency dependency = new Dependency(); dependency.setGroupId(artifact.getGroupId()); dependency.setArtifactId(artifact.getArtifactId()); dependency.setVersion(artifact.getVersion()); dependency.setType(artifact.getType()); dependency.setClassifier(artifact.getClassifier()); // Set the scope of all dependencies to COMPILE // MAYBE Find out the scope from the artifact itself?? dependency.setScope(Artifact.SCOPE_COMPILE); return dependency; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy