Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MINVOKER-233] Improve DefaultInvoker with a timeout. #1

Merged
merged 1 commit into from
Mar 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/main/java/org/apache/maven/shared/invoker/DefaultInvoker.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class DefaultInvoker

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();
Expand All @@ -60,6 +62,12 @@ public class DefaultInvoker
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();
Expand Down Expand Up @@ -108,7 +116,7 @@ public InvocationResult execute( InvocationRequest request )

try
{
int exitCode = executeCommandLine( cli, request );
int exitCode = executeCommandLine( cli, request, timeoutInSeconds );

result.setExitCode( exitCode );
}
Expand All @@ -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;
Expand All @@ -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
{
Expand All @@ -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 );
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/apache/maven/shared/invoker/Invoker.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ public interface Invoker
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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions src/test/resources/test-build-should-timeout/pom.xml
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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!" );
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}