diff --git a/src/it/MINVOKER-365-user-poperties-from-pre-build/pom.xml b/src/it/MINVOKER-365-user-poperties-from-pre-build/pom.xml new file mode 100644 index 00000000..044b2f84 --- /dev/null +++ b/src/it/MINVOKER-365-user-poperties-from-pre-build/pom.xml @@ -0,0 +1,65 @@ + + + + + + 4.0.0 + + org.apache.maven.plugins.invoker.its + minvoker-335 + 1.0-SNAPSHOT + pom + + + Test to check a user properties generated by pre-build + + + + UTF-8 + @version.maven-enforcer-plugin@ + @version.maven-clean-plugin@ + + + + + + org.apache.maven.plugins + maven-invoker-plugin + @project.version@ + + + integration-test + initialize + + run + + + + + + clean + initialize + + + + + + + diff --git a/src/it/MINVOKER-365-user-poperties-from-pre-build/src/it/project/pom.xml b/src/it/MINVOKER-365-user-poperties-from-pre-build/src/it/project/pom.xml new file mode 100644 index 00000000..02d6e99a --- /dev/null +++ b/src/it/MINVOKER-365-user-poperties-from-pre-build/src/it/project/pom.xml @@ -0,0 +1,60 @@ + + + + + + 4.0.0 + org.apache.maven.plugins.invoker.its + minvoker-365 + 0.1-SNAPSHOT + pom + + + + + org.apache.maven.plugins + maven-clean-plugin + @version.maven-clean-plugin@ + + + org.apache.maven.plugins + maven-enforcer-plugin + @version.maven-enforcer-plugin@ + + + initialize + + enforce + + + + + + + + ${testFile} + + + + + + + + diff --git a/src/it/MINVOKER-365-user-poperties-from-pre-build/src/it/project/postbuild.groovy b/src/it/MINVOKER-365-user-poperties-from-pre-build/src/it/project/postbuild.groovy new file mode 100644 index 00000000..6491a73a --- /dev/null +++ b/src/it/MINVOKER-365-user-poperties-from-pre-build/src/it/project/postbuild.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. + */ + +def logs = new File(basedir, 'build.log').text + +assert logs.contains('[INFO] Clean is skipped.') diff --git a/src/it/MINVOKER-365-user-poperties-from-pre-build/src/it/project/prebuild.groovy b/src/it/MINVOKER-365-user-poperties-from-pre-build/src/it/project/prebuild.groovy new file mode 100644 index 00000000..ebb19e07 --- /dev/null +++ b/src/it/MINVOKER-365-user-poperties-from-pre-build/src/it/project/prebuild.groovy @@ -0,0 +1,26 @@ +import java.nio.file.Files + +/* + * 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 userProperties = context.get('userProperties') + +def testFilePath = Files.createTempFile('minvoker-365-prefix', 'suffix') +userProperties.put('testFile', testFilePath.toString()) +userProperties.put('maven.clean.skip', 'true') diff --git a/src/it/MINVOKER-365-user-poperties-from-pre-build/verify.groovy b/src/it/MINVOKER-365-user-poperties-from-pre-build/verify.groovy new file mode 100644 index 00000000..e82ee9ab --- /dev/null +++ b/src/it/MINVOKER-365-user-poperties-from-pre-build/verify.groovy @@ -0,0 +1,29 @@ +/* + * 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 logs = new File(basedir, 'build.log').text + +// check if properties are pass to execution +if (System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("windows")) { + assert logs.contains("-D testFile=") + assert logs.contains("-D maven.clean.skip=true") +} else { + assert logs.contains("'-D' 'testFile=") + assert logs.contains("'-D' 'maven.clean.skip=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 09987f4b..78b2d305 100644 --- a/src/main/java/org/apache/maven/plugins/invoker/AbstractInvokerMojo.java +++ b/src/main/java/org/apache/maven/plugins/invoker/AbstractInvokerMojo.java @@ -1838,24 +1838,15 @@ private boolean runBuild( } Map context = new LinkedHashMap<>(); + Properties scriptUserProperties = new Properties(); + context.put("userProperties", scriptUserProperties); - boolean selectorResult = true; + if (!runSelectorHook(basedir, context, logger)) { + return false; + } try { - try { - scriptRunner.run("selector script", basedir, selectorScript, context, logger); - } catch (ScriptReturnException e) { - selectorResult = false; - return false; - } catch (ScriptException e) { - throw new RunFailureException(BuildJob.Result.ERROR, e); - } - - try { - scriptRunner.run("pre-build script", basedir, preBuildHookScript, context, logger); - } catch (ScriptException e) { - throw new RunFailureException(BuildJob.Result.FAILURE_PRE_HOOK, e); - } + runPreBuildHook(basedir, context, logger); for (int invocationIndex = 1; ; invocationIndex++) { if (invocationIndex > 1 && !invokerProperties.isInvocationDefined(invocationIndex)) { @@ -1889,6 +1880,7 @@ private boolean runBuild( Properties userProperties = getUserProperties(basedir, invokerProperties.getUserPropertiesFile(invocationIndex)); + userProperties.putAll(scriptUserProperties); request.setProperties(userProperties); invokerProperties.configureInvocation(request, invocationIndex); @@ -1911,12 +1903,8 @@ private boolean runBuild( "Maven invocation failed. " + e.getMessage(), BuildJob.Result.FAILURE_BUILD); } } - } catch (IOException e) { - throw new MojoExecutionException(e.getMessage(), e); } finally { - if (selectorResult) { - runPostBuildHook(basedir, context, logger); - } + runPostBuildHook(basedir, context, logger); } return true; } @@ -1931,6 +1919,31 @@ int getParallelThreadsCount() { } } + private boolean runSelectorHook(File basedir, Map context, FileLogger logger) + throws MojoExecutionException, RunFailureException { + try { + scriptRunner.run("selector script", basedir, selectorScript, context, logger); + } catch (ScriptReturnException e) { + return false; + } catch (ScriptException e) { + throw new RunFailureException(BuildJob.Result.ERROR, e); + } catch (IOException e) { + throw new MojoExecutionException(e.getMessage(), e); + } + return true; + } + + private void runPreBuildHook(File basedir, Map context, FileLogger logger) + throws MojoExecutionException, RunFailureException { + try { + scriptRunner.run("pre-build script", basedir, preBuildHookScript, context, logger); + } catch (ScriptException e) { + throw new RunFailureException(BuildJob.Result.FAILURE_PRE_HOOK, e); + } catch (IOException e) { + throw new MojoExecutionException(e.getMessage(), e); + } + } + private void runPostBuildHook(File basedir, Map context, FileLogger logger) throws MojoExecutionException, RunFailureException { try { diff --git a/src/site/apt/examples/post-build-script.apt.vm b/src/site/apt/examples/post-build-script.apt.vm deleted file mode 100644 index 93c15a8c..00000000 --- a/src/site/apt/examples/post-build-script.apt.vm +++ /dev/null @@ -1,97 +0,0 @@ - ------ - Using a Post-Build Script - ------ - Paul Gier - ------ - 2008-08-02 - ------ - - ~~ 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. - - ~~ NOTE: For help with the syntax of this file, see: - ~~ http://maven.apache.org/doxia/references/apt-format.html - -Using a Post-Build Script - - Here is an example of how the Invoker Plugin can be used to run a set of Maven projects and then verify their output - with a {{{http://www.beanshell.org/}BeanShell}} or {{{http://groovy-lang.org/}Groovy}} script. The name of the - script file in this case is <<>>. - -+------------------ - - - - - maven-invoker-plugin - ${project.version} - - true - src/it - setup.bsh - verify.bsh - - - - integration-test - - run - - - - - - - -+------------------ - - Below is an example post-build BeanShell script (<<>>) that checks for the existence of a JAR file after - the build has run. If the JAR file does not exist, the script throws an exception which causes the Invoker Plugin to - log that the build failed. More precisely, any non-null return value which does not equal <<>> will be interpreted - as a failure condition. And of course, if the script exits abnormally due to an exception, the plugin will flag the - corresponding build as a failure, too. - -+------------------ -import java.io.*; - -File file = new File( basedir, "target/my-test-project-1.0-SNAPSHOT.jar" ); -if ( !file.isFile() ) -{ - throw new FileNotFoundException( "Could not find generated JAR: " + file ); -} -+------------------ - - Complementary to the post-build hook script, you can also create a pre-build hook script that will be run before - the invocation of Maven. This can be used to do some preparations for the build. - - To allow the scripts to access some useful data about the test project, the following global variables will be - defined by the Invoker Plugin before running the script: - -*--------------------------+----------------------+-----------------------------------------------------------+--------+ -|| Name || Type || Description || Since | -*--------------------------+----------------------+-----------------------------------------------------------+--------+ -| <<>> | <<>> | The absolute path to the base directory of the test project. | 1.0 | -*--------------------------+----------------------+-----------------------------------------------------------+--------+ -| <<>>| <<>> | The absolute path to the local repository used for the Maven invocation on the test project. | 1.3 | -*--------------------------+----------------------+-----------------------------------------------------------+--------+ -| <<>> | <<>> | The storage of key-value pairs used to pass data from the pre-build hook script to the post-build hook script. | 1.4 | -*--------------------------+----------------------+-----------------------------------------------------------+--------+ -| <<>> | <<>> | The version of Maven executing on the test project. | 1.9 | -*--------------------------+----------------------+-----------------------------------------------------------+--------+ - - Additional variables that can be accessed in the hook scripts can be defined through the - <<<{{{../run-mojo.html#scriptVariables}scriptVariables}}>>> parameter in the Invoker Plugin's configuration. \ No newline at end of file diff --git a/src/site/apt/examples/pre-post-build-script.apt.vm b/src/site/apt/examples/pre-post-build-script.apt.vm new file mode 100644 index 00000000..8a8e0948 --- /dev/null +++ b/src/site/apt/examples/pre-post-build-script.apt.vm @@ -0,0 +1,151 @@ + ------ + Using Pre and Post Build Script + ------ + Paul Gier + ------ + 2008-08-02 + ------ + + ~~ 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. + + ~~ NOTE: For help with the syntax of this file, see: + ~~ http://maven.apache.org/doxia/references/apt-format.html + +Using a Pre-Build Script + + Complementary to the post-build hook script, you can also create a pre-build hook script that will be run before + the invocation of Maven. This can be used to do some preparations for the build. + + Default name of Pre-Build script is <<>>, you can use name <<>> or <<>> + according to chosen script language. + + In Pre-Build scripts you can prepare a test resources for your project. + + You can also define a user properties which will be added to executed test project by <<<-D>>> Maven argument. + + Here is an example of groovy script: + ++------------------ +def userProperties = context.get('userProperties') + +def server = new MockServer() +userProperties.put('serverHost', server.getHost()) +userProperties.put('serverPort', server.getPort()) ++------------------ + + Now you can use it in test project: + ++------------------ + + + + + org.example + my-maven-plugin + @project.version@ + + ${serverHost}:${serverPort} + + + .... + + + + + ++------------------ + +Using a Post-Build Script + + Here is an example of how the Invoker Plugin can be used to run a set of Maven projects and then verify their output + with a {{{https://beanshell.github.io/}BeanShell}} or {{{https://groovy-lang.org/}Groovy}} script. The name of the + script file in this case is <<>> - default value is <<>>. + ++------------------ + + + + + maven-invoker-plugin + ${project.version} + + true + src/it + setup + verify + + + + integration-test + + run + + + + + + + ++------------------ + + Below is an example post-build BeanShell script (<<>>) that checks for the existence of a JAR file after + the build has run. If the JAR file does not exist, the script throws an exception which causes the Invoker Plugin to + log that the build failed. More precisely, any non-null return value which does not equal <<>> will be interpreted + as a failure condition. And of course, if the script exits abnormally due to an exception, the plugin will flag the + corresponding build as a failure, too. + + Example <<>> + ++------------------ +import java.io.*; + +File file = new File( basedir, "target/my-test-project-1.0-SNAPSHOT.jar" ); +if ( !file.isFile() ) +{ + throw new FileNotFoundException( "Could not find generated JAR: " + file ); +} ++------------------ + + and similar <<>> + ++------------------ +def file = new File( basedir, "target/my-test-project-1.0-SNAPSHOT.jar" ) +assert file.isFile() ++------------------ + +Predefined global variables + + To allow the scripts to access some useful data about the test project, the following global variables will be + defined by the Invoker Plugin before running the script: + +*-------------------------------------+----------------------------+-----------------------------------------------------------+--------+ +|| Name || Type || Description || Since | +*-------------------------------------+----------------------------+-----------------------------------------------------------+--------+ +| <<>> | <<>> | The absolute path to the base directory of the test project. | 1.0 | +*-------------------------------------+----------------------------+-----------------------------------------------------------+--------+ +| <<>> | <<>> | The absolute path to the local repository used for the Maven invocation on the test project. | 1.3 | +*-------------------------------------+----------------------------+-----------------------------------------------------------+--------+ +| <<>> | <<>> | The storage of key-value pairs used to pass data from the pre-build hook script to the post-build hook script. | 1.4 | +*-------------------------------------+----------------------------+-----------------------------------------------------------+--------+ +| <<>> | <<>> | The user properties for executing project | 3.7.0 | +*-------------------------------------+----------------------------+-----------------------------------------------------------+--------+ +| <<>> | <<>> | The version of Maven executing on the test project. | 1.9 | +*-------------------------------------+----------------------------+-----------------------------------------------------------+--------+ + + Additional variables that can be accessed in the hook scripts can be defined through the + <<<{{{../run-mojo.html#scriptVariables}scriptVariables}}>>> parameter in the Invoker Plugin's configuration. diff --git a/src/site/apt/examples/selector-scripts.apt.vm b/src/site/apt/examples/selector-scripts.apt.vm index 43072dfe..806a2223 100644 --- a/src/site/apt/examples/selector-scripts.apt.vm +++ b/src/site/apt/examples/selector-scripts.apt.vm @@ -85,4 +85,7 @@ Selector Scripts If the script returns any value other than <<>>, then the project will not be executed. If the script throws an exception, then the project will be marked as being in ERROR. - + +Predefined global variables + + In Selector Scripts the same global variables are defined as in {{{./pre-post-build-script.html#predefined-global-variables}Pre and Post Build Script}} diff --git a/src/site/apt/groovy-version.apt.vm b/src/site/apt/groovy-version.apt.vm index bc1d17f9..76a1aad6 100644 --- a/src/site/apt/groovy-version.apt.vm +++ b/src/site/apt/groovy-version.apt.vm @@ -56,7 +56,7 @@ Groovy version and extensions org.apache.groovy groovy-yaml - 3.0.10 + ${groovy-version} diff --git a/src/site/apt/index.apt.vm b/src/site/apt/index.apt.vm index a05187c4..bcb7e576 100644 --- a/src/site/apt/index.apt.vm +++ b/src/site/apt/index.apt.vm @@ -98,7 +98,7 @@ ${project.name} * {{{./examples/install-artifacts.html}Install}} projects artifacts to a local repository before running. - * {{{./examples/post-build-script.html}Run a BeanShell or Groovy script}} to verify project output. + * {{{./examples/pre-post-build-script.html}Run a BeanShell or Groovy script}} to prepare or verify project. * {{{./examples/fast-use.html}Fast Invoker Plugin configuration}} to accelerate project execution. diff --git a/src/site/apt/usage.apt.vm b/src/site/apt/usage.apt.vm index 8269569f..7562d05c 100644 --- a/src/site/apt/usage.apt.vm +++ b/src/site/apt/usage.apt.vm @@ -122,7 +122,7 @@ Usage When an integration test has finished, the plugin will invoke an optional post build hook script. In the example, this is the case for <<>> where <<>> will be run. The purpose of this script is usually to check that the build of the integration test did not only succeed but also produced the intended output. Have a look - at the example {{{./examples/post-build-script.html}Using a Post Build Script}} for a code snippet. + at the example {{{./examples/pre-post-build-script.html}Using Pre and Post Build Script}} for a code snippet. * Running Only Some Tests diff --git a/src/site/site.xml b/src/site/site.xml index 1b34bc66..a0932f76 100644 --- a/src/site/site.xml +++ b/src/site/site.xml @@ -48,7 +48,7 @@ under the License. - +