-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement yaml2json and json2yaml (#3)
- Loading branch information
1 parent
c0163a1
commit 8241c35
Showing
4 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar | ||
import org.graalvm.buildtools.gradle.dsl.NativeImageOptions | ||
|
||
plugins { | ||
id 'info.ankin.projects.app-conventions' | ||
} | ||
|
||
version = '0.0.1' | ||
|
||
dependencies { | ||
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.13.2' | ||
} | ||
|
||
// for shadowJar | ||
application.mainClass.set 'info.ankin.projects.cli.yaml2json.Yaml2Json' | ||
|
||
shadowJar { | ||
// output first jar as "shadow" | ||
// so as not to confuse with yaml2json and json2Yaml | ||
archiveBaseName.set 'shadow' | ||
} | ||
|
||
void customShadowJar(ShadowJar s, String name, String className) { | ||
s.group 'shadow' | ||
s.description name + 'Jar shadow jar' | ||
s.archiveBaseName.set name | ||
s.manifest { | ||
attributes('Main-Class': 'info.ankin.projects.cli.yaml2json.' + className) | ||
} | ||
s.from zipTree(tasks.shadowJar.getArchiveFile()) | ||
} | ||
|
||
def yaml2JsonJar = tasks.register('yaml2JsonJar', ShadowJar) { | ||
customShadowJar(it, 'yaml2Json', 'Yaml2Json') | ||
} | ||
|
||
def json2YamlJar = tasks.register('json2YamlJar', ShadowJar) { | ||
customShadowJar(it, 'json2Yaml', 'Json2Yaml') | ||
} | ||
|
||
tasks { | ||
shadowJar { | ||
finalizedBy yaml2JsonJar, json2YamlJar | ||
} | ||
} | ||
|
||
task example { | ||
doLast { | ||
println | ||
} | ||
} | ||
|
||
// for graalvmNative | ||
tasks.jar.enabled(true) | ||
|
||
// https://graalvm.github.io/native-build-tools/0.9.11/gradle-plugin.html#_configuration_options | ||
graalvmNative { | ||
binaries { | ||
json2yaml { NativeImageOptions o -> | ||
o.imageName.set 'json2yaml' | ||
o.mainClass.set json2YamlJar.get().getManifest().getAttributes().get('Main-Class') | ||
|
||
o.debug.set false | ||
o.verbose.set true | ||
o.fallback.set false | ||
o.buildArgs.add('-Ob') // faster development builds | ||
o.useFatJar.set false | ||
o.classpath sourceSets.main.output.files, project.tasks.shadowJar | ||
} | ||
|
||
yaml2json { NativeImageOptions o -> | ||
o.imageName.set 'yaml2json' | ||
o.mainClass.set yaml2JsonJar.get().getManifest().getAttributes().get('Main-Class') | ||
o.debug.set false | ||
o.verbose.set true | ||
o.fallback.set false | ||
o.buildArgs.add('-Ob') // faster development builds | ||
o.useFatJar.set false | ||
o.classpath sourceSets.main.output.files, project.tasks.shadowJar | ||
} | ||
} | ||
} | ||
|
||
tasks { | ||
nativeCompile { | ||
dependsOn nativeYaml2jsonCompile, nativeJson2yamlCompile | ||
doLast { | ||
copy { | ||
from tasks.nativeJson2yamlCompile.outputs.files.files | ||
from tasks.nativeYaml2jsonCompile.outputs.files.files | ||
|
||
into tasks.nativeCompile.outputs.files.singleFile | ||
} | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
cli/yaml2json/src/main/java/info/ankin/projects/cli/yaml2json/Json2Yaml.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package info.ankin.projects.cli.yaml2json; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.json.JsonMapper; | ||
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; | ||
import lombok.SneakyThrows; | ||
|
||
public class Json2Yaml { | ||
@SneakyThrows | ||
public static void main(String[] args) { | ||
JsonMapper jsonMapper = new JsonMapper(); | ||
YAMLMapper yamlMapper = new YAMLMapper(); | ||
yamlMapper.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET); | ||
|
||
JsonNode jsonNode = jsonMapper.reader().readTree(System.in); | ||
yamlMapper.writeValue(System.out, jsonNode); | ||
System.out.println(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
cli/yaml2json/src/main/java/info/ankin/projects/cli/yaml2json/Yaml2Json.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package info.ankin.projects.cli.yaml2json; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.json.JsonMapper; | ||
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; | ||
import lombok.SneakyThrows; | ||
|
||
public class Yaml2Json { | ||
@SneakyThrows | ||
public static void main(String[] args) { | ||
JsonMapper jsonMapper = new JsonMapper(); | ||
jsonMapper.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET); | ||
YAMLMapper yamlMapper = new YAMLMapper(); | ||
|
||
JsonNode jsonNode = yamlMapper.reader().readTree(System.in); | ||
jsonMapper.writeValue(System.out, jsonNode); | ||
System.out.println(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
rootProject.name = 'java-projects' | ||
include 'cli:killport' | ||
include 'cli:yaml2json' | ||
include 'system:infer-platform' | ||
include 'system:terminal:supports-hyperlinks' |