com.alphacsp.maven.plugins.sonarj.SonarReportMojo.java Source code

Java tutorial

Introduction

Here is the source code for com.alphacsp.maven.plugins.sonarj.SonarReportMojo.java

Source

/**
 * AlphaCSP
 * Copyright 2004 AlphaCSP Israel Ltd.
 * User: freds
 * Date: Jun 18, 2006
 * Time: 4:30:07 PM
 */
package com.alphacsp.maven.plugins.sonarj;

import com.alphacsp.maven.plugins.common.injectable.MvnInjectableMojoSupport;
import com.hello2morrow.sonar.ui.console.SonarJAntTask;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.tools.plugin.extractor.anno.annotations.MojoGoal;
import org.apache.maven.tools.plugin.extractor.anno.annotations.MojoParameter;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.Path;

import java.io.File;

/**
 * Activate the SonarJ report
 */
@MojoGoal("report")
public class SonarReportMojo extends MvnInjectableMojoSupport {
    /**
     * The sonar home folder
     */
    @MojoParameter(description = "The location of the home installation folder of SonarJ", required = true)
    protected File sonarHome;

    /**
     * The license file
     */
    @MojoParameter(description = "The location of the license file", required = true)
    protected File license;

    /**
     * The report output directory
     */
    @MojoParameter(description = "The report output dir. By default ${project.reporting.outputDirectory}/sonarj")
    protected File report;

    /**
     * The xml file of the architecture
     */
    @MojoParameter(description = "The architecture description file", required = true)
    protected File architectureDescription;

    /**
     * The xml file of the architecture
     */
    @MojoParameter(description = "The workspace description file. Generated in ${project.build.directory}/sonarj/${artifactId}-workspace.xml")
    protected File workspaceDescription;
    public static final String SONARJ = "sonarj";

    public void execute() throws MojoExecutionException, MojoFailureException {

        if (report == null)
            report = new File(getProject().getReporting().getOutputDirectory(), SONARJ);

        if (!report.exists()) {
            report.mkdirs();
        }

        if (sonarHome == null) {
            throw new MojoExecutionException("Sonar Home is mandatory");
        }
        String sonarHomePath = sonarHome.getAbsolutePath();
        if (!sonarHome.exists() || !sonarHome.isDirectory()) {
            throw new MojoExecutionException(
                    "Sonar Home " + sonarHomePath + " does not exists or is not a directory");
        }

        if (license == null) {
            throw new MojoExecutionException("Providing a license file is mandatory");
        }
        String licensePath = license.getAbsolutePath();
        if (!license.exists() || !license.isFile()) {
            throw new MojoExecutionException("License file " + licensePath + " does not exists or is not a file");
        }

        if (architectureDescription == null) {
            throw new MojoExecutionException("Providing an architecture description file is mandatory");
        }
        String architectureDescriptionPath = architectureDescription.getAbsolutePath();
        if (!architectureDescription.exists() || !architectureDescription.isFile()) {
            throw new MojoExecutionException("Architecture description file " + architectureDescriptionPath
                    + " does not exists or is not a file");
        }

        if (workspaceDescription == null) {
            // TODO: generate it
            File sonarOutDir = new File(getProject().getBuild().getDirectory(), SONARJ);
            if (!sonarOutDir.exists()) {
                sonarOutDir.mkdirs();
            }
            workspaceDescription = new File(sonarOutDir.getAbsolutePath(),
                    getProject().getArtifactId() + "-workspace.xml");
        }

        Project antProject = getAntProject();
        SonarJAntTask sonarJTask = new SonarJAntTask();
        sonarJTask.setProject(antProject);
        sonarJTask.setLicense(new Path(antProject, licensePath));
        sonarJTask.setWorkspaceDescription(new Path(antProject, workspaceDescription.getAbsolutePath()));
        sonarJTask.setReport(new Path(antProject, report.getAbsolutePath()));
        sonarJTask.setArchitectureDescription(new Path(antProject, architectureDescriptionPath));

        sonarJTask.execute();

    }
}