Recently I wanted to make a simple Java servlet without dealing with Eclipse (or any IDE). I have traditionally always used Eclipse to start a new Java project, and then committed whatever mess of xml and .project files the thing dumped onto my computer. This is not fun later. I decided to use Apache Buildr, a Java build framework which appears to be Rake mixed together with Maven. The documentation looks promising, but it is not yet popular enough that google won't try to correct your spelling when you try searching about it. It uses Ruby's syntax, so the build file is not XML like Ant or Maven, but it is compatible with Maven2 respositories. This means there is the chance of finding all your dependencies automatically. Not a good chance though.
My project had the following requirements:
1) Build against a jar from a maven repo. 2) Build against some local jars. 3) Remove at least one of the jars from the final war. This had to do with not delivering the servlet-api jar that conflicts with the one tomcat provides. 4) Include an xml file in a specific, non-standard location in the final war.
First the build directory structure:
buildfile <-- this is the buildr build file
lib/ <-- folder containing all dependent jars
src/main/
src/main/webapp/WEB-INF/ web.xml <-- war file's web.xml
src/main/resources/ <-- various configuration files
src/main/java/ <-- Your java code
src/main/conf/ <-- Non-standard location for resource file
The buildr documentation explains that the /src/main/webapp will become the root of the war, your java files will be compiled and placed at /WEB-INF/classes, and the dependent jars will be put into WEB-INF/libs.
Now the buildfile:
# Generated by Buildr 1.4.5, change to your liking
# Version number for this release
VERSION_NUMBER = "1.0.0"
#Group identifier for your projects
GROUP = "SomeWar"
COPYRIGHT = ""
# Specify Maven 2.0 remote repositories here
repositories.remote << "http://www.ibiblio.org/maven2/"
repositories.remote << "http://repo1.maven.org/maven2/"
# Specifying an artifact here, Maven style
SERVLET = "javax.servlet:servlet-api:jar:2.4"
desc "The SomeWar Project"
define "somewar" do
project.version = VERSION_NUMBER
project.group = GROUP
manifest["Implementation-Vendor"] = COPYRIGHT
# Add all the jars in your lib directory to the dependencies
compile.with Dir[_("lib/*.jar")]
# Go find that servlet artifact in one of those Maven repos previously specified
compile.with SERVLET
# Uncomment the line below to show what jars will be included in your war
# puts package(:war).libs.map()
# This final line says to include my.xml file at
# src/main/conf/ as new.xml
# at WEB-INF/classes/conf,
# and remove the jar specified by the SERVLET artifact
package(:war).include("src/main/conf/my.xml",:as=>'WEB-INF/classes/conf/new.xml').libs -= artifacts(SERVLET)
end
That's it.
Thanks for reading, please leave any comments or corrections.