/*
 * Copyright (C) 2016 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.cloud.tools.maven;

import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.Xpp3Dom;

import java.io.File;
import java.nio.file.Path;

/**
 * Abstract Mojo from which all goals inherit.
 */
public abstract class CloudSdkMojo extends AbstractMojo {

  /**
   * Optional parameter to configure the location of the Google Cloud SDK.
   */
  @Parameter(property = "cloudSdkPath", required = false)
  private File cloudSdkPath;

  @Parameter(defaultValue = "${plugin}", readonly = true)
  private PluginDescriptor pluginDescriptor;

  @Parameter(defaultValue = "${project}", readonly = true)
  protected MavenProject mavenProject;

  private AppEngineFactory factory = new CloudSdkAppEngineFactory(this);

  public String getArtifactId() {
    return pluginDescriptor.getArtifactId();
  }

  public String getArtifactVersion() {
    return pluginDescriptor.getVersion();
  }

  public Path getCloudSdkPath() {
    return cloudSdkPath != null ? cloudSdkPath.toPath() : null;
  }

  public AppEngineFactory getAppEngineFactory() {
    return factory;
  }

  /**
   * Determines the Java compiler target version by inspecting the project's maven-compiler-plugin
   * configuration.
   *
   * @return The Java compiler target version.
   */
  public String getCompileTargetVersion() {
    // maven-plugin-compiler default is 1.5
    String javaVersion = "1.5";
    if (mavenProject != null) {
      Plugin compilerPlugin = mavenProject
          .getPlugin("org.apache.maven.plugins:maven-compiler-plugin");
      if (compilerPlugin != null) {
        Xpp3Dom config = (Xpp3Dom) compilerPlugin.getConfiguration();
        if (config != null) {
          Xpp3Dom domVersion = config.getChild("target");
          if (domVersion != null) {
            javaVersion = domVersion.getValue();
          }
        }
      }
    }
    return javaVersion;
  }
}

@@ -2216,24 +2216,318 @@
  != null) %7B%0A
+      // check the maven.compiler.target property first%0A      String mavenCompilerTargetProperty = mavenProject.getProperties()%0A          .getProperty(%22maven.compiler.target%22);%0A      if (mavenCompilerTargetProperty != null) %7B%0A        javaVersion = mavenCompilerTargetProperty;%0A      %7D else %7B%0A  
       Plugin
@@ -2557,16 +2557,18 @@
 Project%0A
+  
         
@@ -2633,24 +2633,26 @@
 in%22);%0A      
+ 
+ 
 if (compiler
@@ -2661,32 +2661,34 @@
 ugin != null) %7B%0A
+  
         Xpp3Dom 
@@ -2737,24 +2737,26 @@
 guration();%0A
+  
         if (
@@ -2775,32 +2775,34 @@
 ll) %7B%0A          
+  
 Xpp3Dom domVersi
@@ -2839,24 +2839,26 @@
 ;%0A          
+  
 if (domVersi
@@ -2875,32 +2875,34 @@
 ) %7B%0A            
+ 
+ 
 javaVersion = do
@@ -2918,24 +2918,38 @@
 getValue();%0A
+            %7D%0A
           %7D%0A

Read maven.compiler.target property to identify Java version (#76)