You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by kh...@apache.org on 2018/03/30 12:25:31 UTC

[maven-help-plugin] 01/01: [MPH-144] - mvn help:evaluate -Dexpression=project.groupId Can't be used in scripts.

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

khmarbaise pushed a commit to branch MPH-144
in repository https://gitbox.apache.org/repos/asf/maven-help-plugin.git

commit acd20b43680bf9a8d3a3c22934552334884608bb
Author: Karl Heinz Marbaise <kh...@apache.org>
AuthorDate: Thu Mar 29 20:43:23 2018 +0200

    [MPH-144] - mvn help:evaluate -Dexpression=project.groupId Can't be used in scripts.
---
 .../apache/maven/plugins/help/EvaluateMojo.java    | 55 +++++++++++-----
 .../maven/plugins/help/EvaluateMojoTest.java       | 76 ++++++++++++++++++++--
 .../unit/evaluate/plugin-config-quiet-stdout.xml   | 39 +++++++++++
 3 files changed, 147 insertions(+), 23 deletions(-)

diff --git a/src/main/java/org/apache/maven/plugins/help/EvaluateMojo.java b/src/main/java/org/apache/maven/plugins/help/EvaluateMojo.java
index 859d654..5076d7f 100644
--- a/src/main/java/org/apache/maven/plugins/help/EvaluateMojo.java
+++ b/src/main/java/org/apache/maven/plugins/help/EvaluateMojo.java
@@ -93,12 +93,11 @@ public class EvaluateMojo
     // ----------------------------------------------------------------------
     // Mojo parameters
     // ----------------------------------------------------------------------
-    
+
     // we need to hide the 'output' defined in AbstractHelpMojo to have a correct "since".
     /**
-     * Optional parameter to write the output of this help in a given file, instead of writing to the console.
-     * This parameter will be ignored if no <code>expression</code> is specified.
-     * <br/>
+     * Optional parameter to write the output of this help in a given file, instead of writing to the console. This
+     * parameter will be ignored if no <code>expression</code> is specified. <br/>
      * <b>Note</b>: Could be a relative path.
      * 
      * @since 3.0.0
@@ -107,10 +106,26 @@ public class EvaluateMojo
     private File output;
 
     /**
-     * An artifact for evaluating Maven expressions.
-     * <br/>
-     * <b>Note</b>: Should respect the Maven format, i.e. <code>groupId:artifactId[:version]</code>. The
-     * latest version of the artifact will be used when no version is specified.
+     * This options gives the option to output informations in cases where the output has been suppressed by using
+     * <code>-q</code> (quiet option) in Maven. This is useful if you like to use
+     * <code>maven-help-plugin:evaluate</code> in a script call (for example in bash) like this:
+     * 
+     * <pre>
+     * RESULT=$(mvn help:evaluate -Dexpression=project.version -q -DforceOutput)
+     * echo $RESULT
+     * </pre>
+     * 
+     * This will only printout the version of the project on <code>stdout</code>.
+     * 
+     * @since 3.0.2
+     */
+    @Parameter( property = "forceOutput", defaultValue = "false" )
+    private boolean forceOutput;
+
+    /**
+     * An artifact for evaluating Maven expressions. <br/>
+     * <b>Note</b>: Should respect the Maven format, i.e. <code>groupId:artifactId[:version]</code>. The latest version
+     * of the artifact will be used when no version is specified.
      */
     @Parameter( property = "artifact" )
     private String artifact;
@@ -155,7 +170,7 @@ public class EvaluateMojo
         {
 
             getLog().error( "Maven is configured to NOT interact with the user for input. "
-                            + "This Mojo requires that 'interactiveMode' in your settings file is flag to 'true'." );
+                + "This Mojo requires that 'interactiveMode' in your settings file is flag to 'true'." );
             return;
         }
 
@@ -379,7 +394,17 @@ public class EvaluateMojo
         }
         else
         {
-            getLog().info( LS + response.toString() );
+            if ( getLog().isInfoEnabled() )
+            {
+                getLog().info( LS + response.toString() );
+            }
+            else
+            {
+                if ( forceOutput )
+                {
+                    System.out.print( response.toString() );
+                }
+            }
         }
     }
 
@@ -500,8 +525,8 @@ public class EvaluateMojo
         try
         {
             jarStream = new JarInputStream( new FileInputStream( jarFile ) );
-            for ( JarEntry jarEntry = jarStream.getNextJarEntry(); jarEntry != null;
-                  jarEntry = jarStream.getNextJarEntry() )
+            for ( JarEntry jarEntry = jarStream.getNextJarEntry(); jarEntry != null; jarEntry =
+                jarStream.getNextJarEntry() )
             {
                 if ( jarEntry.getName().toLowerCase( Locale.ENGLISH ).endsWith( ".class" ) )
                 {
@@ -649,10 +674,8 @@ public class EvaluateMojo
             IOUtil.close( resourceAsStream );
         }
 
-        String artifactString =
-            properties.getProperty( "groupId", "unknown" ) + ":"
-                + properties.getProperty( "artifactId", "unknown" ) + ":"
-                + properties.getProperty( "version", "unknown" );
+        String artifactString = properties.getProperty( "groupId", "unknown" ) + ":"
+            + properties.getProperty( "artifactId", "unknown" ) + ":" + properties.getProperty( "version", "unknown" );
 
         return getMavenProject( artifactString );
     }
diff --git a/src/test/java/org/apache/maven/plugins/help/EvaluateMojoTest.java b/src/test/java/org/apache/maven/plugins/help/EvaluateMojoTest.java
index 7bb29d1..543981c 100644
--- a/src/test/java/org/apache/maven/plugins/help/EvaluateMojoTest.java
+++ b/src/test/java/org/apache/maven/plugins/help/EvaluateMojoTest.java
@@ -19,13 +19,15 @@ package org.apache.maven.plugins.help;
  * under the License.
  */
 
-import static org.mockito.Mockito.anyString;
+import static org.mockito.Matchers.anyString;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
+import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.PrintStream;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -60,6 +62,7 @@ public class EvaluateMojoTest
 
     /**
      * Tests evaluation of an expression in interactive mode with a mock input handler.
+     * 
      * @throws Exception in case of errors.
      */
     public void testEvaluateWithoutExpression()
@@ -78,7 +81,7 @@ public class EvaluateMojoTest
         setUpMojo( mojo, inputHandler, expressionEvaluator );
 
         mojo.execute();
-        
+
         String ls = System.getProperty( "line.separator" );
 
         assertTrue( interceptingLogger.infoLogs.contains( ls + "My result" ) );
@@ -89,6 +92,7 @@ public class EvaluateMojoTest
 
     /**
      * Tests evaluation of an expression in interactive mode with a mock input handler, when "output" is set.
+     * 
      * @throws Exception in case of errors.
      */
     public void testEvaluateWithoutExpressionWithOutput()
@@ -99,7 +103,7 @@ public class EvaluateMojoTest
         EvaluateMojo mojo = (EvaluateMojo) lookupMojo( "evaluate", testPom );
 
         InputHandler inputHandler = mock( InputHandler.class );
-        when( inputHandler.readLine() ).thenReturn( "${project.artifactId}", "0" );
+        when( inputHandler.readLine() ).thenReturn( "${project.artifactId}", "0" );        
 
         ExpressionEvaluator expressionEvaluator = mock( PluginParameterExpressionEvaluator.class );
         when( expressionEvaluator.evaluate( anyString() ) ).thenReturn( "My result" );
@@ -107,7 +111,7 @@ public class EvaluateMojoTest
         setUpMojo( mojo, inputHandler, expressionEvaluator );
 
         mojo.execute();
-        
+
         String ls = System.getProperty( "line.separator" );
 
         assertTrue( interceptingLogger.infoLogs.contains( ls + "My result" ) );
@@ -116,6 +120,47 @@ public class EvaluateMojoTest
         verify( inputHandler, times( 2 ) ).readLine();
     }
 
+    /**
+     * This test will check that only the <code>project.groupId</code> is printed to
+     * stdout nothing else.
+     * 
+     * @throws Exception in case of errors.
+     * @see <a href="https://issues.apache.org/jira/browse/MPH-144">MPH-144</a>
+     */
+    public void testEvaluateQuiteModeWithOutputOnStdout()
+        throws Exception
+    {
+        File testPom = new File( getBasedir(), "target/test-classes/unit/evaluate/plugin-config-quiet-stdout.xml" );
+
+        EvaluateMojo mojo = (EvaluateMojo) lookupMojo( "evaluate", testPom );
+
+        ExpressionEvaluator expressionEvaluator = mock( PluginParameterExpressionEvaluator.class );
+        when( expressionEvaluator.evaluate( anyString() ) ).thenReturn( "org.apache.maven.its.help" );
+
+        // Quiet mode given on command line.(simulation)
+        interceptingLogger.setInfoEnabled( false );
+
+        setUpMojo( mojo, null, expressionEvaluator );
+
+        PrintStream saveOut = System.out;
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        System.setOut( new PrintStream( baos ) );
+
+        try
+        {
+            mojo.execute();
+        }
+        finally
+        {
+            System.setOut( saveOut );
+            baos.close();
+        }
+
+        String stdResult = new String( baos.toByteArray() );
+        assertTrue( stdResult.equals( "org.apache.maven.its.help" ) );
+        assertTrue( interceptingLogger.warnLogs.isEmpty() );
+    }
+
     private void setUpMojo( EvaluateMojo mojo, InputHandler inputHandler, ExpressionEvaluator expressionEvaluator )
         throws IllegalAccessException
     {
@@ -129,21 +174,38 @@ public class EvaluateMojoTest
     private static final class InterceptingLog
         extends DefaultLog
     {
+        private boolean isInfoEnabled;
+
         List<String> infoLogs = new ArrayList<String>();
+
         List<String> warnLogs = new ArrayList<String>();
 
         public InterceptingLog( Logger logger )
         {
             super( logger );
+            this.isInfoEnabled = true;
+        }
+
+        public void setInfoEnabled( boolean isInfoEnabled )
+        {
+            this.isInfoEnabled = isInfoEnabled;
+        }
+
+        public boolean isInfoEnabled()
+        {
+            return isInfoEnabled;
         }
 
         @Override
         public void info( CharSequence content )
         {
-            super.info( content );
-            infoLogs.add( content.toString() );
+            if ( this.isInfoEnabled )
+            {
+                super.info( content );
+                infoLogs.add( content.toString() );
+            }
         }
-        
+
         @Override
         public void warn( CharSequence content )
         {
diff --git a/src/test/resources/unit/evaluate/plugin-config-quiet-stdout.xml b/src/test/resources/unit/evaluate/plugin-config-quiet-stdout.xml
new file mode 100644
index 0000000..22f2fbe
--- /dev/null
+++ b/src/test/resources/unit/evaluate/plugin-config-quiet-stdout.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 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.apache.maven.its.help</groupId>
+  <artifactId>evaluate</artifactId>
+  <packaging>jar</packaging>
+  <version>1.0-SNAPSHOT</version>
+  <url>http://maven.apache.org</url>
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-help-plugin</artifactId>
+        <configuration>
+          <forceOutput>true</forceOutput>
+          <expression>project.groupId</expression>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>

-- 
To stop receiving notification emails like this one, please contact
khmarbaise@apache.org.