5

I followed the suggestions from this post on how to use the EnvInject plugin to create and set Jenkins environment variables. I'm using the "Inject environment variables" in post build step and set "Properties File Path"

The windows batch script create a environment variable OPS and writes it to a property file : results.txt which contains multiple lines , like :

OPS= This is line one,
This is two
This is three 

Challenge: OPS picks up only the first line from results.txt and skips the rest of the lines.

How do I set OPS have all the lines as its value ?

cd C:\To\Test\Class\Path
java utilities.LogExtractor>ops.txt
@echo off
setlocal EnableDelayedExpansion
set LF=^


rem *** Two empty lines are required for the linefeed
FOR /F "delims=" %%a in (ops.txt) do (
  set var=!var!!LF!%%a
)
set var=!var!!LF!
echo OPS=!var! > %JENKINS_HOME%\jobs\%JOB_NAME%\builds\%BUILD_NUMBER%\results.txt

and I set the "Properties File Path" to %JENKINS_HOME%\jobs\%JOB_NAME%\builds\%BUILD_NUMBER%\results.txt

Alferd Nobel
  • 3,185
  • 2
  • 30
  • 35

1 Answers1

2

From the source code, I'd say it uses java.util.Properties to load the file, calling the load method. The documentation says you can escape a line break with a backslash, so using

OPS= This is line one,\
This is two\
This is three 

should be sufficient. (Be careful, whitespace at the beginning of the line is ommitted.)

phi1010
  • 678
  • 6
  • 13
  • Thanks for pointing "\" to escape the line break, which populates the OPS with all the content without line breaks. But I want formatted content , which includes the lines breaks, as I want to include it in the Email Body . Any inputs ? – Alferd Nobel Sep 20 '17 at 17:39
  • 1
    Mh, I missed the fact that line terminators also being discarded. You could try terminating the lines with `\n\ ` or `\r\n\ ` instead. ("Characters in keys and elements can be represented in escape sequences similar to those used for character and string literals (see sections 3.3 and 3.10.6 of The Java™ Language Specification).") – phi1010 Sep 20 '17 at 21:24
  • (You probably also have to escape any other backslash in your input to avoid unexpected behaviour) – phi1010 Sep 20 '17 at 21:31