View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.profiles;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.Reader;
24  import java.io.StringReader;
25  import java.io.StringWriter;
26  
27  import org.apache.maven.profiles.io.xpp3.ProfilesXpp3Reader;
28  import org.codehaus.plexus.component.annotations.Component;
29  import org.codehaus.plexus.interpolation.EnvarBasedValueSource;
30  import org.codehaus.plexus.interpolation.RegexBasedInterpolator;
31  import org.codehaus.plexus.logging.AbstractLogEnabled;
32  import org.codehaus.plexus.util.IOUtil;
33  import org.codehaus.plexus.util.ReaderFactory;
34  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
35  
36  /**
37   * DefaultMavenProfilesBuilder
38   */
39  @Deprecated
40  @Component(role = MavenProfilesBuilder.class)
41  public class DefaultMavenProfilesBuilder extends AbstractLogEnabled implements MavenProfilesBuilder {
42      private static final String PROFILES_XML_FILE = "profiles.xml";
43  
44      public ProfilesRoot buildProfiles(File basedir) throws IOException, XmlPullParserException {
45          File profilesXml = new File(basedir, PROFILES_XML_FILE);
46  
47          ProfilesRoot profilesRoot = null;
48  
49          if (profilesXml.exists()) {
50              ProfilesXpp3Reader reader = new ProfilesXpp3Reader();
51              try (Reader profileReader = ReaderFactory.newXmlReader(profilesXml);
52                      StringWriter sWriter = new StringWriter()) {
53                  IOUtil.copy(profileReader, sWriter);
54  
55                  String rawInput = sWriter.toString();
56  
57                  try {
58                      RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
59                      interpolator.addValueSource(new EnvarBasedValueSource());
60  
61                      rawInput = interpolator.interpolate(rawInput, "settings");
62                  } catch (Exception e) {
63                      getLogger()
64                              .warn("Failed to initialize environment variable resolver. Skipping environment "
65                                      + "substitution in " + PROFILES_XML_FILE + ".");
66                      getLogger().debug("Failed to initialize envar resolver. Skipping resolution.", e);
67                  }
68  
69                  StringReader sReader = new StringReader(rawInput);
70  
71                  profilesRoot = reader.read(sReader);
72              }
73          }
74  
75          return profilesRoot;
76      }
77  }