0

I am working on a tool to automate mass-updating several pom files and pipeline files.

The pom files I am able to update and successfully push to bitbucket but am having an issue updating the yaml file as I don't see much of an option out there to update yaml files directly.

The pipeline yaml we have are quite big and I am wondering if there is a way to keep the same format of the existing pipeline file, sample :

build:
  property1: .
  property2: artifactory
  property3: false
deploy:
  property1: test
  property2: test2
  env:
    property1: true
    property2: '"template"'

I am able to successfully update the above yaml using the below code :

    ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());

    Map<String, Object> pipeline = objectMapper.readValue(
            new File(DEFAULT_DIR + serviceName + "\\pipeline.yml"),
            new TypeReference<Map<String, Object>>() {
            });

    Map<String, Object> build = (Map<String, Object>) pipeline.get("build");
    build.put("property1", "d");
    
    objectMapper.writeValue(new File(DEFAULT_DIR + serviceName + "\\pipeline.yml"), pipeline);

After writeValue creates the new yaml file, we see quotes around all the properties in the new yaml. I assume this is from the writeValue method from ObjectMapper.

build:
  property1: "."
  property2: "artifactory"
  property3: false
deploy:
  property1: "test"
  property2: "test2"
  env:
    property1: true
    property2: "\"template\""

Is this possible to keep the initial format of the yaml file without the quotes and the "\" \"" we are seeing after objectMapper writes the file? We have specific requirements for this layout in order for our Jenkins builds to work correctly.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Criel
  • 805
  • 1
  • 14
  • 32
  • 2
    Valid yaml is valid yaml. The actual issue seems to be your Jenkins build, which doesn't accept yaml with a different format although it's actually valid yaml – maja Oct 07 '20 at 14:47
  • Does this answer your question? [I want to load a YAML file, possibly edit the data, and then dump it again. How can I preserve formatting?](https://stackoverflow.com/questions/60891174/i-want-to-load-a-yaml-file-possibly-edit-the-data-and-then-dump-it-again-how) – flyx Oct 08 '20 at 07:17

0 Answers0