This is an automated email from the ASF dual-hosted git repository.

rfscholte pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-compiler-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new 348c90d  [MCOMPILER-335] Update default source/target Change 
source/target defaults to 1.6, so you can compile with Java 9 & 10 without 
settings these values. Print a warning when target or release isn't set.
348c90d is described below

commit 348c90dbb2f46d0519bce16d55022cff3e1d8c6e
Author: rfscholte <[email protected]>
AuthorDate: Sun Jul 15 13:47:42 2018 +0200

    [MCOMPILER-335] Update default source/target
    Change source/target defaults to 1.6, so you can compile with Java 9 & 10 
without settings these values.
    Print a warning when target or release isn't set.
---
 .../plugin/compiler/AbstractCompilerMojo.java      | 101 ++++++++++++++++++++-
 src/site/apt/index.apt.vm                          |   6 +-
 .../plugin/compiler/CompilerMojoTestCase.java      |  24 +++++
 .../compiler-basic-sourcetarget/plugin-config.xml  |  39 ++++++++
 .../src/main/java/TestCompile0.java                |  29 ++++++
 5 files changed, 192 insertions(+), 7 deletions(-)

diff --git 
a/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java 
b/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java
index d15599f..e213f3b 100644
--- a/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java
+++ b/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java
@@ -21,6 +21,7 @@ package org.apache.maven.plugin.compiler;
 
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.nio.charset.Charset;
@@ -35,6 +36,7 @@ import java.util.Iterator;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Properties;
 import java.util.Set;
 
 import org.apache.maven.artifact.Artifact;
@@ -58,6 +60,8 @@ import 
org.apache.maven.shared.incremental.IncrementalBuildHelperRequest;
 import org.apache.maven.shared.utils.ReaderFactory;
 import org.apache.maven.shared.utils.StringUtils;
 import org.apache.maven.shared.utils.io.FileUtils;
+import org.apache.maven.shared.utils.logging.MessageBuilder;
+import org.apache.maven.shared.utils.logging.MessageUtils;
 import org.apache.maven.toolchain.Toolchain;
 import org.apache.maven.toolchain.ToolchainManager;
 import org.codehaus.plexus.compiler.Compiler;
@@ -76,6 +80,7 @@ import 
org.codehaus.plexus.compiler.util.scan.mapping.SingleTargetSourceMapping;
 import org.codehaus.plexus.compiler.util.scan.mapping.SourceMapping;
 import org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping;
 import org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor;
+import org.codehaus.plexus.languages.java.version.JavaVersion;
 
 /**
  * TODO: At least one step could be optimized, currently the plugin will do two
@@ -93,9 +98,9 @@ public abstract class AbstractCompilerMojo
 {
     protected static final String PS = System.getProperty( "path.separator" );
 
-    static final String DEFAULT_SOURCE = "1.5";
+    static final String DEFAULT_SOURCE = "1.6";
     
-    static final String DEFAULT_TARGET = "1.5";
+    static final String DEFAULT_TARGET = "1.6";
     
     // Used to compare with older targets
     static final String MODULE_INFO_TARGET = "1.9";
@@ -160,13 +165,17 @@ public abstract class AbstractCompilerMojo
     private boolean showWarnings;
 
     /**
-     * The -source argument for the Java compiler.
+     * <p>The -source argument for the Java compiler.</p>
+     * 
+     * <b>NOTE: </b>Since 3.8.0 the default value has changed from 1.5 to 1.6
      */
     @Parameter( property = "maven.compiler.source", defaultValue = 
DEFAULT_SOURCE )
     protected String source;
 
     /**
-     * The -target argument for the Java compiler.
+     * <p>The -target argument for the Java compiler.</p>
+     * 
+     * <b>NOTE: </b>Since 3.8.0 the default value has changed from 1.5 to 1.6
      */
     @Parameter( property = "maven.compiler.target", defaultValue = 
DEFAULT_TARGET )
     protected String target;
@@ -528,6 +537,8 @@ public abstract class AbstractCompilerMojo
         return project;
     }
 
+    private boolean targetOrReleaseSet;
+
     @Override
     public void execute()
         throws MojoExecutionException, CompilationFailureException
@@ -581,6 +592,18 @@ public abstract class AbstractCompilerMojo
             return;
         }
 
