private File getPluginDirAndInstallIfNecessary(final Artifact plugin)
      throws MojoExecutionException {
    boolean targetDirOverridden = true;
    File targetDir = getPluginTargetDirOverride(plugin);
    if (targetDir == null) {
      targetDirOverridden = false;
      targetDir = getPluginTargetDirCentral(plugin);
    }

    String pluginName = getPluginName(plugin);
    final String pluginVersion = plugin.getVersion();
    boolean snapshot = pluginVersion.endsWith("-SNAPSHOT");

    if (snapshot
        && plugin
            .getFile()
            .getAbsolutePath()
            .endsWith("target" + File.separator + "classes")) { // multi module build

      targetDir = plugin.getFile().getParentFile().getParentFile();
      getLog()
          .info(
              String.format(
                  "Plugin %s:%s is coming from a multi-module dependency (%s)",
                  pluginName, pluginVersion, targetDir.getAbsolutePath()));

    } else if ((!snapshot && !targetDir.exists()) || (snapshot && !targetDirOverridden)) {
      // Unpack the plugin if it hasn't already been or if its a SNAPSHOT and not overridden by
      // -Dflag

      // Ideally we need to now do two things (a) see if we are running JDK7
      // and (b) determine if -Dplugin.groupId.artifactId has been set - if this is so, we want to
      // do a Files.createLink
      // to the directory specified by  the -D flag. We should probably also check if the targetDir
      // is a link and
      // the -Dflag hasn't been set, in which case we'd want to remove the link and install the
      // plugin (and let the user
      // know this has happened.
      // We wouldn't actually want this to be allowed when doing a release however.... So people
      // should make sure they don't
      // specify them, they they'll be installed.

      getLog()
          .info(
              String.format(
                  "Installing Plugin %s:%s into (%s)",
                  pluginName, pluginVersion, targetDir.getAbsolutePath()));
      targetDir.mkdirs();

      final ZipUnArchiver unzipper = new ZipUnArchiver();
      unzipper.enableLogging(new ConsoleLogger(Logger.LEVEL_ERROR, "zip-unarchiver"));
      unzipper.setSourceFile(plugin.getFile());
      unzipper.setDestDirectory(targetDir);
      unzipper.setOverwrite(true);
      try {
        unzipper.extract();
      } catch (ArchiverException e) {
        throw new MojoExecutionException("Unable to extract zip", e);
      }
      try {
        File inputPom =
            new File(plugin.getFile().getParentFile(), pluginName + "-" + pluginVersion + ".pom");
        File outputPom = new File(targetDir, "pom.xml");
        getLog()
            .info(
                String.format(
                    "copying %s to %s", inputPom.getAbsolutePath(), outputPom.getAbsolutePath()));
        FileReader fr = new FileReader(inputPom);
        FileWriter fw = new FileWriter(outputPom);
        IOUtils.copy(fr, fw);
        fw.flush();
        fw.close();
        fr.close();
      } catch (IOException e) {
        throw new MojoExecutionException("Unable to copy pom.xml file");
      }
    } else {
      getLog()
          .info(
              String.format(
                  "Plugin %s:%s already installed (%s)",
                  pluginName, pluginVersion, targetDir.getAbsolutePath()));
    }

    return targetDir;
  }