Maven and installing a command-line program

rwallace :

Having written a command-line program in Java, using Maven as the build system, what's the recommended way to go from there to having the program available as a command?

Suppose the program is called Foo. In the foo directory I can run mvn package to generate target/foo-1.0-SNAPSHOT.jar, which can in turn be run with java -cp target/foo-1.0-SNAPSHOT.jar foo.Main %* but that command is too long to expect users to type. I need to get to the point where typing foo on the command line will run the program. mvn install doesn't; it just copies the jar to the local maven repository.

What's the recommended way to make a program available as a command?

rwallace :

I ended up just writing a simple Python script: https://github.com/russellw/ayane/blob/master/build.py

#!/usr/bin/python3
import subprocess
import os   

subprocess.check_call("mvn package", shell=True)
if os.name == "nt":
    with open("ayane.bat", "w") as f:
        f.write("java -ea -jar %s\\target\\ayane-3.0-SNAPSHOT.jar %%*\n" % os.getcwd())
else:
    with open("ayane", "w") as f:
        f.write("#!/bin/sh\n")
        f.write('java -ea -jar %s/target/ayane-3.0-SNAPSHOT.jar "$@"\n' % os.getcwd())
    st = os.stat("ayane")
    os.chmod("ayane", st.st_mode | 0o111)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=95619&siteId=1