Skip to content

Instantly share code, notes, and snippets.

Created February 20, 2018 16:36
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Update SonarQube properties file with project name and version from package.json
#!/usr/bin/env bash
#title :
#description :
# This script parses the project's name and version from its package.json and automagically
# updates the version and package name in the SonarQube configuration properties file.
# It can be used as a pre step before running the sonar-scanner command
# It also creates a backup of the props file with suffix *.bak
#prerequisites : NodeJS based project with package.json, sonar*.properties file in the cwd
#author : Christian-André Giehl <>
#date : 20180220
#version : 1.0
#usage : sh
echo "Updating the SonarQube properties..."
# Get the version from package.json
PACKAGE_VERSION=$(cat package.json \
| grep version \
| head -1 \
| awk -F: '{ print $2 }' \
| sed 's/[",]//g' \
| tr -d '[[:space:]]')
echo "Extracted version: ${PACKAGE_VERSION}"
# Get the project name from package.json
PACKAGE_NAME=$(cat package.json \
| grep name \
| head -1 \
| awk -F: '{ print $2 }' \
| sed 's/[",]//g' \
| tr -d '[[:space:]]')
echo "Extracted project: ${PACKAGE_NAME}"
# Get the Sonar properties file
SONAR_FILE=$(find ./ -iname sonar*.properties -type f)
echo "Sonar file found: ${SONAR_FILE}"
# Update the version
sed -i.bak "s#${REPLACE}#${WITH}#g" ${SONAR_FILE}
# Update the project name
sed -i.bak "s#${REPLACE}#${WITH}#g" ${SONAR_FILE}
echo "Done!"
Copy link

This is my script based on this script that works on both Linux and Mac:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment