Skip to content

Commit 584c544

Browse files
kriegaexslawekjaranowski
authored andcommitted
[#389] Add option 'blockSystemExit' to 'java' mojo
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.
1 parent c545089 commit 584c544

File tree

16 files changed

+460
-0
lines changed

16 files changed

+460
-0
lines changed

pom.xml

+23
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@
9494
</roles>
9595
<timezone>Europe/Berlin</timezone>
9696
</contributor>
97+
<contributor>
98+
<name>Alexander Kriegisch</name>
99+
<email>protected</email>
100+
<organization>Scrum-Master.de - Agiles Projektmanagement</organization>
101+
<organizationUrl>https://scrum-master.de</organizationUrl>
102+
<roles>
103+
<role>Feature Contributor</role>
104+
</roles>
105+
</contributor>
97106
</contributors>
98107

99108
<licenses>
@@ -335,6 +344,15 @@
335344
<invoker.parallelThreads>1</invoker.parallelThreads>
336345
</properties>
337346
</profile>
347+
<profile>
348+
<id>java17+</id>
349+
<activation>
350+
<jdk>[17,)</jdk>
351+
</activation>
352+
<properties>
353+
<invoker.security.manager>-Djava.security.manager=allow</invoker.security.manager>
354+
</properties>
355+
</profile>
338356
<profile>
339357
<id>run-its</id>
340358
<build>
@@ -360,6 +378,11 @@
360378
<scriptVariables>
361379
<projectVersion>${project.version}</projectVersion>
362380
</scriptVariables>
381+
<!--
382+
Necessary on JDK 17+ for ITs "mexec-gh-389-block-exit-*" to avoid "UnsupportedOperationException:
383+
The Security Manager is deprecated and will be removed in a future release". See profile 'java17+'.
384+
-->
385+
<mavenOpts>${invoker.security.manager}</mavenOpts>
363386
</configuration>
364387
<executions>
365388
<execution>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
invoker.goals = clean process-classes
2+
invoker.buildResult = failure
3+
invoker.debug = false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>org.codehaus.mojo.exec.it</groupId>
8+
<artifactId>parent</artifactId>
9+
<version>0.1</version>
10+
</parent>
11+
12+
<artifactId>mexec-gh-389</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
15+
<build>
16+
<plugins>
17+
<plugin>
18+
<groupId>org.codehaus.mojo</groupId>
19+
<artifactId>exec-maven-plugin</artifactId>
20+
<version>@project.version@</version>
21+
<executions>
22+
<execution>
23+
<phase>process-classes</phase>
24+
<goals>
25+
<goal>java</goal>
26+
</goals>
27+
<configuration>
28+
<mainClass>Main</mainClass>
29+
<blockSystemExit>true</blockSystemExit>
30+
<systemProperties>
31+
<systemProperty>
32+
<key>exitBehaviour</key>
33+
<value>system-exit-error</value>
34+
</systemProperty>
35+
</systemProperties>
36+
<arguments>
37+
<argument>one</argument>
38+
<argument>two</argument>
39+
<argument>three</argument>
40+
</arguments>
41+
</configuration>
42+
</execution>
43+
</executions>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.util.Arrays;
2+
3+
public class Main
4+
{
5+
public static void main( String[] args )
6+
{
7+
System.out.println( Arrays.toString( args ) );
8+
switch ( System.getProperty( "exitBehaviour", "ok" ) )
9+
{
10+
case "throw-exception":
11+
throw new RuntimeException( "uh-oh" );
12+
case "system-exit-ok":
13+
System.exit( 0 );
14+
case "system-exit-error":
15+
System.exit( 123 );
16+
}
17+
System.out.println( "OK" );
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright MojoHaus and Contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
def buildLogLines = new File( basedir, "build.log" ).readLines()
18+
19+
// Find "System::exit was called" line index
20+
def infoMessageLineNumber = buildLogLines.indexOf("[INFO] System::exit was called with return code 123")
21+
assert infoMessageLineNumber > 0
22+
// Verify that preceding line is program output
23+
assert buildLogLines[infoMessageLineNumber - 1] == "[one, two, three]"
24+
// Verify that subsequent lines contain the beginning of the thrown SystemExitException stack trace
25+
assert buildLogLines[infoMessageLineNumber + 1].startsWith("[WARNING]")
26+
assert buildLogLines[infoMessageLineNumber + 2].contains("SystemExitException: System::exit was called with return code 123")
27+
assert buildLogLines[infoMessageLineNumber + 3].contains("SystemExitManager.checkExit (SystemExitManager.java")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
invoker.goals = clean process-classes
2+
invoker.buildResult = success
3+
invoker.debug = false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>org.codehaus.mojo.exec.it</groupId>
8+
<artifactId>parent</artifactId>
9+
<version>0.1</version>
10+
</parent>
11+
12+
<artifactId>mexec-gh-389</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
15+
<build>
16+
<plugins>
17+
<plugin>
18+
<groupId>org.codehaus.mojo</groupId>
19+
<artifactId>exec-maven-plugin</artifactId>
20+
<version>@project.version@</version>
21+
<executions>
22+
<execution>
23+
<phase>process-classes</phase>
24+
<goals>
25+
<goal>java</goal>
26+
</goals>
27+
<configuration>
28+
<mainClass>Main</mainClass>
29+
<blockSystemExit>true</blockSystemExit>
30+
<systemProperties>
31+
<systemProperty>
32+
<key>exitBehaviour</key>
33+
<value>system-exit-ok</value>
34+
</systemProperty>
35+
</systemProperties>
36+
<arguments>
37+
<argument>one</argument>
38+
<argument>two</argument>
39+
<argument>three</argument>
40+
</arguments>
41+
</configuration>
42+
</execution>
43+
</executions>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.util.Arrays;
2+
3+
public class Main
4+
{
5+
public static void main( String[] args )
6+
{
7+
System.out.println( Arrays.toString( args ) );
8+
switch ( System.getProperty( "exitBehaviour", "ok" ) )
9+
{
10+
case "throw-exception":
11+
throw new RuntimeException( "uh-oh" );
12+
case "system-exit-ok":
13+
System.exit( 0 );
14+
case "system-exit-error":
15+
System.exit( 123 );
16+
}
17+
System.out.println( "OK" );
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright MojoHaus and Contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
def buildLogLines = new File( basedir, "build.log" ).readLines()
18+
19+
// Find "System::exit was called" line index
20+
def infoMessageLineNumber = buildLogLines.indexOf("[INFO] System::exit was called with return code 0")
21+
assert infoMessageLineNumber > 0
22+
// Verify that preceding line is program output
23+
assert buildLogLines[infoMessageLineNumber - 1] == "[one, two, three]"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
invoker.goals = clean process-classes
2+
# Cannot not check result, because build terminates unexpectedly
3+
# invoker.buildResult = failure
4+
invoker.debug = false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>org.codehaus.mojo.exec.it</groupId>
8+
<artifactId>parent</artifactId>
9+
<version>0.1</version>
10+
</parent>
11+
12+
<artifactId>mexec-gh-389</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
15+
<build>
16+
<plugins>
17+
<plugin>
18+
<groupId>org.codehaus.mojo</groupId>
19+
<artifactId>exec-maven-plugin</artifactId>
20+
<version>@project.version@</version>
21+
<executions>
22+
<execution>
23+
<phase>process-classes</phase>
24+
<goals>
25+
<goal>java</goal>
26+
</goals>
27+
<configuration>
28+
<mainClass>Main</mainClass>
29+
<!-- This is the default, no need to specify it -->
30+
<!--<blockSystemExit>false</blockSystemExit>-->
31+
<systemProperties>
32+
<systemProperty>
33+
<key>exitBehaviour</key>
34+
<!-- System.exit with any return code will terminate the JVM and the whole Maven process -->
35+
<value>system-exit-ok</value>
36+
</systemProperty>
37+
</systemProperties>
38+
<arguments>
39+
<argument>one</argument>
40+
<argument>two</argument>
41+
<argument>three</argument>
42+
</arguments>
43+
</configuration>
44+
</execution>
45+
</executions>
46+
</plugin>
47+
</plugins>
48+
</build>
49+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.util.Arrays;
2+
3+
public class Main
4+
{
5+
public static void main( String[] args )
6+
{
7+
System.out.println( Arrays.toString( args ) );
8+
switch ( System.getProperty( "exitBehaviour", "ok" ) )
9+
{
10+
case "throw-exception":
11+
throw new RuntimeException( "uh-oh" );
12+
case "system-exit-ok":
13+
System.exit( 0 );
14+
case "system-exit-error":
15+
System.exit( 123 );
16+
}
17+
System.out.println( "OK" );
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright MojoHaus and Contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
def buildLogLines = new File( basedir, "build.log" ).readLines()
18+
19+
// Second-last line is the last line the called program prints before exiting the JVM with System.exit.
20+
// Last line is "Running post-build script: ...", i.e. we need to disregard it.
21+
assert buildLogLines[-2] == "[one, two, three]"

0 commit comments

Comments
 (0)