Fix MavenSettings’ handling of profiles activated by a file

Previously, MavenSettings used a FileProfileActivator with no
PathTransformer. If a settings.xml file contains a file-activated
profile this would result in an NPE within Maven. This was made worse
by the NPE not being included in the resulting failure message which
hampered diagnosis of the problem.

This commit updates MavenSettings to configure its FileProfileActivator
with a PathTransformer. It also improves the failure message that’s
created from any problems that are reported by Maven while determining
the active profiles to include a problem’s exception if it has one.

Closes gh-4826
pull/4896/merge
Andy Wilkinson 9 years ago
parent 7d975ecd2e
commit c4f756daee

@ -16,8 +16,11 @@
package org.springframework.boot.cli.compiler.maven;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collections;
@ -29,6 +32,7 @@ import org.apache.maven.model.ActivationOS;
import org.apache.maven.model.ActivationProperty;
import org.apache.maven.model.building.ModelProblemCollector;
import org.apache.maven.model.building.ModelProblemCollectorRequest;
import org.apache.maven.model.path.DefaultPathTranslator;
import org.apache.maven.model.profile.DefaultProfileSelector;
import org.apache.maven.model.profile.ProfileActivationContext;
import org.apache.maven.model.profile.activation.FileProfileActivator;
@ -147,15 +151,47 @@ public class MavenSettings {
PrintWriter printer = new PrintWriter(message);
printer.println("Failed to determine active profiles:");
for (ModelProblemCollectorRequest problem : problemCollector.getProblems()) {
printer.println(
" " + problem.getMessage() + " at " + problem.getLocation());
printer.println(" " + problem.getMessage() + (problem.getLocation() != null
? " at " + problem.getLocation() : ""));
if (problem.getException() != null) {
printer.println(indentStackTrace(problem.getException(), " "));
}
}
return message.toString();
}
private String indentStackTrace(Exception ex, String indent) {
return indentLines(printStackTrace(ex), indent);
}
private String printStackTrace(Exception ex) {
StringWriter stackTrace = new StringWriter();
PrintWriter printer = new PrintWriter(stackTrace);
ex.printStackTrace(printer);
return stackTrace.toString();
}
private String indentLines(String input, String indent) {
StringWriter indented = new StringWriter();
PrintWriter writer = new PrintWriter(indented);
String line;
BufferedReader reader = new BufferedReader(new StringReader(input));
try {
while ((line = reader.readLine()) != null) {
writer.println(indent + line);
}
}
catch (IOException ex) {
return input;
}
return indented.toString();
}
private DefaultProfileSelector createProfileSelector() {
DefaultProfileSelector selector = new DefaultProfileSelector();
selector.addProfileActivator(new FileProfileActivator());
selector.addProfileActivator(new FileProfileActivator()
.setPathTranslator(new DefaultPathTranslator()));
selector.addProfileActivator(new JdkVersionProfileActivator());
selector.addProfileActivator(new PropertyProfileActivator());
selector.addProfileActivator(new OperatingSystemProfileActivator());

@ -28,4 +28,21 @@
</proxy>
</proxies>
</settings>
<profiles>
<profile>
<id>test-profile</id>
<activation>
<file>
<exists>${user.home}/.m2/some_file</exists>
</file>
</activation>
<repositories>
<repository>
<id>example-repository</id>
<url>http://repo.example.com</url>
</repository>
</repositories>
</profile>
</profiles>
</settings>

Loading…
Cancel
Save