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

com.marvelution.utils.maven.settings.SettingsUtils Maven / Gradle / Ivy

/*
 * Copyright 2008 Marvelution.com
 *
 * Licensed 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.utils.maven.settings;

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

import org.apache.maven.settings.RuntimeInfo;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.TrackableBase;
import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

/**
 * Utility class for Maven Settings
 * 
 * @author Mark Rekveld
 */
public final class SettingsUtils {

	/**
	 * Helper method to read Settings from a Settings file
	 * 
	 * @param settingsFile the {@link File} to read
	 * @return the parsed {@link Settings}
	 * @throws IOException in case the {@link File} cannot be read
	 * @throws XmlPullParserException in case of parser exceptions
	 */
	public static Settings read(File settingsFile) throws IOException, XmlPullParserException {
		if (settingsFile == null || !settingsFile.exists() || !settingsFile.isFile()) {
			throw new IllegalArgumentException("Invalid settings file provided.");
		}
		final SettingsXpp3Reader reader = new SettingsXpp3Reader();
		return reader.read(ReaderFactory.newXmlReader(settingsFile));
	}

	/**
	 * Read and Merge two Settings Files into one {@link Settings} instance
	 * 
	 * @param dominate the dominate settings {@link File}
	 * @param recessive the recessive settings {@link File}
	 * @return the merged {@link Settings}
	 * @throws IOException in case of read exceptions
	 * @throws XmlPullParserException in case of XML Parse exceptions
	 */
	public static Settings readAndMerge(File dominate, File recessive) throws IOException, XmlPullParserException {
		final Settings dominateSettings = read(dominate);
		if (dominateSettings.getRuntimeInfo() == null) {
			dominateSettings.setRuntimeInfo(new RuntimeInfo(dominateSettings));
		}
		final Settings recesiveSettings = read(recessive);
		org.apache.maven.settings.SettingsUtils.merge(dominateSettings, recesiveSettings, TrackableBase.GLOBAL_LEVEL);
		return dominateSettings;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy