com.marvelution.utils.maven.model.PluginUtils 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.model;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.maven.model.Plugin;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.xml.Xpp3Dom;
/**
* Maven Model Plugin utility class
*
* @author Mark Rekveld
*/
public final class PluginUtils {
/**
* Create a {@link Plugin} for groupId:artifactId
*
* @param groupId the groupId of the plugin
* @param artifactId the artifactId of the plugin
* @param configuration {@link Map} of Key-Value paired configuration elements where the Key of the {@link Map}
* entry is the element name and the Value of the {@link Map} entry the value of the element
* @return the created {@link Plugin}
*/
public static Plugin createPlugin(String groupId, String artifactId, Map configuration) {
return createPlugin(groupId, artifactId, null, configuration);
}
/**
* Create a {@link Plugin} for groupId:artifactId
*
* @param groupId the groupId of the plugin
* @param artifactId the artifactId of the plugin
* @param version plugin version, may be null
* @param configuration {@link Map} of Key-Value paired configuration elements where the Key of the {@link Map}
* entry is the element name and the Value of the {@link Map} entry the value of the element
* @return the created {@link Plugin}
*/
public static Plugin createPlugin(String groupId, String artifactId, String version, Map configuration) {
final Plugin plugin = new Plugin();
plugin.setGroupId(groupId);
plugin.setArtifactId(artifactId);
if (!StringUtils.isEmpty(version)) {
plugin.setVersion(version);
}
final Xpp3Dom root = new Xpp3Dom("configuration");
for (final Iterator iter = configuration.entrySet().iterator(); iter.hasNext();) {
final Entry entry = (Entry) iter.next();
final Xpp3Dom child = new Xpp3Dom(entry.getKey().toString());
if (entry.getValue() instanceof String) {
child.setValue(entry.getValue().toString());
root.addChild(child);
} else if (entry.getValue() instanceof List) {
for (final Iterator i = ((List) entry.getValue()).iterator(); i.hasNext();) {
final Xpp3Dom param = new Xpp3Dom("param");
param.setValue((String) i.next());
child.addChild(param);
}
root.addChild(child);
} else {
throw new IllegalArgumentException("Unsupported value type found in configuration map");
}
}
plugin.setConfiguration(root);
return plugin;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy