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

org.sonar.api.batch.maven.MavenPlugin Maven / Gradle / Ivy

There is a newer version: 5.1
Show newest version
/*
 * Sonar, open source software quality management tool.
 * Copyright (C) 2009 SonarSource SA
 * mailto:contact AT sonarsource DOT com
 *
 * Sonar is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * Sonar is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sonar; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */
package org.sonar.api.batch.maven;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.ReportPlugin;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.Xpp3Dom;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * @since 1.10
 */
public class MavenPlugin {

  private Plugin plugin;
  private Xpp3Dom configuration;

  public MavenPlugin(Plugin plugin) {
    this.plugin = plugin;
    this.configuration = (Xpp3Dom) plugin.getConfiguration();
    if (this.configuration == null) {
      configuration = new Xpp3Dom("configuration");
      plugin.setConfiguration(this.configuration);
    }
  }

  public MavenPlugin(String groupId, String artifactId, String version) {
    this.plugin = new Plugin();
    plugin.setGroupId(groupId);
    plugin.setArtifactId(artifactId);
    plugin.setVersion(version);
    configuration = new Xpp3Dom("configuration");
    plugin.setConfiguration(this.configuration);
  }

  public MavenPlugin setVersion(String version) {
    this.plugin.setVersion(version);
    return this;
  }

  public Plugin getPlugin() {
    return plugin;
  }

  public MavenPlugin addDependency(String groupId, String artifactId, String version, String dependencyType) {
    Dependency dependency = new Dependency();
    dependency.setGroupId(groupId);
    dependency.setArtifactId(artifactId);
    dependency.setVersion(version);
    dependency.setType(dependencyType);
    plugin.addDependency(dependency);
    return this;
  }

  public String getParameter(String key) {
    Xpp3Dom node = findNodeWith(key);
    return node == null ? null : node.getValue();
  }

  public String[] getParameters(String key) {
    String[] keyParts = StringUtils.split(key, "/");
    Xpp3Dom node = configuration;
    for (int i = 0; i < keyParts.length - 1; i++) {
      node = getOrCreateChild(node, keyParts[i]);
    }
    Xpp3Dom[] children = node.getChildren(keyParts[keyParts.length - 1]);
    String[] result = new String[children.length];
    for (int i = 0; i < children.length; i++) {
      result[i] = children[i].getValue();
    }
    return result;
  }

  public MavenPlugin setParameter(String key, String value) {
    checkKeyArgument(key);
    String[] keyParts = StringUtils.split(key, "/");
    Xpp3Dom node = configuration;
    for (String keyPart : keyParts) {
      node = getOrCreateChild(node, keyPart);
    }
    node.setValue(value);
    return this;
  }

  public void setParameter(String key, String value, boolean override) {
    if (getParameter(key) == null || override) {
      setParameter(key, value);
    }
  }

  public void removeParameters() {
    configuration = new Xpp3Dom("configuration");
    plugin.setConfiguration(this.configuration);
  }


  public MavenPlugin addParameter(String key, String value) {
    String[] keyParts = StringUtils.split(key, "/");
    Xpp3Dom node = configuration;
    for (int i = 0; i < keyParts.length - 1; i++) {
      node = getOrCreateChild(node, keyParts[i]);
    }
    Xpp3Dom leaf = new Xpp3Dom(keyParts[keyParts.length - 1]);
    leaf.setValue(value);
    node.addChild(leaf);
    return this;
  }

  private static Xpp3Dom getOrCreateChild(Xpp3Dom node, String key) {
    Xpp3Dom child = node.getChild(key);
    if (child == null) {
      child = new Xpp3Dom(key);
      node.addChild(child);
    }
    return child;
  }

  public void removeParameter(String key) {
    Xpp3Dom node = findNodeWith(key);
    if (node != null) {
      remove(node);
    }
  }

  private Xpp3Dom findNodeWith(String key) {
    checkKeyArgument(key);
    String[] keyParts = key.split("/");
    Xpp3Dom node = configuration;
    for (String keyPart : keyParts) {
      node = node.getChild(keyPart);
      if (node == null) {
        return null;
      }
    }
    return node;
  }

  private static void remove(Xpp3Dom node) {
    Xpp3Dom parent = node.getParent();
    for (int i = 0; i < parent.getChildCount(); i++) {
      Xpp3Dom child = parent.getChild(i);
      if (child.equals(node)) {
        parent.removeChild(i);
        break;
      }
    }
  }

  public boolean hasConfiguration() {
    return configuration.getChildCount() == 0;
  }

  private static void checkKeyArgument(String key) {
    if (key == null) {
      throw new IllegalArgumentException("Parameter 'key' should not be null.");
    }
  }

