Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically create batch / shell scripts to run a Java console application?

I have a Java command-line application, and would like to create an Ant* build script that will create all the required batch/shell scripts to run the application successfully including all the classpath variables. I need it to do the following:

  1. Create a shell script file for Linux/Unix and a batch file for Windows/DOS
  2. Add all classpath dependencies (from Maven or simply use the build path in Eclipse)
  3. Add any necessary boilerplate sh/bat code to run (ENV variables, JAVA_HOME, etc.)

I found only a partial answer here.

But I haven't found anything that does this basic and trivial task that every build involves.

Disclaimer - the original question was Ant/Maven, but I would prefer to see if it can be done in Ant.

like image 938
Eran Medan Avatar asked Mar 10 '11 14:03

Eran Medan


2 Answers

In Maven the best solution for this is the maven-appassembler-plugin which handles the creation of a shell script / batch file. In combination with maven-assembly you can create a tar.gz or zip archive which contains everything which is needed.

like image 97
khmarbaise Avatar answered Oct 13 '22 00:10

khmarbaise


Maven knows the dependency:build-classpath goal, which does most of the dirty work:

mvn dependency:build-classpath -DoutputFile=cp.txt

You can use this generated file in a shell script to create the java classpath (I know, it ain't much, but it'll get you started).

Or you can use the exec-maven-plugin to launch a main class from the current maven context. Something like this will do:

mvn compile org.codehaus.mojo:exec-maven-plugin:1.2:java \
    -Dexec.mainClass=com.yourcompany.YourClass
like image 26
Sean Patrick Floyd Avatar answered Oct 13 '22 02:10

Sean Patrick Floyd