+        // Verify that target or release is set
+        if ( !targetOrReleaseSet )
+        {
+            MessageBuilder mb = MessageUtils.buffer().a( "No explicit value 
set for target or release! " )
+                            .a( "To ensure the same result even after 
upgrading this plugin, please add " ).newline()
+                            .newline();
+
+            writePlugin( mb );
+
+            getLog().warn( mb.toString() );
+        }
+
         // 
----------------------------------------------------------------------
         // Create the compiler configuration
         // 
----------------------------------------------------------------------
@@ -1669,4 +1692,74 @@ public abstract class AbstractCompilerMojo
                 + e.getLocalizedMessage(), e );
         }
     }
+
+    private void writePlugin( MessageBuilder mb )
+    {
+        mb.a( "    <plugin>" ).newline();
+        mb.a( "      <groupId>org.apache.maven.plugins</groupId>" ).newline();
+        mb.a( "      <artifactId>maven-compiler-plugin</artifactId>" 
).newline();
+        
+        String version = getMavenCompilerPluginVersion();
+        if ( version != null )
+        {
+            mb.a( "      <version>" ).a( version ).a( "</version>" ).newline();
+        }
+        writeConfig( mb );
+        
+        mb.a( "    </plugin>" ).newline();
+    }
+
+    private void writeConfig( MessageBuilder mb )
+    {
+        mb.a( "      <configuration>" ).newline();
+
+        if ( release != null )
+        {
+            mb.a( "        <release>" ).a( release ).a( "</release>" 
).newline();
+        }
+        else if ( JavaVersion.JAVA_VERSION.isAtLeast( "9" ) )
+        {
+            String rls = target.replaceAll( ".\\.", "" );
+            // when using Java9+, motivate to use release instead of 
source/target
+            mb.a( "        <release>" ).a( rls ).a( "</release>" ).newline();
+        }
+        else
+        {
+            mb.a( "        <source>" ).a( source ).a( "</source>" ).newline();
+            mb.a( "        <target>" ).a( target ).a( "</target>" ).newline();
+        }
+        mb.a( "      </configuration>" ).newline();
+    }
+
+    private String getMavenCompilerPluginVersion()
+    {
+        Properties pomProperties = new Properties();
+
+        try ( InputStream is = AbstractCompilerMojo.class
+            .getResourceAsStream( 
"/META-INF/maven/org.apache.maven.plugins/maven-compiler-plugin/pom.properties" 
) )
+        {
+            if ( is != null )
+            {
+                pomProperties.load( is );
+            }
+        }
+        catch ( IOException e )
+        {
+            // noop
+        }
+
+        return pomProperties.getProperty( "version" );
+    }
+
+    public void setTarget( String target )
+    {
+        this.target = target;
+        targetOrReleaseSet = true;
+    }
+
+    public void setRelease( String release )
+    {
+        this.release = release;
+        targetOrReleaseSet = true;
+    }
 }
diff --git a/src/site/apt/index.apt.vm b/src/site/apt/index.apt.vm
index dfea5d7..b6ffac9 100644
--- a/src/site/apt/index.apt.vm
+++ b/src/site/apt/index.apt.vm
@@ -32,9 +32,9 @@ ${project.name}
   default compiler is javax.tools.JavaCompiler (if you are using java 1.6) and 
is used to compile Java sources.
   If you want to force the plugin using  <<<javac>>>, you must configure the 
plugin option 
{{{./compile-mojo.html#forceJavacCompilerUse}<<<forceJavacCompilerUse>>>}}.
 
-  Also note that at present the default <<<source>>> setting is <<<1.5>>> and 
the default <<<target>>>
-  setting is <<<1.5>>>, independently of the JDK you run Maven with.
-  If you want to change these defaults, you should set <<<source>>> and 
<<<target>>>
+  Also note that at present the default <<<source>>> setting is <<<1.6>>> and 
the default <<<target>>>
+  setting is <<<1.6>>>, independently of the JDK you run Maven with.
+  You are highly encouraged to change these defaults by setting <<<source>>> 
and <<<target>>>
   as described in
   {{{./examples/set-compiler-source-and-target.html}Setting the -source and 
-target of the Java Compiler}}.
 
diff --git 
a/src/test/java/org/apache/maven/plugin/compiler/CompilerMojoTestCase.java 
b/src/test/java/org/apache/maven/plugin/compiler/CompilerMojoTestCase.java
index 446c015..8ae1dc6 100644
--- a/src/test/java/org/apache/maven/plugin/compiler/CompilerMojoTestCase.java
+++ b/src/test/java/org/apache/maven/plugin/compiler/CompilerMojoTestCase.java
@@ -19,7 +19,10 @@ package org.apache.maven.plugin.compiler;
  * under the License.
  */
 
+import static org.mockito.Matchers.startsWith;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 import java.io.File;
@@ -39,6 +42,7 @@ import 
org.apache.maven.plugin.compiler.stubs.CompilerManagerStub;
 import org.apache.maven.plugin.compiler.stubs.DebugEnabledLog;
 import org.apache.maven.plugin.descriptor.MojoDescriptor;
 import org.apache.maven.plugin.descriptor.PluginDescriptor;
+import org.apache.maven.plugin.logging.Log;
 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
 import org.apache.maven.plugin.testing.stubs.ArtifactStub;
 import org.apache.maven.project.MavenProject;
@@ -79,6 +83,10 @@ public class CompilerMojoTestCase
     {
         CompilerMojo compileMojo = getCompilerMojo( 
"target/test-classes/unit/compiler-basic-test/plugin-config.xml" );
         
+        Log log = mock( Log.class );
+        
+        compileMojo.setLog( log );
+        
         compileMojo.execute();
 
         File testClass = new File( compileMojo.getOutputDirectory(), 
"TestCompile0.class" );
@@ -95,9 +103,25 @@ public class CompilerMojoTestCase
                        projectArtifact.getFile() );
 
         testClass = new File( testCompileMojo.getOutputDirectory(), 
"TestCompile0Test.class" );
+        
+        verify( log ).warn( startsWith( "No explicit value set for target or 
release!" ) );
 
         assertTrue( testClass.exists() );
     }
+    
+    public void testCompilerBasicSourceTarget()
+                    throws Exception
+    {
+        CompilerMojo compileMojo = getCompilerMojo( 
"target/test-classes/unit/compiler-basic-sourcetarget/plugin-config.xml" );
+        
+        Log log = mock( Log.class );
+        
+        compileMojo.setLog( log );
+        
+        compileMojo.execute();
+        
+        verify( log, never() ).warn( startsWith( "No explicit value set for 
target or release!" ) );
+    }
 
     /**
      * tests the ability of the plugin to respond to empty source
diff --git 
a/src/test/resources/unit/compiler-basic-sourcetarget/plugin-config.xml 
b/src/test/resources/unit/compiler-basic-sourcetarget/plugin-config.xml
new file mode 100644
index 0000000..b4059e0
--- /dev/null
+++ b/src/test/resources/unit/compiler-basic-sourcetarget/plugin-config.xml
@@ -0,0 +1,39 @@
+<!--
+  ~ Licensed to the Apache Software Foundation (ASF) under one
+  ~ or more contributor license agreements.  See the NOTICE file
+  ~ distributed with this work for additional information
+  ~ regarding copyright ownership.  The ASF licenses this file
+  ~ to you 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.
+  -->
+
+<project>
+  <build>
+    <plugins>
+      <plugin>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <configuration>
+          <compileSourceRoots>
+            
<compileSourceRoot>${basedir}/target/test-classes/unit/compiler-basic-test/src/main/java</compileSourceRoot>
+          </compileSourceRoots>
+          <compilerId>javac</compilerId>
+          <debug>true</debug>
+          
<outputDirectory>${basedir}/target/test/unit/compiler-basic-test/target/classes</outputDirectory>
+          
<buildDirectory>${basedir}/target/test/unit/compiler-basic-test/target</buildDirectory>
+          <source>1.6</source>
+          <target>1.6</target>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git 
a/src/test/resources/unit/compiler-basic-sourcetarget/src/main/java/TestCompile0.java
 
b/src/test/resources/unit/compiler-basic-sourcetarget/src/main/java/TestCompile0.java
new file mode 100644
index 0000000..44c0359
--- /dev/null
+++ 
b/src/test/resources/unit/compiler-basic-sourcetarget/src/main/java/TestCompile0.java
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+public class TestCompile0
+{
+
+    public TestCompile0()
+    {
+
+        System.out.println( "Woo Hoo!" );
+    }
+
+}
\ No newline at end of file

Reply via email to