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

com.marvelution.maven.components.migration.manager.phases.prepare.ResolveDependenciesPhase Maven / Gradle / Ivy

The newest version!
/*
 * 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.migration.manager.phases.prepare;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.maven.model.Dependency;
import org.codehaus.plexus.util.StringUtils;

import com.marvelution.maven.components.dependency.resolver.DependencyResolver;
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.migration.manager.MigrationResult;
import com.marvelution.maven.components.migration.manager.configuration.MigrationDescriptor;
import com.marvelution.maven.components.migration.manager.environment.MigrationEnvironment;
import com.marvelution.maven.components.migration.manager.exception.MigrationExecutionException;
import com.marvelution.maven.components.migration.manager.exception.MigrationFailureException;
import com.marvelution.maven.components.migration.manager.phases.AbstractMigrationPhase;
import com.marvelution.utils.io.DirectoryUtils;
import com.marvelution.utils.io.FileUtils;
import com.marvelution.utils.maven.model.DependencyUtils;

/**
 * {@link MigrationPhase} to resolve all the dependencies
 * 
 * @author Mark Rekveld
 */
public class ResolveDependenciesPhase extends AbstractMigrationPhase {

	/**
	 * {@link DependencyResolver} used to resolve jar {@link File} dependencies to a Maven Model {@link Dependency}
	 */
	private DependencyResolver dependencyResolver;

	/**
	 * Gets the {@link DependencyResolver}
	 * 
	 * @return the {@link DependencyResolver}
	 */
	public DependencyResolver getDependencyResolver() {
		return dependencyResolver;
	}

	/**
	 * Sets the {@link DependencyResolver}
	 * 
	 * @param dependencyResolver the {@link DependencyResolver} to set
	 */
	public void setDependencyResolver(DependencyResolver dependencyResolver) {
		this.dependencyResolver = dependencyResolver;
	}

	/**
	 * {@inheritDoc}
	 */
	public MigrationResult execute(final MigrationDescriptor descriptor, final MigrationEnvironment environment)
					throws MigrationExecutionException, MigrationFailureException {
		final MigrationResult result = new MigrationResult();
		if (StringUtils.isNotEmpty(descriptor.getLibraryDirectory())) {
			final Map mappedDependencies = mapLibrariesToDependencies(descriptor, environment);
			final File libdir = new File(descriptor.getWorkingDirectory(), descriptor.getLibraryDirectory());
			int systemDepCount = 0;
			for (final Iterator iter = mappedDependencies.entrySet().iterator(); iter.hasNext();) {
				final Entry entry = (Entry) iter.next();
				final Dependency dependency = (Dependency) entry.getValue();
				if (dependency != null && StringUtils.isNotEmpty(dependency.getSystemPath())) {
					// We have a SystemPath...
					// So the library could not be resolved and thus MUST stay on the file system.
					dependency.setSystemPath("${basedir}/" + libdir.getName() + "/" + entry.getKey().toString());
					getLogger().warn("Library '" + entry.getKey().toString()
							+ "' could not be deleted because it could not be resolved,"
							+ " and the dependency in configured to look for this file");
					systemDepCount++;
					continue;
				} else if (dependency != null) {
					try {
						FileUtils.forceDelete(new File(libdir, entry.getKey().toString()));
					} catch (IOException e) {
						getLogger().info("Failed to delete library '" + entry.getKey().toString()
							+ "'. Please delete it yourself");
					}
				}
			}
			if (libdir.list().length == 0 && systemDepCount == 0) {
				try {
					FileUtils.deleteDirectory(libdir);
				} catch (IOException e) {
					getLogger().info("Failed to delete library directory '" + descriptor.getLibraryDirectory()
							+ "'. Please delete it yourself");
				}
			} else if (systemDepCount == 0) {
				getLogger().info("Library directory '" + descriptor.getLibraryDirectory()
						+ "' is not empty; Please verify that all the libraries are resolved "
						+ "and then delete this directory");
			}
		} else {
			getLogger().info("No library directory specified. Skipping library dependency resolution.");
		}
		result.setResultCode(MigrationResult.SUCCESS);
		return result;
	}

	/**
	 * {@inheritDoc}
	 */
	public MigrationResult simulate(final MigrationDescriptor descriptor, final MigrationEnvironment environment)
					throws MigrationExecutionException, MigrationFailureException {
		final Map mappedDependencies = mapLibrariesToDependencies(descriptor, environment);
		if (!mappedDependencies.isEmpty()) {
			getLogger().info("Full run would delete " + mappedDependencies.size() + " resolved libraries");
		}
		return getMigrationResultSuccess();
	}

	/**
	 * Map Library Files to Dependencies
	 * 
	 * @param descriptor the configuration to use
	 * @param environment the environmental configuration to use, such as Maven Settings, Maven home, Java home, etc.
	 * @return {@link Map} with the library file names as keys and the dependencies as values
	 * @throws MigrationExecutionException in case of resolving exceptions
	 * @throws MigrationFailureException in case of resolving failures
	 */
	private Map mapLibrariesToDependencies(final MigrationDescriptor descriptor,
					final MigrationEnvironment environment) throws MigrationExecutionException,
					MigrationFailureException {
		if (StringUtils.isEmpty(descriptor.getLibraryDirectory())) {
			getLogger().info("No library directory specified. Skipping dependency resolution");
			return Collections.EMPTY_MAP;
		}
		final File libraryDirectory = new File(descriptor.getWorkingDirectory(), descriptor.getLibraryDirectory());
		final String[] libraryFiles = DirectoryUtils.scanDirectoryForFiles(libraryDirectory, new String[] {"**/**"});
		if (libraryFiles.length == 0) {
			getLogger().info("No libraries in '" + libraryDirectory.getName() + "' found to resolve");
			return Collections.EMPTY_MAP;
		}
		final Map mappedDependencies = new HashMap();
		final ResolveEnvironment resolveEnvironment = getResolverEnvironment(environment);
		for (int i = 0; i < libraryFiles.length; i++) {
			Dependency dependency = null;
			try {
				final File library = new File(libraryDirectory, libraryFiles[i]);
				dependency = getDependencyResolver().resolve(library, resolveEnvironment);
				descriptor.getResolvedDependencies().put(DependencyUtils.versionlessKey(dependency), dependency);
				if (dependency != null) {
					mappedDependencies.put(libraryFiles[i], dependency);
				}
			} catch (DependencyResolverExecutionException e) {
				throw new MigrationExecutionException(e.getMessage(), e);
			} catch (DependencyResolverFailureException e) {
				throw new MigrationFailureException(e.getMessage());
			}
		}
		return mappedDependencies;
	}

	/**
	 * Gets a {@link ResolveEnvironment} from a {@link MigrationEnvironment}
	 * 
	 * @param environment the {@link MigrationEnvironment}
	 * @return the {@link ResolveEnvironment}
	 */
	private ResolveEnvironment getResolverEnvironment(MigrationEnvironment environment) {
		final ResolveEnvironment resolveEnvironment = new ResolveEnvironment();
		resolveEnvironment.setInteractive(environment.isInteractive());
		resolveEnvironment.setLocalRepository(environment.getLocalRepository());
		resolveEnvironment.setRemoteRepositories(environment.getRemoteRepositories());
		resolveEnvironment.setProxySettings(environment.getMavenSettings().getActiveProxy());
		return resolveEnvironment;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy