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