Skip to content

Commit

Permalink
[#323] add java executable recognition.
Browse files Browse the repository at this point in the history
- added two ITs to check true/false behaviour of the new parameter.
- removed dead code
  • Loading branch information
bmarwell committed Jul 27, 2022
1 parent c8e2235 commit cbc7a7d
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 149 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@
<email>[email protected]</email>
<timezone>Europe/Warsaw</timezone>
</developer>
<developer>
<id>bmarwell</id>
<name>Benjamin Marwell</name>
<email>[email protected]</email>
<timezone>Europe/Berlin</timezone>
</developer>
</developers>

<contributors>
Expand Down
3 changes: 3 additions & 0 deletions src/it/projects/mexec-323-force-java/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
invoker.goals = compile
invoker.debug = true
invoker.os.family = !windows
44 changes: 44 additions & 0 deletions src/it/projects/mexec-323-force-java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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-323</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>@pom.version@</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>echo</executable>
<forceJava>true</forceJava>
<arguments>
<argument>--</argument>
<argument>-p</argument>
<argument>dist/emails</argument>
</arguments>
<workingDirectory>src/build</workingDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
22 changes: 22 additions & 0 deletions src/it/projects/mexec-323-force-java/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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 log = new File( basedir, "build.log" )
assert log.exists()
// missing dist/emails here
assert log.text.contains( "Executing command line: [echo, --, -p]" )
3 changes: 3 additions & 0 deletions src/it/projects/mexec-323/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
invoker.goals = compile
invoker.debug = true
invoker.os.family = !windows
42 changes: 42 additions & 0 deletions src/it/projects/mexec-323/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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-323</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>@pom.version@</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>mkdir</executable>
<arguments>
<argument>-p</argument>
<argument>dist/emails</argument>
</arguments>
<workingDirectory>src/build</workingDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
25 changes: 25 additions & 0 deletions src/it/projects/mexec-323/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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 createdDir = new File(basedir, "src/build/dist/emails")
assert createdDir.exists()
assert createdDir.isDirectory()

File log = new File(basedir, 'build.log')
assert log.exists()
assert log.getText().contains( "Executing command line: [mkdir, -p, dist/emails]" )
46 changes: 44 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,18 @@ 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>
*
* @since 3.1.1
*/
@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 +746,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
Loading

0 comments on commit cbc7a7d

Please sign in to comment.