
com.marvelution.maven.components.migration.manager.util.MigrationUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of maven-migration-manager Show documentation
Show all versions of maven-migration-manager Show documentation
Maven 2.x Plexus Migration Manager Component used by the maven-migrator-plugin
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.util;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.Map.Entry;
import org.apache.maven.model.Dependency;
import org.codehaus.plexus.util.StringUtils;
import com.marvelution.maven.components.migration.manager.configuration.BuildPlugin;
import com.marvelution.maven.components.migration.manager.configuration.MigrationDescriptor;
import com.marvelution.maven.components.migration.manager.configuration.ParentProject;
import com.marvelution.utils.ListUtils;
import com.marvelution.utils.maven.model.DependencyUtils;
/**
* Helper class for {@link MigrationDescriptor} transformations
*
* @author Mark Rekveld
*/
public final class MigrationUtils {
/**
* Transform {@link Properties} into {@link MigrationDescriptor}
*
* @param properties the {@link Properties} to transform
* @return {@link MigrationDescriptor}
*/
public static MigrationDescriptor copyPropertiesToMigrationDescriptor(Properties properties) {
final MigrationDescriptor config = new MigrationDescriptor();
config.setCompletedPhase(properties.getProperty("completed.phase"));
config.setSkipDirectoryMigration(properties.getProperty("skip.directory.migration"));
config.setProjectName(properties.getProperty("project.name"));
config.setProjectUrl(properties.getProperty("project.url"));
config.setGroupId(properties.getProperty("project.groupId"));
config.setArtifactId(properties.getProperty("project.artifactId"));
config.setVersion(properties.getProperty("project.version"));
config.setPackaging(properties.getProperty("project.packaging"));
if (StringUtils.isNotEmpty(properties.getProperty("project.parent"))) {
config.setParentProject(ParentProject.createParentProject(properties.getProperty("project.parent")));
}
config.setJdkRevision(properties.getProperty("jdk.revision"));
config.setSourceDirectory(properties.getProperty("source.directory"));
config.setResourcesDirectory(properties.getProperty("resources.directory"));
config.setTestSourceDirectory(properties.getProperty("test.source.directory"));
config.setTestResourcesDirectory(properties.getProperty("test.resources.directory"));
config.setWebappDirectory(properties.getProperty("webapp.directory"));
config.setEarAppDirectory(properties.getProperty("earapp.directory"));
config.setLibraryDirectory(properties.getProperty("library.directory"));
config.setPreparationGoals(properties.getProperty("preparation.goals"));
config.setPerformGoals(properties.getProperty("perform.goals"));
config.setAdditionalArguments(properties.getProperty("additional.arguments"));
for (final Iterator iter = properties.entrySet().iterator(); iter.hasNext();) {
final Entry currentEntry = (Entry) iter.next();
if (currentEntry.getKey().toString().startsWith("dependency.")) {
String key = currentEntry.getKey().toString();
final int startIndex = key.lastIndexOf("dependency.") + "dependency.".length();
if (key.endsWith(".version")) {
key = key.substring(startIndex, key.lastIndexOf('.'));
final Dependency dependency = DependencyUtils.versionlessKeyToDependency(key);
dependency.setVersion(currentEntry.getValue().toString());
dependency.setScope(properties.getProperty("dependency." + key + ".scope"));
dependency.setSystemPath(properties.getProperty("dependency." + key + ".systempath"));
config.getResolvedDependencies().put(key, dependency);
}
} else if (currentEntry.getKey().toString().startsWith("directories.")) {
final String key = currentEntry.getKey().toString();
final int startIndex = key.lastIndexOf("directories.") + "directories.".length();
config.getDirectories().put(key.substring(startIndex),
ListUtils.toList(currentEntry.getValue().toString()));
} else if (currentEntry.getKey().toString().startsWith("build.plugins.")) {
final String key = currentEntry.getKey().toString();
final int startIndex = key.lastIndexOf("build.plugins.") + "build.plugins.".length();
final BuildPlugin buildPlugin = new BuildPlugin(key.substring(startIndex));
buildPlugin.setConfiguration(currentEntry.getValue().toString());
config.getBuildPlugins().add(buildPlugin);
}
}
return config;
}
/**
* Transform {@link MigrationDescriptor} into {@link Properties}
*
* @param config the {@link MigrationDescriptor} to transform
* @return the {@link Properties}
*/
public static Properties copyMigrationDescriptorToProperties(MigrationDescriptor config) {
final Properties properties = new Properties();
properties.setProperty("completed.phase", config.getCompletedPhase());
properties.setProperty("project.name", config.getProjectName());
properties.setProperty("project.url", config.getProjectUrl());
if (config.isSkipDirectoryMigration()) {
properties.setProperty("skip.directory.migration", "true");
}
if (config.getGroupId() != null) {
properties.setProperty("project.groupId", config.getGroupId());
}
if (config.getArtifactId() != null) {
properties.setProperty("project.artifactId", config.getArtifactId());
}
if (config.getVersion() != null) {
properties.setProperty("project.version", config.getVersion());
}
if (config.getPackaging() != null) {
properties.setProperty("project.packaging", config.getPackaging());
}
if (config.getParentProject() != null) {
properties.setProperty("project.parent", config.getParentProject().getManagementKey());
}
if (config.getJdkRevision() != null) {
properties.setProperty("jdk.revision", config.getJdkRevision());
}
if (config.getSourceDirectory() != null) {
properties.setProperty("source.directory", config.getSourceDirectory());
}
if (config.getResourcesDirectory() != null) {
properties.setProperty("resources.directory", config.getResourcesDirectory());
}
if (config.getTestSourceDirectory() != null) {
properties.setProperty("test.source.directory", config.getTestSourceDirectory());
}
if (config.getTestResourcesDirectory() != null) {
properties.setProperty("test.resources.directory", config.getTestResourcesDirectory());
}
if (config.getWebappDirectory() != null) {
properties.setProperty("webapp.directory", config.getWebappDirectory());
}
if (config.getEarAppDirectory() != null) {
properties.setProperty("earapp.directory", config.getEarAppDirectory());
}
if (config.getLibraryDirectory() != null) {
properties.setProperty("library.directory", config.getLibraryDirectory());
}
if (config.getPreparationGoals() != null) {
properties.setProperty("preparation.goals", config.getPreparationGoals());
}
if (config.getPerformGoals() != null) {
properties.setProperty("perform.goals", config.getPerformGoals());
}
if (config.getAdditionalArguments() != null) {
properties.setProperty("additional.arguments", config.getAdditionalArguments());
}
for (final Iterator iter = config.getResolvedDependencies().entrySet().iterator(); iter.hasNext();) {
final Entry entry = (Entry) iter.next();
final Dependency dependency = (Dependency) entry.getValue();
properties.setProperty("dependency." + entry.getKey().toString() + ".version", dependency.getVersion());
if (StringUtils.isNotEmpty(dependency.getScope())) {
properties.setProperty("dependency." + entry.getKey().toString() + ".scope", dependency.getScope());
}
if (StringUtils.isNotEmpty(dependency.getSystemPath())) {
properties.setProperty("dependency." + entry.getKey().toString() + ".systempath", dependency
.getSystemPath());
}
}
for (final Iterator iter = config.getDirectories().entrySet().iterator(); iter.hasNext();) {
final Entry entry = (Entry) iter.next();
properties.setProperty("directories." + entry.getKey().toString(), ListUtils.toString((List) entry
.getValue()));
}
for (final Iterator iter = config.getBuildPlugins().iterator(); iter.hasNext();) {
final BuildPlugin buildPlugin = (BuildPlugin) iter.next();
properties.setProperty("build.plugins." + buildPlugin.getManagementKey(), buildPlugin
.getConfigurationAsString());
}
return properties;
}
/**
* Merge two {@link MigrationDescriptor}s into one
*
* @param mergeInto the {@link MigrationDescriptor} to merge into
* @param toBeMerged the {@link MigrationDescriptor} to be merged
* @return the merged {@link MigrationDescriptor}
*/
public static MigrationDescriptor mergeMigrationDescriptors(MigrationDescriptor mergeInto,
MigrationDescriptor toBeMerged) {
mergeInto.setSkipDirectoryMigration(mergeOverride(mergeInto.isSkipDirectoryMigration(), toBeMerged
.isSkipDirectoryMigration()));
mergeInto.setModelVersion(mergeDefault(mergeInto.getModelVersion(), toBeMerged.getModelVersion()));
mergeInto.setProjectName(mergeOverride(mergeInto.getProjectName(), toBeMerged.getProjectName()));
mergeInto.setProjectUrl(mergeOverride(mergeInto.getProjectUrl(), toBeMerged.getProjectUrl()));
mergeInto.setGroupId(mergeOverride(mergeInto.getGroupId(), toBeMerged.getGroupId()));
mergeInto.setArtifactId(mergeOverride(mergeInto.getArtifactId(), toBeMerged.getArtifactId()));
mergeInto.setVersion(mergeOverride(mergeInto.getVersion(), toBeMerged.getVersion()));
mergeInto.setPackaging(mergeOverride(mergeInto.getPackaging(), toBeMerged.getPackaging()));
mergeInto.setParentProject(mergeOverride(mergeInto.getParentProject(), toBeMerged.getParentProject()));
mergeInto.setJdkRevision(mergeOverride(mergeInto.getJdkRevision(), toBeMerged.getJdkRevision()));
mergeInto.setSourceDirectory(mergeOverride(mergeInto.getSourceDirectory(), toBeMerged.getSourceDirectory()));
mergeInto.setResourcesDirectory(mergeOverride(mergeInto.getResourcesDirectory(), toBeMerged
.getResourcesDirectory()));
mergeInto.setTestSourceDirectory(mergeOverride(mergeInto.getTestSourceDirectory(), toBeMerged
.getTestSourceDirectory()));
mergeInto.setTestResourcesDirectory(mergeOverride(mergeInto.getTestResourcesDirectory(), toBeMerged
.getTestResourcesDirectory()));
mergeInto.setWebappDirectory(mergeOverride(mergeInto.getWebappDirectory(), toBeMerged.getWebappDirectory()));
mergeInto.setEarAppDirectory(mergeOverride(mergeInto.getEarAppDirectory(), toBeMerged.getEarAppDirectory()));
mergeInto
.setLibraryDirectory(mergeOverride(mergeInto.getLibraryDirectory(), toBeMerged.getLibraryDirectory()));
mergeInto.setCompletedPhase(toBeMerged.getCompletedPhase());
mergeInto.setResolvedDependencies(mergeOverride(mergeInto.getResolvedDependencies(), toBeMerged
.getResolvedDependencies()));
mergeInto.setDirectories(mergeOverride(mergeInto.getDirectories(), toBeMerged.getDirectories()));
mergeInto.setBuildPlugins(mergeOverride(mergeInto.getBuildPlugins(), toBeMerged.getBuildPlugins()));
mergeInto
.setPreparationGoals(mergeOverride(mergeInto.getPreparationGoals(), toBeMerged.getPreparationGoals()));
mergeInto.setPerformGoals(mergeOverride(mergeInto.getPerformGoals(), toBeMerged.getPerformGoals()));
mergeInto.setAdditionalArguments(mergeOverride(mergeInto.getAdditionalArguments(), toBeMerged
.getAdditionalArguments()));
return mergeInto;
}
/**
* Helper method to override merge a {@link String} value
*
* @param thisValue the current value
* @param mergeValue the merge value
* @return if mergeValue != null
the mergeValue, otherwise thisValue
*/
private static String mergeOverride(String thisValue, String mergeValue) {
return mergeValue != null ? mergeValue : thisValue;
}
/**
* Helper method to override merge a {@link Boolean} value
*
* @param thisValue the current value
* @param mergeValue the merge value
* @return if mergeValue == true
the mergeValue, otherwise thisValue
*/
private static boolean mergeOverride(boolean thisValue, boolean mergeValue) {
return mergeValue ? mergeValue : thisValue;
}
/**
* Helper method to override merge a {@link Map} value
*
* @param thisValue the current value
* @param mergeValue the merge value
* @return the merged {@link Map}
*/
private static Map mergeOverride(Map thisValue, Map mergeValue) {
for (final Iterator iter = mergeValue.entrySet().iterator(); iter.hasNext();) {
final Entry entry = (Entry) iter.next();
if (thisValue.containsKey(entry.getKey())) {
thisValue.remove(entry.getKey());
}
thisValue.put(entry.getKey(), entry.getValue());
}
return thisValue;
}
/**
* Helper method to override merge a {@link Boolean} value
*
* @param thisValue the current {@link ParentProject}
* @param mergeValue the merge {@link ParentProject}
* @return if merge {@link ParentProject} is set the merge {@link ParentProject} otherwise the current
* {@link ParentProject}
*/
private static ParentProject mergeOverride(ParentProject thisValue, ParentProject mergeValue) {
return (mergeValue != null) ? mergeValue : thisValue;
}
/**
* Helper method to override merge a {@link Set} value
*
* @param thisValue the current value
* @param mergeValue the merge value
* @return the merged {@link Set}
*/
private static Set mergeOverride(Set thisValue, Set mergeValue) {
for (final Iterator iter = mergeValue.iterator(); iter.hasNext();) {
final Object item = iter.next();
if (thisValue.contains(item)) {
thisValue.remove(item);
}
thisValue.add(item);
}
return thisValue;
}
/**
* Helper method to merge a {@link String} value
*
* @param thisValue the current value
* @param mergeValue the merge value
* @return if thisValue != null
the thisValue, otherwise mergeValue
*/
private static String mergeDefault(String thisValue, String mergeValue) {
return thisValue != null ? thisValue : mergeValue;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy