This is an automated email from the ASF dual-hosted git repository. slachiewicz pushed a commit to branch MINVOKER-255 in repository https://gitbox.apache.org/repos/asf/maven-invoker-plugin.git
commit 43c56b962978b52fc67a2b6f9fe3895481402335 Author: Slawomir Jaranowski <s.jaranow...@gmail.com> AuthorDate: Mon Feb 17 19:08:43 2020 +0100 [MINVOKER-255] support environmentVariable in invokerPropertiesFile Closes #14 --- src/it/invocation-environmentvariable/pom.xml | 1 + .../src/it/minvoker-145/pom.xml | 1 + .../src/it/minvoker-255/invoker.properties | 24 ++++ .../{ => src/it/minvoker-255}/pom.xml | 125 ++++++++++----------- .../maven/plugins/invoker/AbstractInvokerMojo.java | 6 + .../maven/plugins/invoker/InvokerProperties.java | 92 +++++++++++---- .../plugins/invoker/InvokerPropertiesTest.java | 48 +++++++- 7 files changed, 204 insertions(+), 93 deletions(-) diff --git a/src/it/invocation-environmentvariable/pom.xml b/src/it/invocation-environmentvariable/pom.xml index f6646b7..d7d7f42 100644 --- a/src/it/invocation-environmentvariable/pom.xml +++ b/src/it/invocation-environmentvariable/pom.xml @@ -46,6 +46,7 @@ under the License. </goals> <environmentVariables> <MINVOKER145>foobar</MINVOKER145> + <MINVOKER255_POM>minvoker255_pom</MINVOKER255_POM> </environmentVariables> </configuration> <executions> diff --git a/src/it/invocation-environmentvariable/src/it/minvoker-145/pom.xml b/src/it/invocation-environmentvariable/src/it/minvoker-145/pom.xml index f3c3b04..340b0cb 100644 --- a/src/it/invocation-environmentvariable/src/it/minvoker-145/pom.xml +++ b/src/it/invocation-environmentvariable/src/it/minvoker-145/pom.xml @@ -25,6 +25,7 @@ <profiles> <profile> + <id>envTest</id> <activation> <property> <name>env.MINVOKER145</name> diff --git a/src/it/invocation-environmentvariable/src/it/minvoker-255/invoker.properties b/src/it/invocation-environmentvariable/src/it/minvoker-255/invoker.properties new file mode 100644 index 0000000..2be833f --- /dev/null +++ b/src/it/invocation-environmentvariable/src/it/minvoker-255/invoker.properties @@ -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. + +invoker.name = Test Environment variable + +# override value from plugin config +invoker.environmentVariables.MINVOKER145 = minvoker_145 + +# add next value +invoker.environmentVariables.MINVOKER255_PROPERTIES = minvoker255_properties diff --git a/src/it/invocation-environmentvariable/pom.xml b/src/it/invocation-environmentvariable/src/it/minvoker-255/pom.xml similarity index 62% copy from src/it/invocation-environmentvariable/pom.xml copy to src/it/invocation-environmentvariable/src/it/minvoker-255/pom.xml index f6646b7..9ab12be 100644 --- a/src/it/invocation-environmentvariable/pom.xml +++ b/src/it/invocation-environmentvariable/src/it/minvoker-255/pom.xml @@ -1,64 +1,61 @@ -<?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>invocation-environmentvariable</artifactId> - <version>1.0-SNAPSHOT</version> - <packaging>pom</packaging> - - <description>Test if the environmentvariable is applied</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> - <configuration> - <debug>true</debug> - <goals> - <goal>validate</goal> - </goals> - <environmentVariables> - <MINVOKER145>foobar</MINVOKER145> - </environmentVariables> - </configuration> - <executions> - <execution> - <id>integration-test</id> - <phase>initialize</phase> - <goals> - <goal>run</goal> - </goals> - </execution> - </executions> - </plugin> - </plugins> - </build> - -</project> +<?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-145</artifactId> + <version>0.1-SNAPSHOT</version> + <packaging>pom</packaging> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-enforcer-plugin</artifactId> + <version>3.0.0-M3</version> + <executions> + <execution> + <goals> + <goal>enforce</goal> + </goals> + <configuration> + <rules> + <evaluateBeanshell> + <condition><![CDATA[ + "${env.MINVOKER145}" == "minvoker_145" && + "${env.MINVOKER255_POM}" == "minvoker255_pom" && + "${env.MINVOKER255_PROPERTIES}" == "minvoker255_properties" + ]]></condition> + </evaluateBeanshell> + </rules> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> +</project> 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 6a5c879..deec2cc 100644 --- a/src/main/java/org/apache/maven/plugins/invoker/AbstractInvokerMojo.java +++ b/src/main/java/org/apache/maven/plugins/invoker/AbstractInvokerMojo.java @@ -573,6 +573,12 @@ public abstract class AbstractInvokerMojo * # Since plugin version 3.2.1 * invoker.ordinal = 3 * invoker.ordinal = 1 + * + * # The additional value for the environment variable. + * # Since plugin version 3.2.2 + * invoker.environmentVariables.<variableName> = variableValue + * invoker.environmentVariables.MY_ENV_NAME = myEnvValue + * * </pre> * * @since 1.2 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 a3b8f3c..8dd7742 100644 --- a/src/main/java/org/apache/maven/plugins/invoker/InvokerProperties.java +++ b/src/main/java/org/apache/maven/plugins/invoker/InvokerProperties.java @@ -42,6 +42,9 @@ class InvokerProperties { private static final String SELECTOR_PREFIX = "selector."; + private static final Pattern ENVIRONMENT_VARIABLES_PATTERN = + Pattern.compile( "invoker\\.environmentVariables\\.([A-Za-z][^.]+)(\\.([0-9]+))?" ); + private enum InvocationProperty { PROJECT( "invoker.project" ), @@ -53,9 +56,9 @@ class InvokerProperties OFFLINE( "invoker.offline" ), SYSTEM_PROPERTIES_FILE( "invoker.systemPropertiesFile" ), DEBUG( "invoker.debug" ), - SETTINGS_FILE ( "invoker.settingsFile" ), - TIMEOUT_IN_SECONDS ( "invoker.timeoutInSeconds" ), - ORDINAL ( "invoker.ordinal" ); + SETTINGS_FILE( "invoker.settingsFile" ), + TIMEOUT_IN_SECONDS( "invoker.timeoutInSeconds" ), + ORDINAL( "invoker.ordinal" ); private final String key; @@ -70,20 +73,20 @@ class InvokerProperties return key; } } - + private enum SelectorProperty { JAVA_VERSION( ".java.version" ), MAVEN_VERSION( ".maven.version" ), OS_FAMLY( ".os.family" ); - + private final String suffix; - + SelectorProperty( String suffix ) { this.suffix = suffix; } - + @Override public String toString() { @@ -139,6 +142,7 @@ class InvokerProperties /** * Get the corresponding ordinal value + * * @return The ordinal value */ public int getOrdinal() @@ -163,8 +167,8 @@ class InvokerProperties */ public String getJreVersion( int index ) { - return this.properties.getProperty( SELECTOR_PREFIX + index + SelectorProperty.JAVA_VERSION.suffix, - getJreVersion() ); + return this.properties.getProperty( SELECTOR_PREFIX + index + SelectorProperty.JAVA_VERSION, + getJreVersion() ); } /** @@ -177,17 +181,16 @@ class InvokerProperties { return this.properties.getProperty( "invoker.maven.version", "" ); } - + /** - * * @param index the selector index * @return The specification of Maven versions on which this build job should be run. * @since 3.0.0 */ public String getMavenVersion( int index ) { - return this.properties.getProperty( SELECTOR_PREFIX + index + SelectorProperty.MAVEN_VERSION.suffix, - getMavenVersion() ); + return this.properties.getProperty( SELECTOR_PREFIX + index + SelectorProperty.MAVEN_VERSION, + getMavenVersion() ); } /** @@ -199,7 +202,7 @@ class InvokerProperties { return this.properties.getProperty( "invoker.os.family", "" ); } - + /** * Gets the specification of OS families on which this build job should be run. * @@ -209,18 +212,17 @@ class InvokerProperties */ public String getOsFamily( int index ) { - return this.properties.getProperty( SELECTOR_PREFIX + index + SelectorProperty.OS_FAMLY.suffix, - getOsFamily() ); + return this.properties.getProperty( SELECTOR_PREFIX + index + SelectorProperty.OS_FAMLY, getOsFamily() ); } - + public Collection<InvokerToolchain> getToolchains() { - return getToolchains( Pattern.compile( "invoker\\.toolchain\\.([^\\.]+)\\.(.+)" ) ); + return getToolchains( Pattern.compile( "invoker\\.toolchain\\.([^.]+)\\.(.+)" ) ); } public Collection<InvokerToolchain> getToolchains( int index ) { - return getToolchains( Pattern.compile( "selector\\." + index + "\\.invoker\\.toolchain\\.([^\\.]+)\\.(.+)" ) ); + return getToolchains( Pattern.compile( "selector\\." + index + "\\.invoker\\.toolchain\\.([^.]+)\\.(.+)" ) ); } private Collection<InvokerToolchain> getToolchains( Pattern p ) @@ -248,6 +250,43 @@ class InvokerProperties } /** + * Extract environment variable from properties for given index. + * Every environment variable without index is also returned. + * + * @param index index to lookup + * @return map of environment name and value + */ + + private Map<String, String> getEnvironmentVariables( int index ) + { + + Map<String, String> envItems = new HashMap<>(); + + for ( Map.Entry<Object, Object> entry : properties.entrySet() ) + { + Matcher matcher = ENVIRONMENT_VARIABLES_PATTERN.matcher( entry.getKey().toString() ); + if ( matcher.matches() ) + { + + if ( String.valueOf( index ).equals( matcher.group( 3 ) ) ) + { + // variables with index has higher priority, so override + envItems.put( matcher.group( 1 ), entry.getValue().toString() ); + } + else if ( matcher.group( 3 ) == null ) + { + // variables without index has lower priority, so check if exist + if ( !envItems.containsKey( matcher.group( 1 ) ) ) + { + envItems.put( matcher.group( 1 ), entry.getValue().toString() ); + } + } + } + } + return envItems; + } + + /** * Determines whether these invoker properties contain a build definition for the specified invocation index. * * @param index The one-based index of the invocation to check for, must not be negative. @@ -264,10 +303,10 @@ class InvokerProperties } return false; } - + /** * Determines whether these invoker properties contain a build definition for the specified selector index. - * + * * @param index the index * @return <code>true</code> if the selector with the specified index is defined, <code>false</code> otherwise. * @since 3.0.0 @@ -341,19 +380,24 @@ class InvokerProperties String nonRecursive = get( InvocationProperty.NON_RECURSIVE, index ); if ( nonRecursive != null ) { - request.setRecursive( !Boolean.valueOf( nonRecursive ) ); + request.setRecursive( !Boolean.parseBoolean( nonRecursive ) ); } String offline = get( InvocationProperty.OFFLINE, index ); if ( offline != null ) { - request.setOffline( Boolean.valueOf( offline ) ); + request.setOffline( Boolean.parseBoolean( offline ) ); } String debug = get( InvocationProperty.DEBUG, index ); if ( debug != null ) { - request.setDebug( Boolean.valueOf( debug ) ); + request.setDebug( Boolean.parseBoolean( debug ) ); + } + + for ( Map.Entry<String, String> envItem : getEnvironmentVariables( index ).entrySet() ) + { + request.addShellEnvironment( envItem.getKey(), envItem.getValue() ); } } 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 fe9349c..f4aebb0 100644 --- a/src/test/java/org/apache/maven/plugins/invoker/InvokerPropertiesTest.java +++ b/src/test/java/org/apache/maven/plugins/invoker/InvokerPropertiesTest.java @@ -309,6 +309,44 @@ public class InvokerPropertiesTest } @Test + public void testConfigureEnvironmentVariables() throws Exception + { + Properties props = new Properties(); + InvokerProperties facade = new InvokerProperties( props ); + + props.setProperty( "invoker.abcdef", "abcdf" ); + props.setProperty( "invoker.environmentVariables.KEY1.1", "value1.1" ); + props.setProperty( "invoker.environmentVariables.KEY1", "value1" ); + props.setProperty( "invoker.environmentVariables.KEY2", "value2" ); + props.setProperty( "invoker.environmentVariables.KEY2.1", "value2.1" ); + props.setProperty( "invoker.environmentVariables.KEY3", "value3" ); + facade.configureInvocation( request, 0 ); + verify( request ).addShellEnvironment( "KEY1", "value1" ); + verify( request ).addShellEnvironment( "KEY2", "value2" ); + verify( request ).addShellEnvironment( "KEY3", "value3" ); + verifyNoMoreInteractions( request ); + } + + @Test + public void testConfigureEnvironmentVariablesWithIndex() throws Exception + { + Properties props = new Properties(); + InvokerProperties facade = new InvokerProperties( props ); + + props.setProperty( "invoker.abcdef", "abcdf" ); + props.setProperty( "invoker.environmentVariables.KEY1.1", "value1.1" ); + props.setProperty( "invoker.environmentVariables.KEY1", "value1" ); + props.setProperty( "invoker.environmentVariables.KEY2", "value2" ); + props.setProperty( "invoker.environmentVariables.KEY2.1", "value2.1" ); + props.setProperty( "invoker.environmentVariables.KEY3", "value3" ); + facade.configureInvocation( request, 1 ); + verify( request ).addShellEnvironment( "KEY1", "value1.1" ); + verify( request ).addShellEnvironment( "KEY2", "value2.1" ); + verify( request ).addShellEnvironment( "KEY3", "value3" ); + verifyNoMoreInteractions( request ); + } + + @Test public void testIsInvocationDefined() throws Exception { Properties props = new Properties(); @@ -382,8 +420,8 @@ public class InvokerPropertiesTest assertNotNull( toolchains ); assertEquals( 1, toolchains.size() ); InvokerToolchain toolchain = toolchains.iterator().next(); - assertEquals( "jdk", toolchain.getType()); - assertEquals( Collections.singletonMap( "version", "11" ), toolchain.getProvides()); + assertEquals( "jdk", toolchain.getType() ); + assertEquals( Collections.singletonMap( "version", "11" ), toolchain.getProvides() ); } @Test @@ -394,12 +432,12 @@ public class InvokerPropertiesTest props.put( "selector.1.invoker.toolchain.jdk.version", "11" ); InvokerProperties facade = new InvokerProperties( props ); - Collection<InvokerToolchain> toolchains = facade.getToolchains(1); + Collection<InvokerToolchain> toolchains = facade.getToolchains( 1 ); assertNotNull( toolchains ); assertEquals( 1, toolchains.size() ); InvokerToolchain toolchain = toolchains.iterator().next(); - assertEquals( "jdk", toolchain.getType()); - assertEquals( Collections.singletonMap( "version", "11" ), toolchain.getProvides()); + assertEquals( "jdk", toolchain.getType() ); + assertEquals( Collections.singletonMap( "version", "11" ), toolchain.getProvides() ); } }