rfscholte 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_r289618131
 
 

 ##########
 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( "&", "&" ).replace( 
"<", "&lt;" ).replace( ">", "&gt;" );
 
 Review comment:
   This line of code is pretty straight forward, at least not worth introducing 
a new library. Maybe one of the existing libraries has one.
   Actually, looking at it I wonder the performance penalty here since we loop 
over the whole file 3 times. But that kind of optimization deserves a new issue.

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to