Packaging A Robot To A Jar from the Command Line?
Ant proposes to do the same thing Make does, but with XML syntax instead, which is more portable and more friendly to manual editing. XML is a lot more forgiving on indentation and blank spaces.
Despite my (slight) efforts, I still do not understand Maven. I really should read up on it some time.
Maven has a declarative approach to build scripts, in contrast to Ant which has an imperative approach.
Since most build scripts all end up doing the same thing, like deleting files from a previous build (clean), compiling source (compile) a packaging compiled files (package), Maven standardized these steps in a hard-coded life-cycle. Also, Maven has a programming-by-exception approach, with defaults for all configurations. What you basically do in Maven is reconfigure what you want different from the basic life-cycle, and Maven does the rest.
If you execute an "empty" Maven script with "mvn clean package" command line, it will:
- Delete "/target" directory (clean goal)
- Look for source files in "/src/main/java" directory, compile them and put compiled files in "/target/classes" directory (compile goal, which always runs before package goal, like depend in Ant)
- Package files in "/target/classes" and "/src/main/resources" directories and put them all in a .jar file, which is stored in "/target" directory. (package goal)
You can reconfigure all directories by increasing the build script. But an "empty" script with only the project name and a few other tags also work.
If you need to import other JARs, like robocode.jar, then you need to declare them as dependencies, one-by-one in the pom.xml file. These JARs are searched in a "<home>/.m2/repository" directory, which is called a local repository. If they are not there, they are downloaded from a remote repository. By default, Maven downloads from the public Apache repository on internet, but it can be reconfigured as well. If you declare a dependency, but this dependency also needs another dependency, Maven discovers them for you by analyzing all pom.xml files from all JARs and download them all (recursive dependency resolution).
If you execute the same "empty" script with "mvn clean install" it will perform all the steps above and:
- After packaging, it will copy the .JAR file into the local repository, which can be used as dependency in other builds. (install goal)
If you need another JAR as dependency, which was never built in Maven, it can also be put in a repository with the lengthy command: "mvn install:install-file -Dfile=<jar name> -Dversion=<some version> -DartifactId=<dependency name> -DgroupId=<dependency group name>"
If you omit the "clean" goal, then the build tries to reuse existing files from a previous build to shorten build time (like Ant does).
There are other goals, but those are the essential ones.
I have to say, not too much of a fan of Ant personally. Comparing Makefiles and Ant XML I'd agree the XML is more portable, but I'd consider the Makefile to similar in terms of friendliness for manual editing. On one hand Make is annoyingly picky about certain whitespace, but on the other hand I find some of the boilerplate string repetition in XML to make things less human-readable, especially when not using a syntax highlighting text editor.
(Then again, part of my not being a fan of Ant may be on account of having seen Ant badly abused as a general-ish purpose scripting language. In other words... being forced to write certain kinds of scripting flow control in Ant XML can probably make someone start to dislike Ant...)
I only use it to package my robot. How do you do even do 'general flow control' in ant? The only ways I can think of are massively involved.
Maven tries to fight scripting logic abuse in Ant by hard-coding everything in a strict life-cycle, which is hard to customize. It still doesn't solve the problem though. There is a lot of room for plug-in abuse in Maven as well. But well behaved Maven scripts tend to be smaller than well behaved Ant scripts in general.
You do general flow control in Ant by using ant-contrib add-on. Ant becomes a Turing complete language with it.
For those who don't abuse the scripting language, don't abuse plug-ins and don't like XML, there is Gradle. Which is Turing complete like Ant + ant-contrib, have full support for Maven plug-ins and repositories, and uses Groovy/Java syntax instead of XML.