Skip to content

Commit

Permalink
[MINVOKER-294] Allow override mavenExecutable in invoker.properties
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed May 16, 2022
1 parent e13fec8 commit fbe2481
Show file tree
Hide file tree
Showing 12 changed files with 286 additions and 15 deletions.
57 changes: 57 additions & 0 deletions src/it/MINVOKER-294-mavenExacutable-in-properties/pom.xml
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.plugins.invoker.its</groupId>
<artifactId>minvoker-294</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<description>
Test to check for usage of mavenExecutable in invoker.properties.
</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-invoker-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>integration-test</id>
<phase>initialize</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
@@ -0,0 +1,20 @@
# 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.

invoker.mavenExecutable.1 = mvn1
invoker.mavenExecutable.2 = mvn2
invoker.mavenExecutable.3 = mvn
@@ -0,0 +1,21 @@
#!/bin/sh
# ----------------------------------------------------------------------------
# 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.
# ----------------------------------------------------------------------------

echo "maven 1 execute"
@@ -0,0 +1,21 @@
@REM ----------------------------------------------------------------------------
@REM Licensed to the Apache Software Foundation (ASF) under one
@REM or more contributor license agreements. See the NOTICE file
@REM distributed with this work for additional information
@REM regarding copyright ownership. The ASF licenses this file
@REM to you under the Apache License, Version 2.0 (the
@REM "License"); you may not use this file except in compliance
@REM with the License. You may obtain a copy of the License at
@REM
@REM http://www.apache.org/licenses/LICENSE-2.0
@REM
@REM Unless required by applicable law or agreed to in writing,
@REM software distributed under the License is distributed on an
@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@REM KIND, either express or implied. See the License for the
@REM specific language governing permissions and limitations
@REM under the License.
@REM ----------------------------------------------------------------------------
@ECHO off

echo maven 1 execute
@@ -0,0 +1,21 @@
#!/bin/sh
# ----------------------------------------------------------------------------
# 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.
# ----------------------------------------------------------------------------

echo "maven 2 execute"
@@ -0,0 +1,21 @@
@REM ----------------------------------------------------------------------------
@REM Licensed to the Apache Software Foundation (ASF) under one
@REM or more contributor license agreements. See the NOTICE file
@REM distributed with this work for additional information
@REM regarding copyright ownership. The ASF licenses this file
@REM to you under the Apache License, Version 2.0 (the
@REM "License"); you may not use this file except in compliance
@REM with the License. You may obtain a copy of the License at
@REM
@REM http://www.apache.org/licenses/LICENSE-2.0
@REM
@REM Unless required by applicable law or agreed to in writing,
@REM software distributed under the License is distributed on an
@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@REM KIND, either express or implied. See the License for the
@REM specific language governing permissions and limitations
@REM under the License.
@REM ----------------------------------------------------------------------------
@ECHO off

echo maven 2 execute
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven.plugins.invoker.its</groupId>
<artifactId>minvoker-294</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>pom</packaging>
</project>
@@ -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 buildLog = new File( basedir, "build.log" ).text

assert buildLog.contains( "maven 1 execute" )
assert buildLog.contains( "maven 2 execute" )
assert buildLog.contains( "[INFO] BUILD SUCCESS" )
@@ -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.
*/

// workaround for executable flags, eg during build from source-release.zip
new File( basedir, "mvn1" ).setExecutable( true )
new File( basedir, "mvn2" ).setExecutable( true )
Expand Up @@ -415,13 +415,13 @@ public abstract class AbstractInvokerMojo
private File mavenHome;

/**
* mavenExecutable can either be a file relative to <code>${maven.home}/bin/</code> or an absolute file.
* mavenExecutable can either be a file relative to <code>${maven.home}/bin/</code>, test project workspace
* or an absolute file.
*
* @since 1.8
* @see Invoker#setMavenExecutable(File)
*/
@Parameter( property = "invoker.mavenExecutable" )
private String mavenExecutable;
private File mavenExecutable;

