Skip to content

Instantly share code, notes, and snippets.

@dsyer
Created April 9, 2011 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsyer/911273 to your computer and use it in GitHub Desktop.
Save dsyer/911273 to your computer and use it in GitHub Desktop.
Shell script to fix WTP projects (WAR and JAR)
#!/bin/sh
if [ "$1" = "-h" -o "$1" = "--help" -o "$1" = "-?" ]; then
echo 'Usage: $0 <directory>'
echo ' Fix the WTP configuration in the specified directory and all subdirs.'
echo ' Any file named .classpath is patched to remove excluding="**"'
echo ' (useful for non wtp projects that were generated by m2e).'
echo ' WTP JAR modules are detected by searching for'
echo ' .settings/org.eclipse.wst.common.component in a non-war project.'
echo ' JAR modules are patched to setup .project and .settings correctly'
exit 0
fi
if [ "$#" != "0" ]; then
DIR=$1
shift
else
DIR=.
fi
echo fixing wtp configuration in $DIR
# Someone tried to make this a WTP module project.
# Let's make sure they didn't screw it up...
fix_component () {
DEPLOY_NAME=`awk '/.*<parent>/ {n++} /.*<\/parent>/ {n--} /.*<artifactId>/ {if (n<1&&m<1) {print; m++;}}' $1pom.xml | sed -e 's/.*<artifactId>//' -e 's,</artifactId>.*,,'`
cat > $1.settings/org.eclipse.wst.common.component <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="$DEPLOY_NAME">
<wb-resource deploy-path="/" source-path="/src/main/java"/>
<wb-resource deploy-path="/" source-path="/src/main/resources"/>
</wb-module>
</project-modules>
EOF
}
# Make sure ModuleCoreNature is in the .project, otherwise it won't be
# included in the classpath of any webapp that depends on it
fix_project () {
project=$1
if ! grep -q ModuleCoreNature $project; then
awk '{if (/<natures/) {print; print " <nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>"} else {print}}' $project > foo && mv foo $project
fi
}
# Remove those annoying excludes that stop integration tests working
# (this won't suit everyone, but I never use mvn resource filtering
# so the default Eclipse behaviour is fine).
fix_classpath () {
sed -i -e 's/excluding="\*\*" //g' $1
}
for f in `find $DIR -name pom.xml`; do
current=${f%pom.xml}
echo processing: $current
if [ -e $current.classpath ]; then
echo ' fixing classpath'
fix_classpath $current.classpath
fi
if ! grep -q \<packaging\>war $f; then
# not a war
if [ -e $current.settings/org.eclipse.wst.common.component ]; then
echo ' fixing wtp'
fix_component $current
fix_project $current.project
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment