Skip to content

Instantly share code, notes, and snippets.

@hlzhang
Last active September 26, 2019 21:57
Show Gist options
  • Save hlzhang/48860851bcb1c04432f3507bb20c2e5c to your computer and use it in GitHub Desktop.
Save hlzhang/48860851bcb1c04432f3507bb20c2e5c to your computer and use it in GitHub Desktop.
Fix IDEA Intellij's issue "Module must not contain source root. The root already belongs to module". Intellij always add root module's src/main/resources into child modules in multi module maven projects and prevent me saving module config unless I remove them all manually. That's really sucks.
set -x
function fix_content_root() {
local in_directory="$1"
local targets=($(grep -E -l --include \*.iml '<content url="file://\$MODULE_DIR\$/(\.\./)+src/main/resources"' -r ${in_directory} | grep -v 'fixContentRoot.sh'))
for target in ${targets[@]}; do
echo ${target};
sed -Ei 's#<content url="file://\$MODULE_DIR\$/(\.\./)+src/main/resources" />##g' ${target};
sed -i '/<content url="file:\/\/$MODULE_DIR$\/..\/src\/main\/resources">/{:a;N;/<\/content>/!ba};/<sourceFolder url="file:\/\/$MODULE_DIR$\/..\/src\/main\/resources" type="java-resource" \/>/d' ${target};
sed -i '/<content url="file:\/\/$MODULE_DIR$\/..\/..\/src\/main\/resources">/{:a;N;/<\/content>/!ba};/<sourceFolder url="file:\/\/$MODULE_DIR$\/..\/..\/src\/main\/resources" type="java-resource" \/>/d' ${target};
sed -i '/<content url="file:\/\/$MODULE_DIR$\/..\/..\/..\/src\/main\/resources">/{:a;N;/<\/content>/!ba};/<sourceFolder url="file:\/\/$MODULE_DIR$\/..\/..\/..\/src\/main\/resources" type="java-resource" \/>/d' ${target};
done
}
fix_content_root "${HOME}/Documents/IdeaProjects"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment