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

com.marvelution.maven.components.migration.manager.phases.AbstractParentPomPhase 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;

import java.io.File;
import java.io.IOException;

import org.apache.maven.model.Model;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

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.utils.maven.model.ModelUtils;

/**
 * Abstract {@link MigrationPhase} for Parent POM operations
 * 
 * @author Mark Rekveld
 */
public abstract class AbstractParentPomPhase extends AbstractMigrationPhase {

	/**
	 * {@inheritDoc}
	 */
	public final MigrationResult execute(final MigrationDescriptor descriptor, final MigrationEnvironment environment)
					throws MigrationExecutionException, MigrationFailureException {
		final MigrationResult result = new MigrationResult();
		if (descriptor.getParentProject() != null) {
			final File parentPomFile = new File(descriptor.getWorkingDirectory().getParentFile(), ModelUtils.POMV4);
			if (parentPomFile != null && parentPomFile.exists() && parentPomFile.isFile()) {
				try {
					final Model parentModel = ModelUtils.read(parentPomFile);
					if (isParentFileConfiguredParent(descriptor.getParentProject(), parentModel)) {
						processParentModel(descriptor, environment, parentModel);
						ModelUtils.write(parentModel, parentPomFile);
					} else {
						getLogger().info("Configured parent '" + descriptor.getParentProject().getManagementKey() + "'"
							+ " does not match the project configured in '" + parentPomFile.getName() + "'."
							+ " Skipping processing of project '" + parentModel.getId() + "'");
					}
				} catch (IOException e) {
					throw new MigrationExecutionException("Error updating Parent POM '" + parentPomFile.getName()
						+ "' modules List", e);
				} catch (XmlPullParserException e) {
					throw new MigrationExecutionException("Parent POM '" + parentPomFile.getName() + "' is invalid",
						e);
				}
			}
		}
		result.setResultCode(MigrationResult.SUCCESS);
		return result;
	}

	/**
	 * Process the Parent Project Model
	 * 
	 * @param descriptor the {@link MigrationDescriptor} configuration
	 * @param environment the {@link MigrationEnvironment} to use
	 * @param model the parent {@link Model} to process
	 * @throws MigrationExecutionException in case of processing exceptions
	 * @throws MigrationFailureException in case of processing failures
	 */
	protected abstract void processParentModel(final MigrationDescriptor descriptor,
					final MigrationEnvironment environment, final Model model) throws MigrationExecutionException,
					MigrationFailureException;

	/**
	 * Checks whether the configured {@link ParentProject} is the same as the supplied Parent {@link Model}
	 * 
	 * @param parent the configured {@link ParentProject}
	 * @param parentModel the {@link Model} of the parent project
	 * @return true if the parent Model groupId, artifact, version equals the configured parent groupId,
	 *         artifactId, version
	 */
	private boolean isParentFileConfiguredParent(ParentProject parent, Model parentModel) {
		return (parent.getGroupId().equals(parentModel.getGroupId())
			&& parent.getArtifactId().equals(parentModel.getArtifactId()) && parent.getVersion().equals(
			parentModel.getVersion()));
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy