1 package org.apache.maven.profiles; 2 3 18 19 import org.apache.maven.profiles.io.xpp3.ProfilesXpp3Reader; 20 import org.codehaus.plexus.logging.AbstractLogEnabled; 21 import org.codehaus.plexus.util.IOUtil; 22 import org.codehaus.plexus.util.interpolation.EnvarBasedValueSource; 23 import org.codehaus.plexus.util.interpolation.RegexBasedInterpolator; 24 import org.codehaus.plexus.util.xml.pull.XmlPullParserException; 25 26 import java.io.File ; 27 import java.io.FileReader ; 28 import java.io.IOException ; 29 import java.io.StringReader ; 30 import java.io.StringWriter ; 31 32 public class DefaultMavenProfilesBuilder 33 extends AbstractLogEnabled 34 implements MavenProfilesBuilder 35 { 36 private static final String PROFILES_XML_FILE = "profiles.xml"; 37 38 public ProfilesRoot buildProfiles( File basedir ) 39 throws IOException , XmlPullParserException 40 { 41 File profilesXml = new File ( basedir, PROFILES_XML_FILE ); 42 43 ProfilesRoot profilesRoot = null; 44 45 if ( profilesXml.exists() ) 46 { 47 ProfilesXpp3Reader reader = new ProfilesXpp3Reader(); 48 FileReader fileReader = null; 49 try 50 { 51 fileReader = new FileReader ( profilesXml ); 52 53 StringWriter sWriter = new StringWriter (); 54 55 IOUtil.copy( fileReader, sWriter ); 56 57 String rawInput = sWriter.toString(); 58 59 try 60 { 61 RegexBasedInterpolator interpolator = new RegexBasedInterpolator(); 62 interpolator.addValueSource( new EnvarBasedValueSource() ); 63 64 rawInput = interpolator.interpolate( rawInput, "settings" ); 65 } 66 catch ( Exception e ) 67 { 68 getLogger().warn( "Failed to initialize environment variable resolver. Skipping environment substitution in " + PROFILES_XML_FILE + "." ); 69 getLogger().debug( "Failed to initialize envar resolver. Skipping resolution.", e ); 70 } 71 72 StringReader sReader = new StringReader ( rawInput ); 73 74 profilesRoot = reader.read( sReader ); 75 } 76 finally 77 { 78 IOUtil.close( fileReader ); 79 } 80 } 81 82 return profilesRoot; 83 } 84 85 } 86 | Popular Tags |