  public static MavenPlugin registerPlugin(MavenProject pom, String groupId, String artifactId, String version, boolean overrideVersion) {
    MavenPlugin plugin = getPlugin(pom, groupId, artifactId);
    if (plugin == null) {
      plugin = new MavenPlugin(groupId, artifactId, version);

    } else if (overrideVersion) {
      plugin.setVersion(version);
    }

    // remove from pom
    unregisterPlugin(pom, groupId, artifactId);

    // register
    pom.addPlugin(plugin.getPlugin());

    return plugin;
  }

  public static MavenPlugin getPlugin(MavenProject pom, String groupId, String artifactId) {
    if (pom == null) {
      return null;
    }
    // look for plugin in  section
    Plugin plugin = null;
    if (pom.getBuildPlugins() != null) {
      plugin = getPlugin(pom.getBuildPlugins(), groupId, artifactId);
    }

    // look for plugin in  section
    if (plugin == null && pom.getReportPlugins() != null) {
      plugin = getReportPlugin(pom.getReportPlugins(), groupId, artifactId);
    }

    // look for plugin in  section
    if (pom.getPluginManagement() != null) {
      Plugin pluginManagement = getPlugin(pom.getPluginManagement().getPlugins(), groupId, artifactId);
      if (plugin == null) {
        plugin = pluginManagement;

      } else if (pluginManagement != null) {
        if (pluginManagement.getConfiguration() != null) {
          if (plugin.getConfiguration() == null) {
            plugin.setConfiguration(pluginManagement.getConfiguration());
          } else {
            Xpp3Dom.mergeXpp3Dom((Xpp3Dom) plugin.getConfiguration(), (Xpp3Dom) pluginManagement.getConfiguration());
          }
        }
        if (plugin.getDependencies() == null && pluginManagement.getDependencies() != null) {
          plugin.setDependencies(pluginManagement.getDependencies());
        }
        if (plugin.getVersion() == null) {
          plugin.setVersion(pluginManagement.getVersion());
        }
      }
    }

    if (plugin != null) {
      return new MavenPlugin(plugin);
    }
    return null;
  }

  private static Plugin getPlugin(Collection plugins, String groupId, String artifactId) {
    if (plugins == null) {
      return null;
    }

    for (Plugin plugin : plugins) {
      if (MavenUtils.equals(plugin, groupId, artifactId)) {
        return plugin;
      }
    }
    return null;
  }

  private static Plugin getReportPlugin(Collection plugins, String groupId, String artifactId) {
    if (plugins == null) {
      return null;
    }

    for (ReportPlugin plugin : plugins) {
      if (MavenUtils.equals(plugin, groupId, artifactId)) {
        return cloneReportPluginToPlugin(plugin);
      }
    }
    return null;
  }

  private static Plugin cloneReportPluginToPlugin(ReportPlugin reportPlugin) {
    Plugin plugin = new Plugin();
    plugin.setGroupId(reportPlugin.getGroupId());
    plugin.setArtifactId(reportPlugin.getArtifactId());
    plugin.setVersion(reportPlugin.getVersion());
    plugin.setConfiguration(reportPlugin.getConfiguration());
    return plugin;
  }

  private static void unregisterPlugin(MavenProject pom, String groupId, String artifactId) {
    if (pom.getPluginManagement() != null && pom.getPluginManagement().getPlugins() != null) {
      unregisterPlugin(pom.getPluginManagement().getPlugins(), groupId, artifactId);
    }
    if (pom.getBuildPlugins() != null && pom.getBuildPlugins() != null) {
      unregisterPlugin(pom.getBuildPlugins(), groupId, artifactId);
    }
    if (pom.getReportPlugins() != null) {
      unregisterReportPlugin(pom.getReportPlugins(), groupId, artifactId);
    }
  }

  private static void unregisterPlugin(List plugins, String groupId, String artifactId) {
    for (Iterator iterator = plugins.iterator(); iterator.hasNext();) {
      Plugin p = iterator.next();
      if (MavenUtils.equals(p, groupId, artifactId)) {
        iterator.remove();
      }
    }
  }

  private static void unregisterReportPlugin(List plugins, String groupId, String artifactId) {
    for (Iterator iterator = plugins.iterator(); iterator.hasNext();) {
      ReportPlugin p = iterator.next();
      if (MavenUtils.equals(p, groupId, artifactId)) {
        iterator.remove();
      }
    }
  }


  @Override
  public String toString() {
    return new ToStringBuilder(this)
        .append("groupId", plugin.getGroupId())
        .append("artifactId", plugin.getArtifactId())
        .append("version", plugin.getVersion())
        .toString();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy