From c4f756daee43f89e0ba832ceac17bac216fc899b Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 18 Jan 2016 13:18:03 +0000 Subject: [PATCH] =?UTF-8?q?Fix=20MavenSettings=E2=80=99=20handling=20of=20?= =?UTF-8?q?profiles=20activated=20by=20a=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../cli/compiler/maven/MavenSettings.java | 42 +++++++++++++++++-- .../maven-settings/basic/.m2/settings.xml | 19 ++++++++- 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/maven/MavenSettings.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/maven/MavenSettings.java index 08ca2a6382d..abf05c6447c 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/maven/MavenSettings.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/maven/MavenSettings.java @@ -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()); diff --git a/spring-boot-cli/src/test/resources/maven-settings/basic/.m2/settings.xml b/spring-boot-cli/src/test/resources/maven-settings/basic/.m2/settings.xml index b2b97db3302..5439ce8cfb8 100644 --- a/spring-boot-cli/src/test/resources/maven-settings/basic/.m2/settings.xml +++ b/spring-boot-cli/src/test/resources/maven-settings/basic/.m2/settings.xml @@ -28,4 +28,21 @@ - \ No newline at end of file + + + test-profile + + + ${user.home}/.m2/some_file + + + + + example-repository + http://repo.example.com + + + + + +