You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by "Hervé Boutemy (JIRA)" <ji...@apache.org> on 2019/01/19 11:43:00 UTC

[jira] [Commented] (MSHARED-795) add timeout feature to Invoker

    [ https://issues.apache.org/jira/browse/MSHARED-795?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16747049#comment-16747049 ] 

Hervé Boutemy commented on MSHARED-795:
---------------------------------------

olamy closed pull request #1: MINVOKER-233 Improve DefaultInvoker with a timeout.
URL: https://github.com/apache/maven-invoker/pull/1

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/src/main/java/org/apache/maven/shared/invoker/DefaultInvoker.java b/src/main/java/org/apache/maven/shared/invoker/DefaultInvoker.java
index 39be540..3ef26c3 100644
— a/src/main/java/org/apache/maven/shared/invoker/DefaultInvoker.java
+++ b/src/main/java/org/apache/maven/shared/invoker/DefaultInvoker.java
@@ -39,6 +39,8 @@

public static final String ROLE_HINT = "default";

+ private static final int NO_TIMEOUT = 0;
+
private static final InvokerLogger DEFAULT_LOGGER = new SystemOutLogger();

private static final InvocationOutputHandler DEFAULT_OUTPUT_HANDLER = new SystemOutHandler();
@@ -60,6 +62,12 @@
private InvocationOutputHandler errorHandler = DEFAULT_OUTPUT_HANDLER;

public InvocationResult execute( InvocationRequest request )
+ throws MavenInvocationException
+

{ + return this.execute( request , NO_TIMEOUT ); + }

+
+ public InvocationResult execute( InvocationRequest request, int timeoutInSeconds )
throws MavenInvocationException
{
MavenCommandLineBuilder cliBuilder = new MavenCommandLineBuilder();
@@ -108,7 +116,7 @@ public InvocationResult execute( InvocationRequest request )

try

{ - int exitCode = executeCommandLine( cli, request ); + int exitCode = executeCommandLine( cli, request, timeoutInSeconds ); result.setExitCode( exitCode ); }

@@ -120,7 +128,7 @@ public InvocationResult execute( InvocationRequest request )
return result;
}

    private int executeCommandLine( Commandline cli, InvocationRequest request )
    + private int executeCommandLine( Commandline cli, InvocationRequest request, int timeoutInSeconds )
    throws CommandLineException { int result = Integer.MIN_VALUE; @@ -141,7 +149,7 @@ private int executeCommandLine( Commandline cli, InvocationRequest request ) getLogger().info( "Executing in batch mode. The configured input stream will be ignored." ); }

    result = CommandLineUtils.executeCommandLine( cli, outputHandler, errorHandler );
    + result = CommandLineUtils.executeCommandLine( cli, outputHandler, errorHandler, timeoutInSeconds );
    }
    else { @@ -150,11 +158,12 @@ private int executeCommandLine( Commandline cli, InvocationRequest request ) getLogger().warn( "Maven will be executed in interactive mode" + ", but no input stream has been configured for this MavenInvoker instance." ); - result = CommandLineUtils.executeCommandLine( cli, outputHandler, errorHandler ); + result = CommandLineUtils.executeCommandLine( cli, outputHandler, errorHandler, timeoutInSeconds ); }

    else
    { - result = CommandLineUtils.executeCommandLine( cli, inputStream, outputHandler, errorHandler ); + result = CommandLineUtils.executeCommandLine( cli, inputStream, outputHandler, errorHandler, + timeoutInSeconds ); }

    }

diff --git a/src/main/java/org/apache/maven/shared/invoker/Invoker.java b/src/main/java/org/apache/maven/shared/invoker/Invoker.java
index 580e7b3..10574f6 100644
— a/src/main/java/org/apache/maven/shared/invoker/Invoker.java
+++ b/src/main/java/org/apache/maven/shared/invoker/Invoker.java
@@ -47,6 +47,19 @@
InvocationResult execute( InvocationRequest request )
throws MavenInvocationException;

+ /**
+ * Executes Maven using the parameters specified by the given invocation request. Parameters not specified by the
+ * invocation request will be derived from the state of this invoker instance. In case both the invoker instance and
+ * the invocation request provide a value for a particular option, the value from the invocation request dominates.
+ *
+ * @param request The invocation request to execute, must not be <code>null</code>.
+ * @param timeoutInSeconds If a value > 0 is specified, the goal might be interrupted after the timeout is reached.
+ * @return The result of the Maven invocation, never <code>null</code>.
+ * @throws MavenInvocationException
+ */
+ InvocationResult execute( InvocationRequest request, int timeoutInSeconds )
+ throws MavenInvocationException;
+
/**

    Gets the path to the base directory of the local repository to use for the Maven invocation.

    diff --git a/src/test/java/org/apache/maven/shared/invoker/DefaultInvokerTest.java b/src/test/java/org/apache/maven/shared/invoker/DefaultInvokerTest.java
    index 02084ae..00c4abb 100644
            a/src/test/java/org/apache/maven/shared/invoker/DefaultInvokerTest.java
            +++ b/src/test/java/org/apache/maven/shared/invoker/DefaultInvokerTest.java
            @@ -87,6 +87,32 @@ public void testBuildShouldFail()
            assertEquals( 1, result.getExitCode() );
            }

+ @Test
+ public void testBuildShouldTimeout()
+ throws IOException, MavenInvocationException, URISyntaxException
+ {
+ File basedir = getBasedirForBuild();
+
+ Invoker invoker = newInvoker();
+
+ InvocationRequest request = new DefaultInvocationRequest();
+ request.setBaseDirectory( basedir );
+ request.setDebug( true );
+ request.setGoals( Arrays.asList( "clean", "package" ) );
+
+ if ( !System.getProperty( "java.version" ).startsWith( "1." ) )
+

{ + Properties properties = new Properties(); + properties.put( "maven.compiler.source", "1.6" ); + properties.put( "maven.compiler.target", "1.6" ); + request.setProperties( properties ); + }

+
+ InvocationResult result = invoker.execute( request, 10 );
+
+ assertEquals( 1, result.getExitCode() );
+ }
+
@Test
public void testSpacePom()
throws Exception
diff --git a/src/test/resources/test-build-should-timeout/pom.xml b/src/test/resources/test-build-should-timeout/pom.xml
new file mode 100644
index 0000000..d43d9a5
— /dev/null
+++ b/src/test/resources/test-build-should-timeout/pom.xml
@@ -0,0 +1,34 @@
+<!--
+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/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.apache.maven.shared.invoker</groupId>
+ <artifactId>test-build-should-timeout</artifactId>
+ <packaging>jar</packaging>
+ <version>1</version>
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>3.8.2</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+</project>
diff --git a/src/test/resources/test-build-should-timeout/src/main/java/org/apache/maven/shared/invoker/App.java b/src/test/resources/test-build-should-timeout/src/main/java/org/apache/maven/shared/invoker/App.java
new file mode 100644
index 0000000..753a51c
— /dev/null
+++ b/src/test/resources/test-build-should-timeout/src/main/java/org/apache/maven/shared/invoker/App.java
@@ -0,0 +1,32 @@
+package org.apache.maven.shared.invoker;
+
+/*
+ * 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.
+ */
+
+/**
+ * Hello world!
+ *
+ */
+public class App
+{
+ public static void main( String[] args )
+

{ + System.out.println( "Hello World!" ); + }

+}
diff --git a/src/test/resources/test-build-should-timeout/src/test/java/org/apache/maven/shared/invoker/AppTest.java b/src/test/resources/test-build-should-timeout/src/test/java/org/apache/maven/shared/invoker/AppTest.java
new file mode 100644
index 0000000..3a01f8a
— /dev/null
+++ b/src/test/resources/test-build-should-timeout/src/test/java/org/apache/maven/shared/invoker/AppTest.java
@@ -0,0 +1,61 @@
+package org.apache.maven.shared.invoker;
+
+/*
+ * 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.
+ */
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * Unit test for simple App.
+ */
+public class AppTest
+ extends TestCase
+{
+ /**
+ * Create the test case
+ *
+ * @param testName name of the test case
+ */
+ public AppTest( String testName )
+

{ + super( testName ); + }

+
+ /**
+ * @return the suite of tests being tested
+ */
+ public static Test suite()
+

{ + return new TestSuite( AppTest.class ); + }

+
+ /**
+ * Not ending test
+ */
+ public void testApp()
+ {
+ while (true)

{ + Thread.sleep(1); + }

+
+ assertTrue(true);
+ }
+}

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org

> add timeout feature to Invoker
> ------------------------------
>
>                 Key: MSHARED-795
>                 URL: https://issues.apache.org/jira/browse/MSHARED-795
>             Project: Maven Shared Components
>          Issue Type: New Feature
>          Components: maven-invoker
>    Affects Versions: maven-invoker-3.0.0
>            Reporter: Hervé Boutemy
>            Assignee: Olivier Lamy (*$^¨%`£)
>            Priority: Major
>             Fix For: maven-invoker-3.0.1
>
>
> new feature required for MINVOKER-233



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)