Properties loader from Json file

This mojo can import json files as properties using a pseudo-normalized json path as property names, it can also extract values using JsonPath and then assign the extracted values to project properties.

License

License

Categories

Categories

Maven Build Tools JSON Data
GroupId

GroupId

com.github.iarellano
ArtifactId

ArtifactId

iad-json-properties-maven-plugin
Last Version

Last Version

1.1
Release Date

Release Date

Type

Type

maven-plugin
Description

Description

Properties loader from Json file
This mojo can import json files as properties using a pseudo-normalized json path as property names, it can also extract values using JsonPath and then assign the extracted values to project properties.
Project URL

Project URL

https://github.com/iarellano/iad-json-properties-maven-plugin
Source Code Management

Source Code Management

https://github.com/iarellano/iad-json-properties-maven-plugin

Download iad-json-properties-maven-plugin

How to add to project

<plugin>
    <groupId>com.github.iarellano</groupId>
    <artifactId>iad-json-properties-maven-plugin</artifactId>
    <version>1.1</version>
</plugin>

Dependencies

compile (4)

Group / Artifact Type Version
com.google.code.gson : gson jar 2.8.5
com.jayway.jsonpath : json-path jar 2.4.0
org.apache.maven : maven-plugin-api jar 3.0
org.apache.commons : commons-lang3 jar 3.8.1

provided (2)

Group / Artifact Type Version
org.apache.maven.plugin-tools : maven-plugin-annotations jar 3.4
org.apache.maven : maven-core jar 3.3.9

Project Modules

There are no modules declared in this project.

iad-json-properties-maven-plugin

License: MIT Maven Central

Support to import json files as project properties.

About this plugin

This mojo can import json files as system properties using a pseudo-normalized json path as property names, it can also extract values using JsonPath and then assign the extracted values to project properties.

A sample project is provided in the samples directory.

Importing a json files

<plugin>
    <groupId>com.github.iarellano</groupId>
    <artifactId>iad-json-properties-maven-plugin</artifactId>
    <version>1.1</version>
    <executions>
        <execution>
            <id>parse-json-files</id>
            <phase>initialize</phase>
            <goals>
                <goal>load-json-properties</goal>
            </goals>
            <configuration>
                <skip>false</skip>
                <prefix>parentPrefix.</prefix>
                <files>
                    <file>
                        <prefix>testPrefix.</prefix>
                        <failIfFileNotFound>true</failIfFileNotFound>
                        <filePath>src/main/resources/test.json</filePath>
                    </file>
                    <file>
                        <charset>UTF-8</charset>
                        <prefix>arrayPrefix.</prefix>
                        <failIfFileNotFound>false</failIfFileNotFound>
                        <filePath>src/main/resources/array.json</filePath>
                    </file>
                </files>
            </configuration>
        </execution>
    </executions>
</plugin>

Global parameters

Parameter Description Required
skip Enable or disable plugin execution No
prefix Global prefix for the current execution configuration, if given then all imported properties will be prefixed by this value No

File specific parameters

Parameter Description Required
filePath Path to the json file, it can be absolute or relative to the project root directory Yes
prefix Local prefix, if specified then all properties imported from the current file will have this prefrixed. No
failIfFileNotFound If true and filePath does not point to a valid json file then this mojo throws an exception. Default is false No
charset File charset. Default is UTF-8 No

Using JsonPath

This method makes use of Jsonway Jsonpath implementation to evaluate JsonPaths.

<plugin>
    <groupId>com.github.iarellano</groupId>
    <artifactId>iad-json-properties-maven-plugin</artifactId>
    <version>1.1</version>
    <executions>
        <execution>
            <id>extract-current-revision</id>
            <phase>initialize</phase>
            <goals>
                <goal>load-json-properties</goal>
            </goals>
            <configuration>
                <prefix>otherParentPrefix.</prefix>
                <skip>false</skip>
                <jsonPath>
                    <file>
                        <prefix>childPrefix.</prefix>
                        <filePath>src/main/resources/array.json</filePath>
                        <charset>UTF-8</charset>
                        <failIfFileNotFound>false</failIfFileNotFound>
                        <lookups>
                            <lookup>
                                <jsonPath>$.[1]</jsonPath>
                                <propertyName>item1.value</propertyName>
                                <failFast>true</failFast>
                            </lookup>
                        </lookups>
                    </file>
                    <file>
                        <prefix>suffix2.</prefix>
                        <filePath>src/main/resources/test.json</filePath>
                        <lookups>
                            <lookup>
                                <jsonPath>$.address.street</jsonPath>
                                <propertyName>address.street</propertyName>
                            </lookup>
                        </lookups>
                    </file>
                </jsonPath>
            </configuration>
        </execution>
    </executions>
</plugin>

The only difference with with method 1 is that <file> is enclosed in <jsonPath> rather than in <files> and now have lookup elements are defined within <file><lookups>, therefore let's go straight to the definition of the <lookup> parameters.

Lookup parameters

Parameter Description Required
jsonPath JsonPath to evaluate Yes
propertyName Name of the property to which the result of the JsonPath evaluation wiil be assigned to Yes
failFast If true and the provided JsonPath does not evaluate to a primitive or null then an error is thrown. Default is false No

Versions

Version
1.1
1.0