From 0b67f1c2ce44718c039fb1d451dae96834fb32d4 Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Sat, 14 May 2022 22:48:34 +0200 Subject: [PATCH] [MINVOKER-294] Allow override mavenExecutable in invoker.properties --- .../pom.xml | 57 +++++++++++++++++++ .../src/it/project/invoker.properties | 20 +++++++ .../src/it/project/mvn1 | 21 +++++++ .../src/it/project/mvn1.cmd | 21 +++++++ .../src/it/project/mvn2 | 21 +++++++ .../src/it/project/mvn2.cmd | 21 +++++++ .../src/it/project/pom.xml | 28 +++++++++ .../src/it/project/postbuild.groovy | 24 ++++++++ .../src/it/project/prebuild.groovy | 22 +++++++ .../plugins/invoker/AbstractInvokerMojo.java | 23 ++++---- .../plugins/invoker/InvokerProperties.java | 15 +++++ .../invoker/InvokerPropertiesTest.java | 28 ++++++++- 12 files changed, 286 insertions(+), 15 deletions(-) create mode 100644 src/it/MINVOKER-294-mavenExacutable-in-properties/pom.xml create mode 100644 src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/invoker.properties create mode 100644 src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/mvn1 create mode 100644 src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/mvn1.cmd create mode 100644 src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/mvn2 create mode 100644 src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/mvn2.cmd create mode 100644 src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/pom.xml create mode 100644 src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/postbuild.groovy create mode 100644 src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/prebuild.groovy diff --git a/src/it/MINVOKER-294-mavenExacutable-in-properties/pom.xml b/src/it/MINVOKER-294-mavenExacutable-in-properties/pom.xml new file mode 100644 index 00000000..7bb91d8f --- /dev/null +++ b/src/it/MINVOKER-294-mavenExacutable-in-properties/pom.xml @@ -0,0 +1,57 @@ + + + + + + 4.0.0 + + org.apache.maven.plugins.invoker.its + minvoker-294 + 1.0-SNAPSHOT + pom + + + Test to check for usage of mavenExecutable in invoker.properties. + + + + UTF-8 + + + + + + org.apache.maven.plugins + maven-invoker-plugin + @project.version@ + + + integration-test + initialize + + run + + + + + + + + diff --git a/src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/invoker.properties b/src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/invoker.properties new file mode 100644 index 00000000..9cf046a2 --- /dev/null +++ b/src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/invoker.properties @@ -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 diff --git a/src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/mvn1 b/src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/mvn1 new file mode 100644 index 00000000..2a9ca7ea --- /dev/null +++ b/src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/mvn1 @@ -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" diff --git a/src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/mvn1.cmd b/src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/mvn1.cmd new file mode 100644 index 00000000..a2615a30 --- /dev/null +++ b/src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/mvn1.cmd @@ -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 diff --git a/src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/mvn2 b/src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/mvn2 new file mode 100644 index 00000000..3fad8205 --- /dev/null +++ b/src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/mvn2 @@ -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" diff --git a/src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/mvn2.cmd b/src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/mvn2.cmd new file mode 100644 index 00000000..7fb138c3 --- /dev/null +++ b/src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/mvn2.cmd @@ -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 diff --git a/src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/pom.xml b/src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/pom.xml new file mode 100644 index 00000000..2ec89022 --- /dev/null +++ b/src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/pom.xml @@ -0,0 +1,28 @@ + + + + + + 4.0.0 + org.apache.maven.plugins.invoker.its + minvoker-294 + 0.1-SNAPSHOT + pom + diff --git a/src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/postbuild.groovy b/src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/postbuild.groovy new file mode 100644 index 00000000..2cde89bc --- /dev/null +++ b/src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/postbuild.groovy @@ -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" ) diff --git a/src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/prebuild.groovy b/src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/prebuild.groovy new file mode 100644 index 00000000..0ac20bba --- /dev/null +++ b/src/it/MINVOKER-294-mavenExacutable-in-properties/src/it/project/prebuild.groovy @@ -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 ) diff --git a/src/main/java/org/apache/maven/plugins/invoker/AbstractInvokerMojo.java b/src/main/java/org/apache/maven/plugins/invoker/AbstractInvokerMojo.java index 4ec0de16..74220aa9 100644 --- a/src/main/java/org/apache/maven/plugins/invoker/AbstractInvokerMojo.java +++ b/src/main/java/org/apache/maven/plugins/invoker/AbstractInvokerMojo.java @@ -415,13 +415,13 @@ public abstract class AbstractInvokerMojo private File mavenHome; /** - * mavenExecutable can either be a file relative to ${maven.home}/bin/ or an absolute file. + * mavenExecutable can either be a file relative to ${maven.home}/bin/, 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 JAVA_HOME environment variable to use for forked Maven invocations. Defaults to the current Java @@ -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 ${maven.home}/bin/, 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 @@ -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 ) ) @@ -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 ); @@ -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 ); diff --git a/src/main/java/org/apache/maven/plugins/invoker/InvokerProperties.java b/src/main/java/org/apache/maven/plugins/invoker/InvokerProperties.java index a057f8c3..ecebdcdd 100644 --- a/src/main/java/org/apache/maven/plugins/invoker/InvokerProperties.java +++ b/src/main/java/org/apache/maven/plugins/invoker/InvokerProperties.java @@ -54,6 +54,7 @@ class InvokerProperties private String defaultMavenOpts; private Integer defaultTimeoutInSeconds; private Map defaultEnvironmentVariables; + private File defaultMavenExecutable; private enum InvocationProperty { @@ -61,6 +62,7 @@ private enum InvocationProperty 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" ), @@ -147,6 +149,15 @@ public void setDefaultProfiles( List 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 @@ -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 ) diff --git a/src/test/java/org/apache/maven/plugins/invoker/InvokerPropertiesTest.java b/src/test/java/org/apache/maven/plugins/invoker/InvokerPropertiesTest.java index c380e207..1deeb116 100644 --- a/src/test/java/org/apache/maven/plugins/invoker/InvokerPropertiesTest.java +++ b/src/test/java/org/apache/maven/plugins/invoker/InvokerPropertiesTest.java @@ -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 ); @@ -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