* The request for new settings
*/
public Settings buildSettings(SettingsBuildingRequest request) {
SettingsBuildingResult result;
try {
SettingsBuilder builder = new DefaultSettingsBuilderFactory().newInstance();
if (request.getGlobalSettingsFile() != null) {
log.log(Level.FINE, "Using {0} to get global Maven settings.xml", request.getGlobalSettingsFile()
.getAbsolutePath());
}
final File userSettingsFile = request.getUserSettingsFile();
if (userSettingsFile != null) {
log.log(Level.FINE, "Using {0} to get user Maven settings.xml", userSettingsFile.getAbsolutePath());
// Maven will not check the format passed in (any XML will do), so let's ensure we have a
// settings.xml by checking just the top-level element
final XMLStreamReader reader;
try {
reader = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream(userSettingsFile));
// get the first element name
while (reader.hasNext()) {
if (reader.next() == XMLStreamConstants.START_ELEMENT) {
break;
}
}
final String topLevel = reader.getLocalName();
if (!"settings".equals(topLevel)) {
throw new InvalidConfigurationFileException("Invalid format settings.xml found: "
+ userSettingsFile);
}
} catch (final FileNotFoundException e) {
// Ignore at this level
} catch (final XMLStreamException xmlse) {
throw new RuntimeException("Could not check file format of specified settings.xml: "
+ userSettingsFile, xmlse);
}
}
result = builder.build(request);
}
// wrap exception message
catch (SettingsBuildingException e) {
StringBuilder sb = new StringBuilder("Found ").append(e.getProblems().size())
.append(" problems while building settings.xml model from both global Maven configuration file")