You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by GitBox <gi...@apache.org> on 2019/06/02 13:39:13 UTC

[GitHub] [maven] MartinKanters commented on a change in pull request #251: [MNG-6665] toolchain.xml file should support environment variables

MartinKanters commented on a change in pull request #251: [MNG-6665] toolchain.xml file should support environment variables
URL: https://github.com/apache/maven/pull/251#discussion_r289643063
 
 

 ##########
 File path: maven-core/src/main/java/org/apache/maven/toolchain/building/DefaultToolchainsBuilder.java
 ##########
 @@ -66,16 +75,86 @@ public ToolchainsBuildingResult build( ToolchainsBuildingRequest request )
         toolchainsMerger.merge( userToolchains, globalToolchains, TrackableBase.GLOBAL_LEVEL );
         
         problems.setSource( "" );
-        
+
+        userToolchains = interpolate( userToolchains, problems );
+
         if ( hasErrors( problems.getProblems() ) )
         {
             throw new ToolchainsBuildingException( problems.getProblems() );
         }
-        
-        
+
+
         return new DefaultToolchainsBuildingResult( userToolchains, problems.getProblems() );
     }
 
+    private PersistedToolchains interpolate( PersistedToolchains toolchains, ProblemCollector problems )
+    {
+
+        StringWriter stringWriter = new StringWriter( 1024 * 4 );
+        try
+        {
+            toolchainsWriter.write( stringWriter, null, toolchains );
+        }
+        catch ( IOException e )
+        {
+            throw new IllegalStateException( "Failed to serialize toolchains to memory", e );
+        }
+
+        String serializedToolchains = stringWriter.toString();
+
+        RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
+
+        try
+        {
+            interpolator.addValueSource( new EnvarBasedValueSource() );
+        }
+        catch ( IOException e )
+        {
+            problems.add( Problem.Severity.WARNING, "Failed to use environment variables for interpolation: "
+                    + e.getMessage(), -1, -1, e );
+        }
+
+        interpolator.addPostProcessor( new InterpolationPostProcessor()
+        {
+            @Override
+            public Object execute( String expression, Object value )
+            {
+                if ( value != null )
+                {
+                    // we're going to parse this back in as XML so we need to escape XML markup
+                    value = value.toString().replace( "&", "&amp;" ).replace( "<", "&lt;" ).replace( ">", "&gt;" );
 
 Review comment:
   This logic is taken from the DefaultSettingsBuilder, if it already exists in one of the libraries I can change both places. 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services