Skip to content

Commit

Permalink
[#389] Add option 'blockSystemExit' to 'java' mojo
Browse files Browse the repository at this point in the history
This new option enables users to stop programs called by 'exec:java'
from calling System::exit, terminating not just the mojo but the whole
Maven JVM.

Closes #389.
Relates to #388.
  • Loading branch information
kriegaex committed Nov 9, 2023
1 parent 75b2e74 commit 2bab5ec
Show file tree
Hide file tree
Showing 16 changed files with 466 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,15 @@
<invoker.parallelThreads>1</invoker.parallelThreads>
</properties>
</profile>
<profile>
<id>java17+</id>
<activation>
<jdk>[17,)</jdk>
</activation>
<properties>
<invoker.security.manager>-Djava.security.manager=allow</invoker.security.manager>
</properties>
</profile>
<profile>
<id>run-its</id>
<build>
Expand All @@ -360,6 +369,11 @@
<scriptVariables>
<projectVersion>${project.version}</projectVersion>
</scriptVariables>
<!--
Necessary on JDK 17+ for ITs "mexec-gh-389-block-exit-*" to avoid "UnsupportedOperationException:
The Security Manager is deprecated and will be removed in a future release". See profile 'java17+'.
-->
<mavenOpts>${invoker.security.manager}</mavenOpts>
</configuration>
<executions>
<execution>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
invoker.goals = clean process-classes
invoker.buildResult = failure
invoker.debug = false
47 changes: 47 additions & 0 deletions src/it/projects/mexec-gh-389-block-exit-non-zero/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>

<parent>
<groupId>org.codehaus.mojo.exec.it</groupId>
<artifactId>parent</artifactId>
<version>0.1</version>
</parent>

<artifactId>mexec-gh-389</artifactId>
<version>0.0.1-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>Main</mainClass>
<blockSystemExit>true</blockSystemExit>
<systemProperties>
<systemProperty>
<key>exitBehaviour</key>
<value>system-exit-error</value>
</systemProperty>
</systemProperties>
<arguments>
<argument>one</argument>
<argument>two</argument>
<argument>three</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.util.Arrays;

public class Main
{
public static void main( String[] args )
{
System.out.println( Arrays.toString( args ) );
switch ( System.getProperty( "exitBehaviour", "ok" ) )
{
case "throw-exception":
throw new RuntimeException( "uh-oh" );
case "system-exit-ok":
System.exit( 0 );
case "system-exit-error":
System.exit( 123 );
}
System.out.println( "OK" );
}
}
30 changes: 30 additions & 0 deletions src/it/projects/mexec-gh-389-block-exit-non-zero/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.
*/

def buildLogLines = new File( basedir, "build.log" ).readLines()

// Find "System::exit was called" line index
def infoMessageLineNumber = buildLogLines.indexOf("[INFO] System::exit was called with return code 123")
assert infoMessageLineNumber > 0
// Verify that preceding line is program output
assert buildLogLines[infoMessageLineNumber - 1] == "[one, two, three]"
// Verify that subsequent lines contain the beginning of the thrown SystemExitException stack trace
assert buildLogLines[infoMessageLineNumber + 1].startsWith("[WARNING]")
assert buildLogLines[infoMessageLineNumber + 2].contains("SystemExitException: System::exit was called with return code 123")
assert buildLogLines[infoMessageLineNumber + 3].contains("SystemExitManager.checkExit (SystemExitManager.java")
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
invoker.goals = clean process-classes
invoker.buildResult = success
invoker.debug = false
47 changes: 47 additions & 0 deletions src/it/projects/mexec-gh-389-block-exit-zero/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>

<parent>
<groupId>org.codehaus.mojo.exec.it</groupId>
<artifactId>parent</artifactId>
<version>0.1</version>
</parent>

<artifactId>mexec-gh-389</artifactId>
<version>0.0.1-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>Main</mainClass>
<blockSystemExit>true</blockSystemExit>
<systemProperties>
<systemProperty>
<key>exitBehaviour</key>
<value>system-exit-ok</value>
</systemProperty>
</systemProperties>
<arguments>
<argument>one</argument>
<argument>two</argument>
<argument>three</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.util.Arrays;

public class Main
{
public static void main( String[] args )
{
System.out.println( Arrays.toString( args ) );
switch ( System.getProperty( "exitBehaviour", "ok" ) )
{
case "throw-exception":
throw new RuntimeException( "uh-oh" );
case "system-exit-ok":
System.exit( 0 );
case "system-exit-error":
System.exit( 123 );
}
System.out.println( "OK" );
}
}
26 changes: 26 additions & 0 deletions src/it/projects/mexec-gh-389-block-exit-zero/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.
*/

def buildLogLines = new File( basedir, "build.log" ).readLines()

// Find "System::exit was called" line index
def infoMessageLineNumber = buildLogLines.indexOf("[INFO] System::exit was called with return code 0")
assert infoMessageLineNumber > 0
// Verify that preceding line is program output
assert buildLogLines[infoMessageLineNumber - 1] == "[one, two, three]"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
invoker.goals = clean process-classes
# Cannot not check result, because build terminates unexpectedly
# invoker.buildResult = failure
invoker.debug = false
49 changes: 49 additions & 0 deletions src/it/projects/mexec-gh-389-default-permit-exit/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>

<parent>
<groupId>org.codehaus.mojo.exec.it</groupId>
<artifactId>parent</artifactId>
<version>0.1</version>
</parent>

<artifactId>mexec-gh-389</artifactId>
<version>0.0.1-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>Main</mainClass>
<!-- This is the default, no need to specify it -->
<!--<blockSystemExit>false</blockSystemExit>-->
<systemProperties>
<systemProperty>
<key>exitBehaviour</key>
<!-- System.exit with any return code will terminate the JVM and the whole Maven process -->
<value>system-exit-ok</value>
</systemProperty>
</systemProperties>
<arguments>
<argument>one</argument>
<argument>two</argument>
<argument>three</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.util.Arrays;

public class Main
{
public static void main( String[] args )
{
System.out.println( Arrays.toString( args ) );
switch ( System.getProperty( "exitBehaviour", "ok" ) )
{
case "throw-exception":
throw new RuntimeException( "uh-oh" );
case "system-exit-ok":
System.exit( 0 );
case "system-exit-error":
System.exit( 123 );
}
System.out.println( "OK" );
}
}
24 changes: 24 additions & 0 deletions src/it/projects/mexec-gh-389-default-permit-exit/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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.
*/

def buildLogLines = new File( basedir, "build.log" ).readLines()

// Second-last line is the last line the called program prints before exiting the JVM with System.exit.
// Last line is "Running post-build script: ...", i.e. we need to disregard it.
assert buildLogLines[-2] == "[one, two, three]"

0 comments on commit 2bab5ec

Please sign in to comment.