Search Posts

Solved – The root cause why Netbeans can’t print colored text using latest Maven

Netbeans has bundled an old Maven, version 3.3.9, it can’t print out colored text to the output window. But when we switch to latest Maven 3.6.0, all color gone. I asked Netbeans guys, maven guys and jansi guys for help, but can’t get a solution. So i try to hack the maven code, i found out maven 3.6.0 embedded with jansi library, so i dive into jansi. In jansi website it clearly say that we can pass jansi.passthrough=true to force jansi to print out colored text. I also tried to set the MAVEN_OPTS variable but no help. When i print out the value in jansi code (jansi/jansi/src/main/java/org/fusesource/jansi/AnsiConsole.java), i got a false. I i hard-coded it and force it to get into the jansi.passthrough if-loop. Then i recompile my new maven with my modified jansi library. Finally everything works.

This is my testing code:

public class TestConsole {
	
	@Test
	public void test() throws Exception {
		
		String ANSI_RESET = "\u001B[0m";
		String ANSI_RED = "\u001B[31m";
		System.out.println(ANSI_RED + "XX");
		System.out.flush();
		System.out.println(ANSI_RESET);
		System.out.println("\033[31;1mHello\033[0m, \033[32;1;2mworld!\033[0m");
		System.out.println((char) 27 + "[32m" + "ERROR MESSAGE IN GREEN");
		System.out.println((char) 27 + "[33mYELLOW");

//		System.setProperty("jansi.force", "true");
		System.setProperty("jansi.passthrough", "true");
//		System.setProperty("jansi.strip", "true");
//		System.setProperty("jansi.force", "true");

//		AnsiConsole.systemInstall();
//		AnsiMain.main();
//		
//		System.out.println(ansi().eraseScreen().render("@|red Hello|@ @|green World|@"));
		System.out.println(ansi().fg(RED).a("Hello").fg(GREEN).a(" World").reset());
//		AnsiMain.main();
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *