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

org.sonar.maven.CollectMojo Maven / Gradle / Ivy

/*
 * 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.maven;

import org.sonar.commons.resources.Resource;
import org.sonar.commons.resources.Snapshot;
import org.sonar.commons.resources.ProjectLink;
import org.apache.maven.model.CiManagement;
import org.apache.maven.model.IssueManagement;
import org.apache.maven.model.Scm;
import org.apache.maven.plugin.MojoExecutionException;
import org.sonar.plugins.api.maven.MavenCollector;
import org.sonar.plugins.api.maven.ProjectContext;
import org.sonar.plugins.api.maven.model.MavenPom;

import java.util.List;

/**
 * @goal collect
 */
public class CollectMojo extends CoreMojo {
  public void doExecute() throws MojoExecutionException {
    ProjectContext context = (ProjectContext) getContainer().getComponent(ProjectContext.class);
    MavenPom pom = getMavenPom();
    Snapshot snapshot = ((ProjectContextImpl) context).init(pom);
    updateProject(snapshot.getResource(), pom.getMavenProject());

    List collectors = getContainer().getMavenCollectors();
    for (MavenCollector collector : collectors) {
      getLog().info("Starting " + collector.getClass());
      collector.collect(pom, context);
    }

    setSnapshotId(snapshot.getId(), mavenProject);
  }


  private void updateProject(Resource sonarProject, org.apache.maven.project.MavenProject pom) {
    updateProjectLink(ProjectLink.LINK_HOME_PAGE, pom.getUrl(), sonarProject);
    Scm scmConfig = pom.getScm();
    if (scmConfig == null) {
      scmConfig = new Scm();
    }
    updateProjectLink(ProjectLink.LINK_SCM_URL, scmConfig.getUrl(), sonarProject);
    updateProjectLink(ProjectLink.LINK_SCM_DEV_CONNECTION, scmConfig.getDeveloperConnection(), sonarProject);
    updateProjectLink(ProjectLink.LINK_SCM_RO_CONNECTION, scmConfig.getConnection(), sonarProject);

    CiManagement cimConfig = pom.getCiManagement();
    if (cimConfig == null) {
      cimConfig = new CiManagement();
    }
    updateProjectLink(ProjectLink.LINK_CONTINUOUS_INTEGRATION, cimConfig.getUrl(), sonarProject);

    IssueManagement imConfig = pom.getIssueManagement();
    if (imConfig == null) {
      imConfig = new IssueManagement();
    }
    updateProjectLink(ProjectLink.LINK_ISSUES_TRACKER, imConfig.getUrl(), sonarProject);
  }

  private void updateProjectLink(String linkType, String href, Resource sonarProject) {
    if (href != null && !"".equals(href)) {
      ProjectLink link = sonarProject.getProjectLinkByType(linkType);
      if (link == null) {
        link = new ProjectLink();
        link.setResource(sonarProject);
        link.setType(linkType);
        sonarProject.getProjectLinks().add(link);
      }
      link.setHref(href);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy