Home > Groovy, Maven > Groovy/Maven – Multiline Strings

Groovy/Maven – Multiline Strings

Groovy multiline Strings are always using “\n” as line terminator and sometimes this may be a problem but a simple replace() solves it:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns              = "http://maven.apache.org/POM/4.0.0"
         xmlns:xsi          = "http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0
""".replace( "\n", "\r\n" )

I had this issue when ran the above multi-line replacement today (yeah, I’d like all POMs to have this nice header) – some files that already had this header were still modified by the script as it replaced “\r\n” that were previously in a file by “\n” – that’s when I had to add the replace() call.

Just something to keep in mind.

Update: same issue happens in Maven when it passes a multiline plugin’s configuration to Mojo (like replacement content between <to> tags) – it joins the lines with “\n” as well. If they’re to be written to a file – s.replace( "\n", System.getProperty( "line.separator" )) is required. Note that in Groovy version (above) I did not use System.getProperty() and that’s because we want all POMs have “\r\n” line-endings regardless of OS the script is running on (but we’re mostly using Windows anyway).

Categories: Groovy, Maven Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: