I have YAML file
with my configuration name applications.yaml, this data will be my bindings:
applications:
- name: service1
port: 8080
path: /servier1
- name: service2
port: 8081
path: /service2
Then I have a template file applications.config:
<% applications.each { application -> %>
ApplicationName: <%= application.name %>
<% } $ %>
And putting all together:
@Grab('org.yaml:snakeyaml:1.17')
import org.yaml.snakeyaml.Yaml
Yaml parser = new Yaml()
Map data = parser.load(("applications.yaml" as File).text)
String template_content = new File('applications.config').text
def binding = [applications: data.applications]
def template = new groovy.text.GStringTemplateEngine().createTemplate(template_content).make(binding)
println template.toString()
The issues is now: the output of this process is:
ApplicationName: service1
ApplicationName: service2
But I want this:
ApplicationName: service1
ApplicationName: service2
I do not know why are those extra spaces there. I will like to remove those but I do not see how or when or what is putting those new or breaking lines.
Thank you.
What I observed (from answer, comments and some practice) is : All new Lines
, we created inside the file applications.config
are considered as new line
in the output. As daggett said it is a default thing.
So Here I just want to show the possible config
file format, where can be applied some conditional logic
as u asked and looks ok for me. ex : if()
application.config :
<% applications.each { application ->
if (application.valid) {%>\
Type :<%=application.valid%>
ApplicationName:<%=application.name%>
Path:<%=application.path%>
Port:<%=application.port%>
<%} else{%>\
--------------------
Found invalid Application : <%= application.name %>
--------------------\
<%}}%>
application.yaml
applications:
- name: service1
port: 8080
path: /servier1
valid: true
- name: service2
port: 8081
path: /service2
valid: false
code.groovy
@Grab('org.yaml:snakeyaml:1.17')
import org.yaml.snakeyaml.Yaml
Yaml parser = new Yaml()
Map data = parser.load(("applications.yaml" as File).text)
String template_content = new File('applications.config').text
def binding = [applications: data.applications]
def template = new groovy.text.GStringTemplateEngine().createTemplate(template_content).make(binding)
println template.toString()
Output :
Type :true
ApplicationName:service1
Path:/servier1
Port:8080
--------------------
Found invalid Application : service2
--------------------
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With