Skip to content

Commit

Permalink
[#323] add java executable recognition.
Browse files Browse the repository at this point in the history
  • Loading branch information
bmarwell committed Jul 22, 2022
1 parent 11f6800 commit debe185
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions src/main/java/org/codehaus/mojo/exec/ExecMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.regex.Pattern;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.ExecuteException;
Expand Down Expand Up @@ -83,6 +84,12 @@
public class ExecMojo
extends AbstractExecMojo
{

/**
* Trying to recognize whether the given {@link #executable} might be a {@code java} binary.
*/
private static final Pattern ENDS_WITH_JAVA = Pattern.compile( "^.*java(\\.exe|\\.bin)?$", Pattern.CASE_INSENSITIVE );

/**
* <p>
* The executable. Can be a full path or the name of the executable. In the latter case, the executable must be in
Expand Down Expand Up @@ -313,6 +320,16 @@ public class ExecMojo
@Parameter( property = "exec.longModulepath", defaultValue = "true" )
private boolean longModulepath;

/**
* Forces the plugin to recognize the given executable as java executable. This helps with {@link #longClasspath}
* and {@link #longModulepath} parameters.
*
* <p>You shouldn't normally be needing this unless you renamed your java binary or are executing tools
* other than {@code java} which need modulepath or classpath parameters in a separate file.</p>
*/
@Parameter (property = "exec.forceJava", defaultValue = "false" )
private boolean forceJava;

/**
* If set to true the child process executes asynchronously and build execution continues in parallel.
*/
Expand Down Expand Up @@ -727,12 +744,35 @@ boolean isResultCodeAFailure( int result )

private boolean isLongClassPathArgument( String arg )
{
return longClasspath && ( "-classpath".equals( arg ) || "-cp".equals( arg ) );
return isJavaExec() && longClasspath && ( "-classpath".equals( arg ) || "-cp".equals( arg ) );
}

private boolean isLongModulePathArgument( String arg )
{
return longModulepath && ( "--module-path".equals( arg ) || "-p".equals( arg ) );
return isJavaExec() && longModulepath && ( "--module-path".equals( arg ) || "-p".equals( arg ) );
}

/**
* Returns {@code true} when a java binary is being executed.
*
* @return {@code true} when a java binary is being executed.
*/
private boolean isJavaExec()
{
if ( forceJava )
{
return true;
}

if ( this.executable.contains( "%JAVA_HOME" )
|| this.executable.contains( "${JAVA_HOME}" )
|| this.executable.contains( "$JAVA_HOME" ) )
{
// also applies to *most* other tools.
return true;
}

return ENDS_WITH_JAVA.matcher( this.executable ).matches();
}

/**
Expand Down

0 comments on commit debe185

Please sign in to comment.