How should a bash script determine a classpath for a maven project and its dependencies?

Yay, my thesis is done! Now that the pressure is off and I've had my fill of playing Skyrim, I'm converting the code I wrote for my thesis from a chaotic directory built with ant to a nice maven project.

I originally had a bin directory with about 20 bash scripts that ran the various java and ruby programs used in my thesis, including the final jruby/sinatra-based web server. I am planning on moving my scripts to src/main/scripts, but I need to figure out how to handle the classpath.

I had previously just hardcoded paths in my scripts to the manually-downloaded dependencies. However, now that maven is downloading and storing all the jars I need, what's the best way to reference them from my scripts:

  • Should I just get the scripts to reference the full paths of various jars in the local repository like before?
  • Should I make the local repository directory a configuration option for my scripts and use relative paths to this directory?
  • Should I build a big hairy jar with all the dependencies using the maven assembly plugin and access this via the script-relative path ../../../target/*-jar-with-dependencies.jar?
  • Is there some better option I haven't thought of?

Answers


In your script, use the exec:java plugin to run Java classes. It will sort out the classpath based on the defined dependencies. Then you don't need to worry about it.


Relook at all the scripts that you have. Potentially you could achieve the functionality of some of them using maven exec plugin.

Besides assembly and shade plugins, you may want to look at the functionalities provided by maven dependency plugin as well.


In my project (Soluvas fb-tools/fbcli), because I use Java 6 and later (which supports wildcard classpaths), here's what I do:

#!/bin/bash                                                                                
# Must run first: mvn package dependency:copy-dependencies                                 
java -cp 'target/dependency/*:target/fbcli-1.0.0-SNAPSHOT.jar' org.jboss.weld.environment.se.StartMain "$@"         

No need for manual generation of classpaths. :)


There are quite some plugin doing similar things you mentioned. Assembly plugin you mentioned is doubtless one of them (and the way you suggested is also a neat working solution).

You may want to take a look in AppAssembler and Shade. They all provide some mechanism to bundle the dependencies and produce a directly executable package.


Need Your Help

how to use the touchpad gearVR

c# android unity3d oculus gear-vr

I try to code a script to change the skybox when someone touch the touch pad (of the samsung gear VR). I make some research about the touchpad input, and I found that it's supposed to work like a m...

Accurate Timings with Oscilloscopes on PC

embedded timing firmware

In the world of embedded software (firmware) it is fairly common to observe the order of events, take timings and optimise a program by getting it to waggle PIO lines and capturing their behavior o...