8

YAML file的配置名称为applications.yaml,这些数据将是我的绑定:

applications:
- name: service1
  port: 8080
  path: /servier1
- name: service2
  port: 8081
  path: /service2

然后我有一个模板文件applications.config

<% applications.each { application ->  %>
ApplicationName: <%= application.name %>
<% } $ %>

并将所有内容放在一起:

@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()

现在的问题是:这个过程的输出是:


ApplicationName: service1

ApplicationName: service2

但我想要这个:

ApplicationName: service1
ApplicationName: service2

我不知道为什么那里有那些额外的空间。我想删除那些,但我看不到这些新的或断线的放置方式、时间或内容。

谢谢你。

4

3 回答 3

3

我观察到的(来自答案、评论和一些实践)是:所有new Lines,我们在文件中创建的applications.config都被认为是new line在输出中。正如daggett所说,这是默认设置。

所以在这里我只想展示可能的config文件格式,可以conditional logic按照你的要求在哪里应用,对我来说看起来还可以。前任 :if()

应用程序配置:

<% applications.each { application ->
 if (application.valid) {%>\
Type :<%=application.valid%>
ApplicationName:<%=application.name%>
Path:<%=application.path%>
Port:<%=application.port%>
<%} else{%>\
--------------------
Found invalid Application : <%= application.name %>
--------------------\
<%}}%>

应用程序.yaml

applications:
- name: service1
  port: 8080
  path: /servier1
  valid: true
- name: service2
  port: 8081
  path: /service2
  valid: false

代码.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()

输出 :

Type :true
ApplicationName:service1
Path:/servier1
Port:8080
--------------------
Found invalid Application : service2
--------------------
于 2019-07-24T07:26:27.423 回答
2

例如从记事本++(JSP 语言)查看您的模板:

在此处输入图像描述

在第一行的末尾<% applications.each { application -> %>有一个CRLF将在每个之前打印出来Application

在下一行中,另一个CRLF将在每个Application.

所以你CRLF在每两个应用程序之间有两个

于 2019-07-23T15:29:56.710 回答
0

避免换行符的唯一方法是修复您的模板,如下所示

<% applications.each { application ->  %>ApplicationName: <%= application.name %>
<% } $ %>

结果

$ groovy test.groovy
ApplicationName: service1
ApplicationName: service2

或处理代码中的换行符,如下所示

@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)
def output = template.toString().replaceAll(/^\n|\n$/, "").replaceAll(/\n{2,}/,"\n")
println output

结果

$ groovy test.groovy
ApplicationName: service1
ApplicationName: service2
于 2019-07-24T14:17:18.170 回答