/**
* The <code>JAVA_HOME</code> environment variable to use for forked Maven invocations. Defaults to the current Java
Expand Down Expand Up @@ -501,6 +501,12 @@ public abstract class AbstractInvokerMojo
* # can be indexed
* invoker.project = sub-module
*
* # The maven executable can either be a file relative to <code>${maven.home}/bin/</code>, test project workspace
* # or an absolute file.
* # Since plugin version 3.3.0
* # can be indexed
* invoker.mavenExecutable = mvnw
*
* # The value for the environment variable MAVEN_OPTS
* # can be indexed
* invoker.mavenOpts = -Dfile.encoding=UTF-16 -Xms32m -Xmx256m
Expand Down Expand Up @@ -2007,14 +2013,6 @@ private boolean runBuild( File basedir, File pomFile, File settingsFile, File ac
throw new RunFailureException( BuildJob.Result.FAILURE_PRE_HOOK, e );
}

// TODO - move to request
invoker.setMavenHome( mavenHome );

if ( mavenExecutable != null )
{
invoker.setMavenExecutable( new File( mavenExecutable ) );
}

for ( int invocationIndex = 1;; invocationIndex++ )
{
if ( invocationIndex > 1 && !invokerProperties.isInvocationDefined( invocationIndex ) )
Expand All @@ -2031,7 +2029,7 @@ private boolean runBuild( File basedir, File pomFile, File settingsFile, File ac
request.setShowErrors( showErrors );
request.setShowVersion( showVersion );
request.setJavaHome( actualJavaHome );

request.setMavenHome( mavenHome );
setupLoggerForBuildJob( logger, request );

request.setBaseDirectory( basedir );
Expand Down Expand Up @@ -2639,6 +2637,7 @@ private InvokerProperties getInvokerProperties( final File projectDirectory, Pro
invokerProperties.setDefaultDebug( debug );
invokerProperties.setDefaultGoals( goals );
invokerProperties.setDefaultProfiles( profiles );
invokerProperties.setDefaultMavenExecutable( mavenExecutable );
invokerProperties.setDefaultMavenOpts( mavenOpts );
invokerProperties.setDefaultTimeoutInSeconds( timeoutInSeconds );
invokerProperties.setDefaultEnvironmentVariables( environmentVariables );
Expand Down
Expand Up @@ -54,13 +54,15 @@ class InvokerProperties
private String defaultMavenOpts;
private Integer defaultTimeoutInSeconds;
private Map<String, String> defaultEnvironmentVariables;
private File defaultMavenExecutable;

private enum InvocationProperty
{
PROJECT( "invoker.project" ),
BUILD_RESULT( "invoker.buildResult" ),
GOALS( "invoker.goals" ),
PROFILES( "invoker.profiles" ),
MAVEN_EXECUTABLE( "invoker.mavenExecutable" ),
MAVEN_OPTS( "invoker.mavenOpts" ),
FAILURE_BEHAVIOR( "invoker.failureBehavior" ),
NON_RECURSIVE( "invoker.nonRecursive" ),
Expand Down Expand Up @@ -147,6 +149,15 @@ public void setDefaultProfiles( List<String> defaultProfiles )
this.defaultProfiles = defaultProfiles;
}

/**
* Default value for mavenExecutable
* @param defaultMavenExecutable a default value
*/
public void setDefaultMavenExecutable( File defaultMavenExecutable )
{
this.defaultMavenExecutable = defaultMavenExecutable;
}

/**
* Default value for mavenOpts
* @param defaultMavenOpts a default value
Expand Down Expand Up @@ -423,6 +434,10 @@ public void configureInvocation( InvocationRequest request, int index )
.filter( l -> !l.isEmpty() )
.orElse( defaultProfiles ) );

setIfNotNull( request::setMavenExecutable, get( InvocationProperty.MAVEN_EXECUTABLE, index )
.map( File::new )
.orElse( defaultMavenExecutable ) );

setIfNotNull( request::setMavenOpts, get( InvocationProperty.MAVEN_OPTS, index ).orElse( defaultMavenOpts ) );

get( InvocationProperty.FAILURE_BEHAVIOR, index )
Expand Down
Expand Up @@ -223,11 +223,35 @@ public void testConfigureRequestProject() throws Exception
}
}

@Test
public void testConfigureRequestMavenExecutable()
{
Properties props = new Properties();

InvokerProperties facade = new InvokerProperties( props );
File aDefExecutable = new File( "defExecutable" );
facade.setDefaultMavenExecutable( aDefExecutable );

props.setProperty( "invoker.mavenExecutable", "aPropExecutable" );
facade.configureInvocation( request, 0 );
verify( request ).setMavenExecutable( new File( "aPropExecutable" ) );
verifyNoMoreInteractions( request );
clearInvocations( request );

props.clear();

facade.configureInvocation( request, 0 );
verify( request ).setMavenExecutable( aDefExecutable );
verifyNoMoreInteractions( request );
}

@Test
public void testConfigureRequestMavenOpts()
{
Properties props = new Properties();

InvokerProperties facade = new InvokerProperties( props );
facade.setDefaultMavenOpts( "-XxxDef" );

props.setProperty( "invoker.mavenOpts", "-Xmx512m" );
facade.configureInvocation( request, 0 );
Expand All @@ -237,11 +261,9 @@ public void testConfigureRequestMavenOpts()

props.clear();

facade.setDefaultMavenOpts( "-Xxx" );
facade.configureInvocation( request, 0 );
verify( request ).setMavenOpts( "-Xxx" );
verify( request ).setMavenOpts( "-XxxDef" );
verifyNoMoreInteractions( request );

}

@Test
Expand Down

0 comments on commit fbe2481

Please sign in to comment.