How to Read .yml Files in Jar Files
Intro.
Some far ago, I've been introducing 'How to manage your configuration file with YAML in Java programmatically' in previous mail service.
if you wanna know information technology, follow link: How to manage your configuration file with YAML in Java programmatically
It was about handing Yaml file contents to create Java Edible bean object. That was including data of a configuration or serialized Valued-Object(VO). Simply to say, It was well-nigh load or dump information between Yaml file and Java object.
Meanwhile, Someone who has keen thinking asked how to access a Yaml file in internal resources?
So I realized it's need,
Hither I would evidence how to load and dump Yaml file in Jar.
Setting upwards environments.
- As aforementioned as previous post, To handle Yaml file, We demand SnakeYAML library.
- For our works, Whatever IDE Tools is able to use, Simply I gonna to employ Visual Studio Code(VSCODE) on fever in these days.
- And nosotros accept to set SnakeYAML MAVEN dependency to POM.xml. In previous postal service I used Gradle only now, Would employ MAVEN for diversity :)
- If you lot are prepared with above things, Readied to kickoff coding.
Designing Java Class.
Before we gonna see implemented code, I would bear witness you entire pattern of our works.
- We will define java class named "YamlInternalHandler" to handle Yaml file in Jar.
- Volition ascertain ii method named "loadInternalJar" and "dumpInternalJar". First ane is to load Yaml'south contents to Java object with jar entry path and a Form object to exist loaded the contents. Second one is to dump a object values to Yaml file in Jar with jar entry path and instance of Java object including a values.
- We could load and dump a specified object mapping with a Yaml file in jar. Surely, It'due south proficient to make certain syntax of Yaml file in Jar.
Let'southward dive into code.
Contents of Yaml file in Jar is down below.
firstName : Kooin lastName : Shin age : 48 profile : - grapheme : bright hobby : challenge address : Seoul postalCode : 123 - graphic symbol : delight hobby : ski address : Seoul postalCode : 123
Java Bean to exist loaded contents is 'Customer' and 'Profile' representing Yaml'due south values.
packet org.chaostocosmos.doc.yml ; import coffee.util.List ; public class Customer { private String firstName ; individual Cord lastName ; private int historic period ; private Listing < Profile > profile ; public Client () {} public String getFirstName () { return this . firstName ; } public void setFirstName ( String firstName ) { this . firstName = firstName ; } public String getLastName () { return this . lastName ; } public void setLastName ( Cord lastName ) { this . lastName = lastName ; } public int getAge () { return this . historic period ; } public void setAge ( int age ) { this . historic period = historic period ; } public List < Contour > getProfile () { return this . profile ; } public void setProfile ( List < Contour > profile ) { this . profile = contour ; } @Override public String toString () { return "{" + " firstName='" + firstName + "'" + ", lastName='" + lastName + "'" + ", historic period='" + age + "'" + ", contour='" + contour + "'" + "}" ; } }
package org.chaostocosmos.doc.yml ; public class Profile { Cord character ; String hobby ; String address ; int postalCode ; public Profile () {} public Cord getCharacter () { render this . character ; } public void setCharacter ( String character ) { this . character = character ; } public Cord getHobby () { return this . hobby ; } public void setHobby ( String hobby ) { this . hobby = hobby ; } public String getAddress () { return this . address ; } public void setAddress ( String address ) { this . address = address ; } public int getPostalCode () { render this . postalCode ; } public void setPostalCode ( int postalCode ) { this . postalCode = postalCode ; } @Override public String toString () { return "{" + " character='" + character + "'" + ", hobby='" + hobby + "'" + ", address='" + accost + "'" + ", postalCode='" + postalCode + "'" + "}" ; } }
And At present nosotros gonna testify cadre code of 'YamlInternalHandler' grade.
bundle org.chaostocosmos.doc.yml ; import java.io.File ; import java.io.FileNotFoundException ; import coffee.io.FileOutputStream ; import java.io.IOException ; import java.io.InputStream ; import java.io.OutputStreamWriter ; import java.util.jar.JarEntry ; import java.util.jar.JarFile ; import java.util.jar.JarOutputStream ; import java.util.zip.ZipEntry ; import org.yaml.snakeyaml.DumperOptions ; import org.yaml.snakeyaml.Yaml ; import org.yaml.snakeyaml.DumperOptions.FlowStyle ; import org.yaml.snakeyaml.constructor.Constructor ; /** * Yaml Internal Handler * * @writer Kooin-Shin * @since 2021.01.04 */ public form YamlInternalHandler { /** * Jar file having yaml file */ File file ; /** * Constructor * @param file * @throws IOException */ public YamlInternalHandler ( File file ) throws IOException { this . file = file ; } /** * Load object from internal yaml file in jar * @param jarEntry * @param clazz * @return * @throws IOException */ public Object loadInternalJar ( String jarEntry , Class <? extends Object > clazz ) throws IOException { if (! jarEntry . endsWith ( ".yml" )) { throw new IllegalArgumentException ( "This component only support a yaml file in jar!!!" ); } JarFile jarFile = new JarFile ( this . file ); //Create JarFile object JarEntry entry = jarFile . getJarEntry ( jarEntry ); //Get JarEntry specified past first parameter InputStream is = jarFile . getInputStream ( entry ); //Get InputStream from JarFile by JarEntry Constructor constructor = new Constructor ( clazz ); //Create Constructor object specified by second parameter Yaml yaml = new Yaml ( constructor ); //Create Yaml object with Constructor object Object obj = yaml . load ( is ); //Load contents to object case is . shut (); //Close InputStream jarFile . shut (); //Close JarFile return obj ; } /** * Dump object to yaml file in jar * @param jarEntry * @param obj * @throws FileNotFoundException * @throws IOException */ public void dumpInternalJar ( Cord jarEntry , Object obj ) throws FileNotFoundException , IOException { JarOutputStream jos = new JarOutputStream ( new FileOutputStream ( this . file )); //Create JarOutputStream object jos . putNextEntry ( new ZipEntry ( jarEntry )); //Put jar entry by get-go parameter DumperOptions options = new DumperOptions (); //Set up dump options options . setDefaultFlowStyle ( FlowStyle . BLOCK ); options . setPrettyFlow ( true ); Yaml yaml = new Yaml ( options ); //Create Yaml object with DumperOptions object yaml . dump ( obj , new OutputStreamWriter ( jos )); //Dump contents of second parameter jos . close (); // Close JarOutputStream } public static void main ( String [] args ) throws IOException { File file = new File ( "D:\\Projects\\chaos-commons\\yaml.jar" ); YamlInternalHandler ymlHandler = new YamlInternalHandler ( file ); Customer customer = ( Customer ) ymlHandler . loadInternalJar ( "tmp/yaml/Client.yml" , Customer . class ); System . out . println ( customer . toString ()); customer . setFirstName ( "Jim " ); customer . getProfile (). become ( 0 ). setAddress ( "Busan" ); ymlHandler . dumpInternalJar ( "tmp/yaml/Customer.yml" , customer ); } }
Decision
Above code is assumed very simple cases of accessing of a Yaml in Jar. As a situation, It can be transformed, modified and improved.
E'er rubberband thinking is yours!!!
Good Luck!!!
Source: https://dev.to/kooin/how-to-handling-internal-yaml-in-jar-261n
Postar um comentário for "How to Read .yml Files in Jar Files"