Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

'ant' build file for HelloWorldServlet and JettyLauncher

A pretty straightforward 'ant' build file. You will need to edit the jetty.home property to indicate where you installed Jetty.

<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="compile" name="hello-servlet">
  <property name="build.dir"	location="classes" />
  <property name="src.dir"	location="src/main/java" />
  <property name="jetty.home"	location="/home/mrw/projects/jetty-6.1.3" />
  <property name="jetty.lib"	location="${jetty.home}/lib" />

  <path id="jetty.lib.path">
    <pathelement path="${jetty.lib}/jetty-6.1.3.jar" />
    <pathelement path="${jetty.lib}/jetty-util-6.1.3.jar" />
    <pathelement path="${jetty.lib}/servlet-api-2.5-6.1.3.jar" />
  </path>

  <target name="compile" description="Compile the project">
    <mkdir dir="${build.dir}" />
    <javac debug="true" destdir="${build.dir}" srcdir="${src.dir}"
	classpathref="jetty.lib.path">
      <compilerarg value="-Xlint:unchecked" />
      <compilerarg value="-Xlint:deprecation" />
    </javac>
  </target>

  <target name="all"  depends="clean,compile"
      description="Recompile from scratch"/>

  <target name="server" depends="compile" description="Launch the server">
    <java classname="com.babblemind.JettyLauncher" fork="true"
	classpathref="jetty.lib.path">
      <classpath>
	<pathelement path="${build.dir}" />
      </classpath>
    </java>
  </target>

  <target name="clean" description="Delete all files created by compile">
    <delete dir="${build.dir}" />
    <delete dir="docs/api" />
  </target>
</project>

Automated GNU Makefile

A simple automated GNU Makefile to build and link c/c++/asm projects. All the variables that need to be tweaked are located at the begining:
PROGNAME: the name of the executable to be built
CC: the C compiler
CPP: the C++ compiler
ASM: the assambler
LD: the linker

This searches for all *.c, *.cpp and *.s files and compiles them into objects. It then links all the files into a single executable.

The strip rule is used to strip all unwanted symbols from the resulting executable. This usually results in a significant (think 40%) size optimisation.

$(VERBOSE).SILENT:

PROGNAME = prog

CC = gcc
CC += -c
CPP = g++
CPP += -c
ASM = nasm
ASM += -f elf -d ELF_TYPE
LD = g++

OBJFILES = $(patsubst %.c,%.o,$(wildcard *.c))
OBJFILES += $(patsubst %.s,%.o,$(wildcard *.s))
OBJFILES += $(patsubst %.cpp,%.o,$(wildcard *.cpp))

all: $(PROGNAME)

clean:
	@echo "Cleaning object files"
	@echo "    rm -f     *.o"
	rm -f *.o
	@echo "Cleaning backups"
	@echo "    rm -f     *~"
	rm -f *~
	@echo "Removing programme"
	@echo "    rm -f     "$(PROGNAME)
	rm -f $(PROGNAME)

%.o: %.s
	@echo "Assambling "$@
	@echo "    ASM       "$<
	$(ASM) $<

%.o: %.c
	@echo "Compiling "$@
	@echo "    CC        "$<
	$(CC) $<

%.o: %.cpp
	@echo "Compiling "$@
	@echo "    CPP       "$<
	$(CPP) $<

$(PROGNAME): $(OBJFILES)
	@echo "Linking "$@
	@echo "    LD        -o "$(PROGNAME)"        "$(OBJFILES)
	$(LD) -o $(PROGNAME) $(OBJFILES)

strip: $(PROGNAME)
	@echo "Stripping "$(PROGNAME)
	echo -n "Size of "$(PROGNAME)" before strip is "
	ls -sh $(PROGNAME) | cut -d' ' -f1
	@echo "    strip     "$(PROGNAME)
	strip $(PROGNAME)
	echo -n "Size of "$(PROGNAME)" after strip is "
	ls -sh $(PROGNAME) | cut -d' ' -f1

nothing:
	@echo "Nothing to do; quitting  :("
	@echo "HINT: Try make all"



Basic ant script with vim & jikes

Apache ant build XML. This will use jikes in place of javac. Any compiler error output is formatted so that vim can parse it.

ant is a modern alternative to make. The build script is an XML file. It works particularly well with java. Download it for free from the Apache ant site. Most people find ant a lot nicer to live with than make.

<?xml version="1.0"?>

<project name="Hello" default="compile" basedir=".">
  <property name="name" value="Hello"/>
  <property name="version" value="1.0"/>

  <!-- Project directories.
  -->

  <property name="build" value="build"/>
  <property name="dist" value="dist"/>
  <property name="src" value="src"/>

  <!-- Compiler directives.
  -->

  <property name="optimize" value="off"/>
  <property name="deprecation" value="on"/>
  <property name="debug" value="on"/>

  <property name="build.compiler" value="jikes"/>
  <property name="build.compiler.emacs" value="true"/>

  <target name="init">
    <tstamp/>
    <mkdir dir="${build}"/>
  </target>

  <!-- Compile all the .java files from the source directory into
       the build directory.
  -->

  <target name="compile" depends="init">
    <javac srcdir="${src}" destdir="${build}" includes="**/*.java"
      debug="${debug}" deprecation="${deprecation}" optimize="${optimize}">
    </javac>
  </target>

  <target name="clean" depends="init">
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>

  <target name="test" depends="compile">
    <java classname="HelloWorld" fork="yes">
      <classpath>
        <pathelement location="${build}"/>
      </classpath>
    </java>
  </target>
</project>

Ant 101

// Ah Ant. How I love thee, how much better a build tool
// than my ex build tool Make.
//
// This is my current starting build.xml Ant script for any
// Java project. It specifies my usual directories, it will
// build source code, make a Jar, build a War, clean up build
// products, create Javadoc, and even run unit tests.
//
// Notice that I use the JReleaseInfo library to automatically
// generate a Java class that contains version numbers, build
// numbers, etc. Be sure to put the Jar from that library in
// the library directory to get that feature:
// http://jreleaseinfo.sourceforge.net/

<?xml version="1.0"?>
<project name="New Project" basedir="." default="all">
    <target name="init">
        <property name="appName" value="newProject"/>
    
        <property name="buildDir" value="${basedir}/build"/>
        <property name="libDir" value="${basedir}/lib"/>
        <property name="javaSourceDir" value="${basedir}/src"/>
        <property name="testDir" value="${basedir}/test"/>
        <property name="webSourceDir" value="${basedir}/web"/>
        <property name="etcDir" value="${basedir}/etc"/>
        <property name="tempDir" value="${basedir}/tmp"/>
        <property name="classDir" value="${buildDir}/classes"/>

        <!-- This may get overridden when we are called from Anthill. -->
        <property name="version" value="Local Build"/>
        <property name="deployDir" value="${buildDir}"/>

        <property name="distDir" value="${deployDir}/dist"/>
        <property name="junitDir" value="${deployDir}/JUnit"/>
        <property name="javadocDir" value="${deployDir}/apidoc"/>

        <!-- Create the directories where we put all the build products. -->    
        <mkdir dir="${buildDir}"/>
        <mkdir dir="${classDir}"/>
        <mkdir dir="${distDir}"/>
        <mkdir dir="${junitDir}"/>
        <mkdir dir="${javadocDir}"/>

        <path id="compileClasspath">
            <fileset dir="${libDir}"/>
        </path>

        <!-- Make available build info in such a way that we can display it to the user. -->
        <taskdef name="jreleaseinfo" classname="ch.oscg.jreleaseinfo.anttask.JReleaseInfoAntTask">
            <classpath refid="compileClasspath"/>
        </taskdef>

        <jreleaseinfo className="MyReleaseInfo" packageName="com.johnmunsch.${appName}"
                targetDir="${javaSourceDir}" project="${appName}" version="${version}" 
                buildNumFile="buildnum.properties" buildNumProperty="buildnum">
            <parameter name="VersionNum" type="int" value="1" />
            <parameter name="RevisionNum" type="Integer" value="0" />
        </jreleaseinfo>
    </target>

    <target name="compile" depends="init">
        <javac srcdir="${javaSourceDir}" destdir="${classDir}" debug="true" deprecation="true">
            <classpath refid="compileClasspath"/>
        </javac>

        <!-- Copy files needed to run the software to destinations in the 
         build directory. I do this because I usually pull all binary files like
         this from inside the Jar files that make up my application rather than
         having them loose. So they need to be copied to the class dir so they
         get included in the Jar file for the application. -->
        <copy todir="${classDir}" >
            <fileset dir="${javaSourceDir}">
                <include name="**/*.gif"/>
                <include name="**/*.jpg"/>
                <include name="**/*.png"/>
                <include name="**/*.wav"/>
                <include name="**/*.dtd"/>
                <include name="**/*.properties"/>
            </fileset>
        </copy>
    </target>

    <target name="jar" depends="init,compile">
        <jar jarfile="${distDir}/${appName}.jar" compress="true" 
            basedir="${classDir}"/>
    </target>

    <target name="war" depends="jar">
        <war destfile="${distDir}/${appName}.war" webxml="${etcDir}/WEB-INF/web.xml">
            <fileset dir="web"/>
            <lib dir="${libDir}">
                <include name="jstl.jar"/>
                <include name="standard.jar"/>
            </lib>
            <lib dir="${libDir}">
                <include name="*.jar"/>
            </lib>
            <lib dir="${distDir}">
                <include name="${appName}.jar"/>
            </lib>
        </war>
    </target>

    <target name="all" depends="war" description="Build everything.">
        <echo message="Application built."/>
    </target>

    <target name="javadoc" depends="init" description="Javadoc for the code.">
        <javadoc packagenames="*" sourcepath="${javaSourceDir}" 
	        destdir="${javadocDir}"/>
    </target>

    <target name="clean" depends="init" description="Clean all build products.">
        <delete dir="${tempDir}"/>
        <delete dir="${javadocDir}"/>
        <delete dir="${junitDir}"/>
        <delete dir="${distDir}"/>
        <delete dir="${classDir}"/>
        <delete dir="${buildDir}"/>
    </target>

    <target name="junit" depends="jar" description="Performs unit tests.">
        <javac srcdir="test" destdir="${classDir}" debug="true" deprecation="true">
            <classpath refid="compileClasspath"/>
        </javac>

        <mkdir dir="${tempDir}"/>

        <junit failureproperty="junit.failed">
            <formatter type="xml"/>
            <batchtest todir="${tempDir}">
                <fileset dir="${classDir}">
                   <include name="**/*Test.class"/>
                   <include name="**/Test*.class"/>
                </fileset>
            </batchtest>
            <classpath>
                <path refid="compileClasspath"/>
                <pathelement location="${classDir}"/>
            </classpath>
        </junit>

        <mkdir dir="${buildDir}/JUnit"/>

        <junitreport tofile="TESTS-TestSuites.xml" todir="${tempDir}">
            <fileset dir="${tempDir}">
                <include name="TEST-*.xml"/>
            </fileset>
            <report todir="${junitDir}"/>
        </junitreport>
  	
        <fail message="JUnit test failure." if="junit.failed"/>  	
    </target>
</project>
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS