This tutorial assumes you will read also some other pages on the site, and concentrates on stating rules and recommendations in each step.
Basic steps
If you use Maven2, you have a project with a pom.xml so you will need to add something like this (with the released version that you selected, of course):
<dependency> <groupId>net.kozelka</groupId> <artifactId>args-inject</artifactId> <version>0.1-SNAPSHOT</version> </dependency>
If you compile your code with Ant, then fragments of following code might be useful to you:
<project name="my-project" xmlns="antlib:org.apache.tools.ant" xmlns:artifact="urn:maven-artifact-ant"> <!-- add maven-ant-tasks --> <typedef classpath="${user.home}/.m2/lib/maven-ant-tasks-2.2.1.jar" resource="org/apache/maven/artifact/ant/antlib.xml" uri="urn:maven-artifact-ant"/> <!-- resolve the dependency --> <artifact:dependencies> <artifact:dependency groupId="net.kozelka" artifactId="args-inject" version="0.1-SNAPSHOT"/> </artifact:dependencies> <!-- use the dependency as a file --> <echo>add this path to javac classpath: ${net.kozelka:args-inject:jar}</echo> <!-- ... -->
Note that this uses the ant library maven-ant-tasks which is an excellent library for reusing Maven artifacts in Ant code; I highly recommend it to your attention.
In some cases, it might still be useful to have direct download links. Here they are:
Artifact | MD5 | SHA1 | Description |
args-inject-0.1-SNAPSHOT.jar | MD5 | SHA1 | library JAR |
args-inject-0.1-SNAPSHOT-sources.jar | MD5 | SHA1 | library sources |
Rules
Hints
package net.kozelka.args; import java.io.File; import java.util.Arrays; import java.util.Properties; import java.util.concurrent.TimeUnit; import net.kozelka.args.annotation.AnnottationAwareSetup; import net.kozelka.args.annotation.Option; import net.kozelka.args.annotation.SubCommand; import net.kozelka.args.api.ArgsCommand; @SubCommand(name = "ls", description = "list files (demo)") public class DemoFileLister implements ArgsCommand { private final boolean deep; private final int maxCount; final File[] files; private boolean colorful; private final Properties properties = new Properties(); private TimeUnit timeunit = TimeUnit.NANOSECONDS; private File[] locations; private int[] numbers; public DemoFileLister(boolean deep, int maxCount, File... files) { this.deep = deep; this.maxCount = maxCount; this.files = files; } @Option(longName = "--locations", description = "shows that default separator for files is File.pathSeparator") public void setLocations(File[] locations) { this.locations = locations; } @Option(longName = "--numbers", description = "shows that default separator is comma") public void setNumbers(int[] numbers) { this.numbers = numbers; } @Option(longName = "--time-unit", shortName = "-u") public void setTimeunit(TimeUnit timeunit) { this.timeunit = timeunit; } @Option(longName = "--property", shortName = "-D") public void addProperty(String propertyName, String propertyValue) { properties.put(propertyName, propertyValue); } @Option(longName = "--color", shortName = "-C", description = "set output colors") public void setColorful(boolean colorful) { this.colorful = colorful; } public Integer call() throws Exception { System.out.println("colorful = " + colorful); System.out.println("deep = " + deep); System.out.println("maxCount = " + maxCount); System.out.println("files = " + Arrays.asList(files)); System.out.println("properties = " + properties); System.out.println("timeunit = " + timeunit); return 0; } static int run(String... args) throws Exception { final AnnottationAwareSetup setup = new AnnottationAwareSetup("DemoFileLister"); setup.setDefaultSubCommand(DemoFileLister.class); return BasicArgsParser.process(setup, args); } }
Rules
Hints
Rules
Hints
public static void main(String[] args) throws Exception { final AnnottationAwareSetup setup = new AnnottationAwareSetup("myapp"); setup.setDefaultSubCommand(DemoFileLister.class); final int exitCode = BasicArgsParser.process(setup, args); if (exitCode != 0) { System.exit(exitCode); // indicate failure to shell } }
Rules
Hints
exec java -classpath $CP net.kozelka.args.minicalc.MiniCalc $*