maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From khmarba...@apache.org
Subject [maven-help-plugin] 01/01: [MPH-144] - Add ability to print mvn help:evaluate output to stdout in quiet mode. o Added option -DforceStdout to print out information independent of -q option of Maven.
Date Fri, 30 Mar 2018 18:00:55 GMT
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 682a5456ce9aefa948f392c7b05bb96931b381e0
Author: Karl Heinz Marbaise <khmarbaise@apache.org>
AuthorDate: Fri Mar 30 19:33:43 2018 +0200

    [MPH-144] - Add ability to print mvn help:evaluate output to stdout in quiet mode.
     o Added option -DforceStdout to print out information
       independent of -q option of Maven.
---
 .../apache/maven/plugins/help/EvaluateMojo.java    | 37 ++++++++++--
 .../maven/plugins/help/EvaluateMojoTest.java       | 68 ++++++++++++++++++++--
 .../unit/evaluate/plugin-config-quiet-stdout.xml   | 39 +++++++++++++
 3 files changed, 135 insertions(+), 9 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..65c1b88 100644
--- a/src/main/java/org/apache/maven/plugins/help/EvaluateMojo.java
+++ b/src/main/java/org/apache/maven/plugins/help/EvaluateMojo.java
@@ -107,10 +107,27 @@ 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 information 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 -DforceStdout)
+     * echo $RESULT
+     * </pre>
+     * 
+     * This will only printout the information which has been requested by <code>expression</code>
to
+     * <code>stdout</code>.
+     * 
+     * @since 3.1.0
+     */
+    @Parameter( property = "forceStdout", defaultValue = "false" )
+    private boolean forceStdout;
+
+    /**
+     * 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;
@@ -379,7 +396,17 @@ public class EvaluateMojo
         }
         else
         {
-            getLog().info( LS + response.toString() );
+            if ( getLog().isInfoEnabled() )
+            {
+                getLog().info( LS + response.toString() );
+            }
+            else
+            {
+                if ( forceStdout )
+                {
+                    System.out.print( response.toString() );
+                }
+            }
         }
     }
 
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..bf22790 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;
 
@@ -116,6 +118,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 +172,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..0e9b41c
--- /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>
+          <forceStdout>true</forceStdout>
+          <expression>project.groupId</expression>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>

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

Mime
View raw message