Skip to content

org-metalib/json-path-maven-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json-path maven plugin

A Maven plugin that wraps the JsonPath Java library, enabling JSON parsing and querying directly within Maven builds.

Features

  • Parse JSON files or inline JSON strings.
  • Extract values from JSON using JsonPath expressions.
  • Automatically update .env files with extracted values.
  • Automatically update .gitignore

Requirements

  • Java 21 or higher
  • Maven 3.9.9 or higher

Installation

To use the plugin, add the following to your pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.metalib.maven.plugin</groupId>
            <artifactId>json-path-maven-plugin</artifactId>
            <version>0.0.3</version>
        </plugin>
    </plugins>
</build>

Usage

Configuration

The plugin accepts an array of JsonInput objects via the paths parameter. Each JsonInput object can specify:

  • format: The format of the input (e.g., JSON).
  • text: Inline JSON content.
  • file: A file containing JSON content.
  • queryMaps: A set of key-value pairs where the key is the property name and the value is the JsonPath query.

text and file parameters are mutually exclusive.

Example

Here is an example configuration in your pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.metalib.maven.plugin</groupId>
            <artifactId>json-path-maven-plugin</artifactId>
            <version>0.0.3</version>
            <configuration>
                <paths>
                    <path>
                        <file>json-text.json</file>
                        <queryMaps>
                            <FOO>$.foo</FOO>
                            <BAR_BAZ>$.bar.baz</BAR_BAZ>
                        </queryMaps>
                    </path>
                </paths>
            </configuration>
        </plugin>
    </plugins>
</build>

with the sample input file json-text.json:

{
  "foo": "bar",
  "bar": {
    "baz": "qux"
  }
}

Goals

paths

This goal processes the JSON inputs and performs the following:

  1. Extracts values from JSON using the specified JsonPath queries.
  2. Updates the .env file in the project directory with the extracted values.
  3. Ensures the .env file is listed in .gitignore to prevent accidental commits.

Running the Plugin

To execute the plugin, run:

mvn jq:paths

It will create or update .env as shown:

#Wed Apr 23 16:58:10 CDT 2025
BAR_BAZ=qux
FOO=bar

It will make sure that there is a

### protecting .env file ###
.env

References

License

This project is licensed under the Apache License, version 2.0. See the LICENSE file for details.

About

This plugin tries to do the same what jq does for bash environment

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors