8000 Dev team by Lakshmanperumal · Pull Request #50 · skeeto/sample-java-project · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Aug 30, 2021. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 21 additions & 25 deletions README → README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
# Sample Java Project

This is a reference for setting up a new Ant-based Java project. It
contains most of the little Ant tricks I've learned over the
years. When I start a new Java project I clone this repository, remove
sections of build.xml that I don't care about for that particular
project, set the project properties, clear out the sample sources, and
get to work.

## Setup

Building this project requires that Ivy be available to Ant. All you
need is ivy.jar in Ant's classpath (in your $CLASSPATH, $ANT_HOME/lib,
or ~/.ant/lib).
need is ivy.jar in Ant's classpath (in your `$CLASSPATH`,
`$ANT_HOME/lib`, or `~/.ant/lib`).

## Dependencies

You will need to have Astyle installed and in your path for the
"format" target to work. If it's missing, that's fine. It won't affect
any other targets.

Ant will be looking for Checkstyle if you try to use the "check"
target. Like a hardcore bureaucrat, Checkstyle will harshly criticize
any stylistic mistakes and missing documentation, requiring you to dot
every "i" and cross every "t". That can keep idle hands pretty busy.

Ant will be looking for ProGuard if you try to use the "optimize"
target. This tool does a bytecode analysis to shorten (and obfuscate)
identifiers and remove unreachable code.

There is a "hotswap" target for replacing live code while an
application is running. You'll need the hotswap Ant extension
installed to use it. This target is to be used alongside the
Expand All @@ -31,26 +28,25 @@ the printed string in the code, and running "hotswap" in another
terminal. The printed message in the running program should change to
the new string.

Note that ProGuard, hotswap, Ivy, and Checkstyle are needed only in
the build classpath, not your project's classpath. That means you
probably don't want them in lib/. They should be where Ant can get to
them, not your project.
## Bundles

Take note of the sample pom.xml file. This is not actually for Maven
Take note of the sample `pom.xml` file. This is not actually for Maven
builds -- this is an Ant project afterall -- but for publishing builds
for a Maven repository. It's packed up by the "bundle" target, which
creates a bundle.jar containing your project's signed artifacts. To
creates a `bundle.jar` containing your project's signed artifacts. To
use the "bundle" target you need to have GnuPG set up in your path, a
generated key pair, and a running gpg-agent, unless you like typing
generated key pair, and a running `gpg-agent`, unless you like typing
your passphrase a bunch of times in a row.

## Philosophy

I hate coding absolute paths in my build script and I hate including
built files as part of the base project. My philosophy is that the
*environment* should be set up so that the tool can easily find the
external resources they need (JUnit, etc.) from the system, not within
the project. It's the system or dependency manager that provides the
libraries. Anyone who has the proper development environment set up --
one that works across many projects -- should be able to clone the
repository and do a build simply by running the build program with no
special arguments. There should be no need to edit or install anything
into the project space for the initial build.
external resources they need (JUnit, etc.) from the system or
dependency manager. It's the system or dependency manager that
provides the libraries. Anyone who has the proper development
environment set up -- one that works across many projects -- should be
able to clone the repository and do a build simply by running the
build program with no special arguments. There should be no need to
edit or install anything into the project space for the initial build.
69 changes: 54 additions & 15 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@
<!-- Targets -->

<target name="resolve" description="Retrieve dependencies with Ivy.">
<ivy:resolve log="quiet"/>
<ivy:resolve log="download-only"/>
<ivy:cachepath conf="build" pathid="build.classpath"/>
<ivy:cachepath conf="default" pathid="runtime.classpath"/>
<ivy:cachefileset conf="default" setid="runtime.fileset"/>
<ivy:cachepath conf="test" pathid="test.classpath"/>
<ivy:cachepath conf="analysis" pathid="analysis.classpath"/>
</target>

<target name="compile" depends="resolve" description="Compile all sources.">
<mkdir dir="${dist.dir}"/>
<mkdir dir="${build.dir}/classes"/>
<javac srcdir="${src.dir}" destdir="${build.dir}/classes"
optimize="on" debug="on" deprecation="on" includeantruntime="no">
Expand Down Expand Up @@ -53,8 +55,8 @@
</target>

<target name="run" depends="compile" description="Run the application.">
<java classname="${main.class}" classpath="${build.dir}/classes">
<classpath refid="build.classpath"/>
<java classname="${main.class}" classpath="${build.dir}/classes" fork="yes">
<classpath refid="runtime.classpath"/>
<arg value="--name"/>
<arg value="Java"/>
</java>
Expand All @@ -64,7 +66,7 @@
description="Run the application in hotswap mode.">
<java classname="${main.class}" classpath="${build.dir}/classes"
fork="true">
<classpath refid="build.classpath"/>
<classpath refid="runtime.classpath"/>
<jvmarg line="-Xdebug -Xrunjdwp:transport=dt_socket,address=9000,server=y,suspend=n"/>
<arg value="--loop"/>
<arg value="--name"/>
Expand All @@ -84,7 +86,6 @@
<target name="clean" description="Delete all generated files.">
<delete dir="${build.dir}"/>
<delete dir="${dist.dir}"/>
<delete dir="${lib.dir}"/>
<delete file="cache.properties"/>
</target>

Expand Down Expand Up @@ -113,15 +114,6 @@
</apply>
</target>

<target name="check" depends="resolve">
<taskdef resource="checkstyletask.properties"
classpathref="build.classpath"/>
<checkstyle config="checkstyle.xml">
<fileset dir="${src.dir}" includes="**/*.java"/>
<classpath path="${build.dir}/classes"/>
</checkstyle>
</target>

<target name="tags" description="Generate a TAGS file for your editor.">
<delete file="TAGS"/>
<apply executable="etags">
Expand All @@ -131,6 +123,40 @@
</apply>
</target>

<target name="check" depends="resolve" description="Run Checkstyle.">
<taskdef resource="checkstyletask.properties"
classpathref="analysis.classpath"/>
<checkstyle config="checkstyle.xml">
<fileset dir="${src.dir}" includes="**/*.java"/>
<classpath path="${build.dir}/classes"/>
</checkstyle>
</target>

<target name="findbugs" depends="compile" description="Run FindBugs.">
<taskdef classname="edu.umd.cs.findbugs.anttask.FindBugsTask"
name="findbugs" classpathref="analysis.classpath"/>
<findbugs output="emacs" classpathref="analysis.classpath" pluginList="">
<auxclasspath refid="analysis.classpath"/>
<sourcepath path="${src.dir}"/>
<class location="${build.dir}/classes"/>
</findbugs>
</target>

<target name="pmd" depends="resolve" description="Run PMD on the code.">
<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask"
classpathref="analysis.classpath"/>
<pmd>
<ruleset>pmd.xml</ruleset>
<formatter type="emacs" toConsole="true"/>
<fileset dir="${src.dir}">
<include name="**/*.java"/>
</fileset>
</pmd>
</target>

<target name="analyze" depends="check,pmd,findbugs"
description="Run all static analysis tools."/>

<!-- Unit testing (JUnit) -->

<target name="test-compile" depends="compile">
Expand All @@ -145,20 +171,33 @@
<copy todir="${build.dir}/test">
<fileset dir="${test.dir}" excludes="**/*.java"/>
</copy>
<!-- Test coverage -->
<taskdef classpathref="test.classpath" resource="tasks.properties"/>
<cobertura-instrument todir="${build.dir}/instrumented"
datafile="${build.dir}/cobertura.ser">
<fileset dir="${build.dir}/classes">
<include name="**/*.class"/>
</fileset>
</cobertura-instrument>
</target>

<target name="test" depends="test-compile" description="Run the unit tests.">
<junit fork="yes">
<classpath>
<pathelement path="${build.dir}/instrumented"/>
<pathelement path="${build.dir}/classes"/>
<pathelement path="${build.dir}/test"/>
<path refid="test.classpath"/>
</classpath>
<sysproperty key="net.sourceforge.cobertura.datafile"
file="${build.dir}/cobertura.ser"/>
<batchtest>
<formatter type="brief" usefile="false"/>
<fileset dir="${build.dir}/test"/>
</batchtest>
</junit>
<cobertura-report srcdir="${src.dir}" destdir="${dist.dir}/test"
datafile="${build.dir}/cobertura.ser"/>
</target>

<!-- Maven repository support -->
Expand All @@ -178,7 +217,7 @@
<fileset dir="${src.dir}"/>
</jar>
<!-- Sign all the things. You'll need gpg-agent help here. -->
<apply executable="gpg">
<apply executable="gpg" failonerror="true">
<arg value="--detach-sign"/>
<arg value="--armor"/>
<fileset dir="${dist.dir}/bundle"/>
Expand Down
24 changes: 20 additions & 4 deletions ivy.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,31 @@
<conf name="default"/>
<conf name="build" extends="default" visibility="private"/>
<conf name="test" extends="build" visibility="private"/>
<conf name="analysis" extends="build" visibility="private"/>
</configurations>
<dependencies>
<dependency org="commons-cli" name="commons-cli" rev="1.2"
<dependency org="com.beust" name="jcommander" rev="1.20"
conf="default"/>
<dependency org="junit" name="junit" rev="4.10"
conf="test->default"/>

<!-- Build -->
<dependency org="org.projectlombok" name="lombok" rev="0.10.4"
conf="build->default"/>

<!-- Unit Test -->
<dependency org="junit" name="junit" rev="4.10"
conf="test->default"/>
<dependency org="net.sourceforge.cobertura" name="cobertura" rev="1.9.4.1"
conf="test->default">
<exclude module="ant"/>
</dependency>

<!-- Static Analysis -->
<dependency org="com.puppycrawl.tools" name="checkstyle" rev="5.5"
conf="build->default"/>
conf="analysis->default"/>
<dependency org="com.google.code.findbugs" name="findbugs" rev="2.0.0"
conf="analysis->default"/>
<!-- note: due to FindBugs BCEL conflict, list PMD last. -->
<dependency org="net.sourceforge.pmd" name="pmd" rev="5.0.0"
conf="analysis->default"/>
</dependencies>
</ivy-module>
31 changes: 31 additions & 0 deletions pmd.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0"?>
<ruleset name="custom-ruleset">
<description>Custom set of preferred, sane rules</description>
<!-- This ruleset excludes things already covered by checkstyle. -->

<rule ref="rulesets/java/design.xml">
<!-- This ignores any explanation in the Javadoc. Bad. -->
<exclude name="UncommentedEmptyConstructor"/>
<!-- I disagree. -->
<exclude name="ConfusingTernary"/>
<!-- Complains about enums. I know what I'm doing so it's always wrong. -->
<exclude name="CompareObjectsWithEquals"/>
<!-- Maintainability is more important than a particular JVM's quirks. -->
<exclude name="TooFewBranchesForASwitchStatement"/>
<!-- Interacts poorly with Lombok and JCommander, but could be useful. -->
<exclude name="ImmutableField"/>
</rule>
<rule ref="rulesets/java/basic.xml"/>
<rule ref="rulesets/java/clone.xml"/>
<rule ref="rulesets/java/empty.xml"/>
<rule ref="rulesets/java/finalizers.xml"/>
<rule ref="rulesets/java/sunsecure.xml"/>
<rule ref="rulesets/java/strictexception.xml"/>
<rule ref="rulesets/java/strings.xml"/>
<rule ref="rulesets/java/typeresolution.xml"/>
<rule ref="rulesets/java/unnecessary.xml">
<!-- Ignores readability concerns. Very bad.-->
<exclude name="UselessParentheses"/>
</rule>
<rule ref="rulesets/java/unusedcode.xml"/>
</ruleset>
Loading
0