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

com.marvelution.maven.components.migration.manager.phases.prepare.CheckProjectParentGavPhase 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.util.Properties;

import org.apache.maven.profiles.DefaultProfileManager;
import org.apache.maven.profiles.ProfileManager;
import org.apache.maven.project.DefaultProjectBuilderConfiguration;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.project.ProjectBuilderConfiguration;
import org.apache.maven.project.ProjectBuildingException;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.components.interactivity.PrompterException;
import org.codehaus.plexus.context.Context;
import org.codehaus.plexus.context.ContextException;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;

import com.marvelution.maven.components.migration.manager.MigrationResult;
import com.marvelution.maven.components.migration.manager.configuration.MigrationDescriptor;
import com.marvelution.maven.components.migration.manager.configuration.ParentProject;
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.AbstractPrompterMigrationPhase;
import com.marvelution.utils.maven.model.ModelUtils;

/**
 * {@link MigrationPhase} to check for a possible parent project
 * 
 * @author Mark Rekveld
 */
public class CheckProjectParentGavPhase extends AbstractPrompterMigrationPhase implements Contextualizable {
	
	/**
	 * Plexus Container
	 */
	private PlexusContainer container;
	
	/**
	 * {@link MavenProjectBuilder} component
	 */
	private MavenProjectBuilder projectBuilder;
	
	/**
	 * Sets the {@link MavenProjectBuilder} implementation
	 * 
	 * @param builder the {@link MavenProjectBuilder} implementation
	 */
	public void setProjectBuilder(MavenProjectBuilder builder) {
		projectBuilder = builder;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void contextualize(Context context) throws ContextException {
		this.container = ((PlexusContainer) context.get("plexus"));
	}

	/**
	 * {@inheritDoc}
	 */
	public MigrationResult execute(final MigrationDescriptor descriptor, final MigrationEnvironment environment)
					throws MigrationExecutionException, MigrationFailureException {
		final MigrationResult result = new MigrationResult();
		getLogger().info("Scanning for possible parent project...");
		final File parentPom = new File(descriptor.getWorkingDirectory().getParentFile(), ModelUtils.POMV4);
		if (parentPom.exists() && parentPom.canRead()) {
			getLogger().info("Found possible parent project " + ModelUtils.POMV4);
			String groupId = null, artifactId = null, version = null;
			try {
				final MavenProject parent = createMavenProject(descriptor, environment);
				groupId = parent.getGroupId();
				artifactId = parent.getArtifactId();
				version = parent.getVersion();
				if ("pom".equalsIgnoreCase(parent.getPackaging())) {
					try {
						if (promptForYesNoResponse("Configure " + groupId + ":" + artifactId + ":" + version
							+ " as parent project?", true)) {
							descriptor.setParentProject(new ParentProject(groupId, artifactId, version));
							getLogger()
								.info(
									"Configured " + groupId + ":" + artifactId + ":" + version
										+ " as parent project");
						}
					} catch (PrompterException e) {
						getLogger().error(e.getMessage(), e);
						throw new MigrationExecutionException(e.getMessage(), e);
					}
				} else {
					getLogger().error(
						"Unable to add as module to " + groupId + ":" + artifactId + ":" + version
							+ " as the parent project is not of packaging type 'pom'");
				}
			} catch (ProjectBuildingException e) {
				getLogger().error(e.getMessage(), e);
				throw new MigrationExecutionException(e.getMessage(), e);
			}
		} else {
			getLogger().info("No possible parent project found");
		}
		result.setResultCode(MigrationResult.SUCCESS);
		return result;
	}
	
	/**
	 * Create a {@link MavenProject} from a pom.xml file.
	 * 
	 * @param descriptor the {@link MigrationDescriptor} containing project related configuration
	 * @param environment the {@link MigrationEnvironment} containing environmental configuration
	 * @return the build {@link MavenProject}
	 * @throws ProjectBuildingException in case of build exceptions
	 */
	private MavenProject createMavenProject(MigrationDescriptor descriptor, MigrationEnvironment environment)
					throws ProjectBuildingException {
		final ProfileManager profileManager =
			new DefaultProfileManager(container, environment.getMavenSettings(), null);
		final ProjectBuilderConfiguration config = new DefaultProjectBuilderConfiguration();
	    config.setLocalRepository(environment.getLocalRepository());
	    config.setGlobalProfileManager(profileManager);
	    config.setExecutionProperties(new Properties());
	    config.setUserProperties(new Properties());
	    return projectBuilder.build(new File(descriptor.getWorkingDirectory().getParentFile(), ModelUtils.POMV4),
	    	config);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy