Excluding jars from dynamically generated class classes?
I am starting my Java server from a bash script. The server has "provided" dependencies to third party banks. However, some of these jars conflict with jars in my application and should be excluded from the classpath ...
At the moment I only need to exclude one jar, so this idiom turns me on
EXTERNAL_JARS=$(find "${EXTERNAL_LIB}" -name 'slf4j-log4j12-1.4.3.jar' -prune -o -type f -name '*.jar' -printf ':%p' )
CLASSPATH=${CLASSPATH}${EXTERNAL_JARS}
Is there a better approach to use when the number of external cans is 20-30 and the number of excluded cans is ~ 5?
source to share
This will work, although it assumes you don't have spaces in your filenames.
EXCLUDED="slf4j-log4j12-1.4.3.jar
some-other-library.jar
something-else.jar"
EXTERNAL_JARS=$(
find "${EXTERNAL_LIB}" -type f -name '*.jar' \
| grep -v -F"$EXCLUDED" \
| xargs \
| tr ' ' ':'
)
CLASSPATH=${CLASSPATH}:${EXTERNAL_JARS}
The idea here is to use grep -v -F
and multi-line string variable to filter excluded jars.
When you do, you can no longer use the flag -printf
in find
, so you replace it with xargs | tr ' ' '-'
. It xargs
will concatenate all the jars here, separating them with a space, and the command will tr
replace those spaces with colons. Again, this will work if you have no gaps in your path.
source to share