Skip to content

XML Usage

Luiz-Micci edited this page Nov 13, 2024 · 16 revisions

Configuration Example

In this example, we configure replacements for XML files, specifying the paths and placeholder details. Here’s a breakdown of the configuration structure and its elements:

YAML Configuration

steps:
  - kind: XmlHandler
    apiVersion: v1
    spec:
      - files:
          - pom.xml
        placeholders:
          - match: /project/groupId
            replace: cookiecutter.param.groupId
          - match: /project/artifactId
            replace: Cookiecutter.test.replace.map.artifactId
          - match: /project/dependencies/dependency/scope[text()='test']
            replace: cookiecutter.replace.map.scopes
      - files:
          - xmls/generic_1.xml
        placeholders:
          - match: /note/heading
            replace: New Reminder
      - files:
          - xmls/complex/generic_2.xml
        placeholders:
          - match: /bookstore/book/author[text()='Kurt Cagle']
            replace: cookiecutter.kurtCagle
          - match: /bookstore/book/year[text()='2005']
            replace: cookiecutter.NewYear

Explanation of Key Sections

1. kind: XmlHandler

This defines the handler type as XmlHandler, specifying that the operations pertain to XML files.

2. apiVersion: v1

Indicates the version of the API for handling XML configurations. Ensure compatibility by matching the correct API version.

3. spec Section

Each element in the spec array defines a set of files to be processed along with specific placeholders.

Example Use Cases

pom.xml Replacements

The pom.xml file has several placeholders to automate common configurations:

  • /project/groupId: Replaces the project’s groupId with the value specified in cookiecutter.param.groupId.
  • /project/artifactId: Substitutes the artifactId with Cookiecutter.test.replace.map.artifactId.
  • /project/dependencies/dependency/scope[text()='test']: Replaces the test scope with cookiecutter.replace.map.scopes.

Custom XML Files

  1. xmls/generic_1.xml:

    • /note/heading: Updates the heading element within a <note> tag to "New Reminder".
  2. xmls/complex/generic_2.xml:

    • /bookstore/book/author[text()='Kurt Cagle']: Replaces occurrences of "Kurt Cagle" in the author tag with the value from cookiecutter.kurtCagle.
    • /bookstore/book/year[text()='2005']: Changes the year from "2005" to the new value in cookiecutter.NewYear.

Example project

You can see this example working here

Clone this wiki locally