Reputation: 33365
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?
Upvotes: 3
Views: 284
Reputation: 33365
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)
Upvotes: 0
Reputation:
You can use Maven Assembler Plugin like this:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.10</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
</execution>
</executions>
<configuration>
<programs>
<program>
<mainClass>sandbox.Main</mainClass>
<id>app</id>
</program>
</programs>
</configuration>
</plugin>
Running mvn package
will generate a windows (.bat) and unix shell script in the bin folder of the ${project.build.directory}/appassembler
sub folder.
Upvotes: 3