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

org.apache.maven.dotnet.CompileMojo Maven / Gradle / Ivy

Go to download

A plugin that provides general build and test facilities for .Net projects and solutions

There is a newer version: 1.2
Show newest version
/**
 * Maven and Sonar plugin for .Net
 * Copyright (C) 2010 Jose Chillan and Alexandre Victoor
 * mailto: [email protected] or [email protected]
 *
 * 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
 */

/*
 * Created on Apr 21, 2009
 */
package org.apache.maven.dotnet;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.maven.dotnet.commons.project.ArtifactType;
import org.apache.maven.dotnet.commons.project.VisualStudioProject;
import org.apache.maven.dotnet.commons.project.VisualStudioSolution;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;

/**
 * Builds a .Net project of solution using MSBuild
 * 
 * @goal compile
 * @phase compile
 * @description compiles a .Net project or solution
 * @author Jose CHILLAN Apr 9, 2009
 */
public class CompileMojo extends AbstractDotNetBuildMojo {
  /**
   * A flag to use to force the rebuild of the project or solution.
   * 
   * @parameter expression="${rebuild}" default-value="false"
   */
  private boolean rebuild;

  /**
   * A flag to disable the pre-build events during a compilation.
   * 
   * @parameter expression="${maven.dotnet.disable.prebuild.event}"
   *            default-value="false" alias="disablePreBuildEvent"
   */
  private boolean disablePreBuildEvent;

  /**
   * A flag to disable the post build events during a compilation.
   * 
   * @parameter expression="${maven.dotnet.disable.postbuild.event}"
   *            default-value="false" alias="disablePostBuildEvent"
   */
  private boolean disablePostBuildEvent;

  /**
   * A flag to disable the pre-link events during a compilation (to activate in
   * VS 2010)
   * 
   * @parameter expression="${maven.dotnet.disable.prelink.event}"
   *            default-value="false" alias="disablePreLinkEvent"
   */
  // private boolean disablePreLinkEvent;

  /**
   * Builds a {@link CompileMojo}.
   */
  public CompileMojo() {
  }

  /**
   * Launches the compiling of a visual studio project.
   * 
   * @param visualProject
   *          the project to compile
   */
  @Override
  public void executeProject(VisualStudioProject visualProject)
      throws MojoExecutionException, MojoFailureException {
    // Cannot compile a web project alone
    if (visualProject.getType() != ArtifactType.WEB) {
      File csprojFile = visualProject.getProjectFile();
      launchBuild(csprojFile);
    }
  }

  /**
   * Launches the compiling of a visual studio solution.
   * 
   * @param visualSolution
   *          the solution to compile
   */
  @Override
  public void executeSolution(VisualStudioSolution visualSolution)
      throws MojoExecutionException, MojoFailureException {
    File solutionFile = visualSolution.getSolutionFile();
    launchBuild(solutionFile);
  }

  /**
   * Launches the build of a project or solution
   * 
   * @param file
   * @throws MojoExecutionException
   * @throws MojoFailureException
   */
  public void launchBuild(File file) throws MojoExecutionException,
      MojoFailureException {
    File executable = getMsBuildCommand();
    if (!executable.exists()) {
      throw new MojoExecutionException(
          "Could not find the MSBuild executable for the version "
              + toolVersion
              + ". Please "
              + "ensure you have properly defined the properties 'dotnet.2.0.sdk.dir' or 'dotnet.3.5.sdk.dir'");
    }

    List configurations = getBuildConfigurations();
    for (String configuration : configurations) {

      List arguments = new ArrayList();
      arguments.add(toCommandPath(file));
      // Activates the build or rebuild
      if (rebuild) {
        arguments.add("/t:Rebuild");
      } else {
        arguments.add("/t:Build");
      }

      // Manages the disabled events
      if (disablePostBuildEvent) {
        // Disable the post build events if required
        arguments.add("/p:PostBuildEvent=\"\"");
        // arguments.add("/p:PostBuildEventUseInBuild=false");
      }
      if (disablePreBuildEvent) {
        // Disable the pre-build events if required
        // arguments.add("/p:PreBuildEventUseInBuild=false");
        arguments.add("/p:PreBuildEvent=\"\"");
      }

      // For VS 2010 uncomment those lines
      // if (disablePreLinkEvent)
      // {
      // Disable the pre-link events if required
      // arguments.add("/p:PreLinkEventUseInBuild=false");
      // }

      // Case of disabled debug symbols
      if (!debug) {
        arguments.add("/p:DebugSymbols=false");
      }

      // Adds the configuration
      arguments.add("/p:Configuration=" + configuration);

      Log log = getLog();
      log.info("Launching the build of " + file);
      log.debug(" - Tool Version  : " + toolVersion);
      log.debug(" - MsBuild exe   : " + executable);
      log.debug(" - Configuration : " + configuration
          + (rebuild ? " (force rebuild)" : ""));

      // We launch the compile command (the logs are put in debug because they
      // may be verbose)
      launchCommand(executable, arguments, "build", 0, true);
      log.info("Build of " + solutionName + " in configuration "
          + configuration + " terminated with SUCCESS!");
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy