This is an old revision of the document!
Table of Contents
maven and ant
prepare environment variables for building java with ant and maven
- add below paths to PATH environment variable in windows
d:\tools\jdk1.7.0_67\bin;D:\tools\apache-ant-1.8.4\bin;D:\tools\apache-maven-3.2.3\bin;
- add new environment variable JAVA_HOME
d:\tools\jdk1.7.0_67
Build simple application with command line
Basic steps to build application
- step1: create simple application with below directory structure:
md src md build\classes md build\jar
- step3:build and run
set PATH=%PATH%;d:\tools\jdk1.7.0_67\bin javac -sourcepath src -d build\classes src\oata\helloworld.java java -cp build\classes oata.helloworld
- step4: create manifest and jar file
echo Main-Class: oata.helloworld>myManifest md build\jar jar cfm build\jar\helloworld.jar myManifest -C build\classes . java -jar build\jar\helloworld.jar
compile and run with external libraries(log4j)
- update code to use log4j2
package oata; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class helloworld { static Logger logger = LogManager.getLogger(helloworld.class); public static void main(String[] args) { System.out.println("Hello World"); logger.error("Hello World"); } }
- compile:
D:\projects\java\externaljar>javac -cp "lib\log4j-core-2.0.jar";"lib\log4j-api-2.0.jar" -sourcepath src -d build\classes src\oata\helloworld.java
- run:
java -cp "lib\log4j-core-2.0.jar";"lib\log4j-api-2.0.jar";"build\classes" oata.helloworld
Ant
refer:
Apache Ant is a Java-based build tool. In theory, it is kind of like make, without make's wrinkles.
Instead of writing shell commands, the configuration files are XML-based, calling out a target tree where various tasks get executed. Each task is run by an object that implements a particular Task interface. Below are list ant tasks:(refer http://ant.apache.org/manual/index.html at toc Ant Tasks)
- Archive Tasks
- Audit/Coverage Tasks
- Compile Tasks
- Deployment Tasks
- Documentation Tasks
- EJB Tasks
- Execution Tasks
- File Tasks
- Java2 Extensions Tasks
- Logging Tasks
- Mail Tasks
- Miscellaneous Tasks
- Pre-process Tasks
- Property Tasks
- Remote Tasks
- SCM Tasks
- Testing Tasks
create simple xml for building above simple application with ant
- step1: create build.xml(the same structure of makefile in linux)
<project> <target name="clean"> <delete dir="build"/> </target> <target name="compile"> <mkdir dir="build/classes"/> <javac srcdir="src" destdir="build/classes"/> </target> <target name="jar"> <mkdir dir="build/jar"/> <jar destfile="build/jar/helloworld.jar" basedir="build/classes"> <manifest> <attribute name="Main-Class" value="oata.helloworld"/> </manifest> </jar> </target> <target name="run"> <java jar="build/jar/helloworld.jar" fork="true"/> </target> </project>
⇒ list targets in build.xml:
clean, compile, jar, run
⇒ and list tasks in build.xml:
delete, mkdir, javac, jar, java
- step2: build with ant and run
set PATH=%PATH%;D:\tools\apache-ant-1.8.4\bin;d:\tools\jdk1.7.0_67\bin ant compile ant jar ant run
Enhance the build file
- Step1: custom enhance-build.xml with content below:
<project name="helloworld" basedir="." default="main"> <property name="src.dir" value="src"/> <property name="build.dir" value="build"/> <property name="classes.dir" value="${build.dir}/classes"/> <property name="jar.dir" value="${build.dir}/jar"/> <property name="main-class" value="oata.helloworld"/> <target name="clean"> <delete dir="${build.dir}"/> </target> <target name="compile"> <mkdir dir="${classes.dir}"/> <javac srcdir="${src.dir}" destdir="${classes.dir}"/> </target> <target name="jar" depends="compile"> <mkdir dir="${jar.dir}"/> <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}"> <manifest> <attribute name="Main-Class" value="${main-class}"/> </manifest> </jar> </target> <target name="run" depends="jar"> <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/> </target> <target name="clean-build" depends="clean,jar"/> <target name="main" depends="clean,run"/> </project>
- Step2: to build and run, we only run command below:
ant -buildfile enhance-build.xml
Output:
Buildfile: D:\projects\java\simpleapplication\enhance-build.xml clean: [delete] Deleting directory D:\projects\java\simpleapplication\build compile: [mkdir] Created dir: D:\projects\java\simpleapplication\build\classes [javac] D:\projects\java\simpleapplication\enhance-build.xml:12: warning: 'includeantruntime' was not set, defaultin g to build.sysclasspath=last; set to false for repeatable builds [javac] Compiling 1 source file to D:\projects\java\simpleapplication\build\classes jar: [mkdir] Created dir: D:\projects\java\simpleapplication\build\jar [jar] Building jar: D:\projects\java\simpleapplication\build\jar\helloworld.jar run: [java] Hello World main: BUILD SUCCESSFUL Total time: 1 second
external libraries with ant
Update helloworld.java for using log4j external library:
package oata; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class helloworld { static Logger logger = LogManager.getLogger(helloworld.class); public static void main(String[] args) { System.out.println("Hello World"); logger.error("Hello World"); } }
to use external lib log4j, we update the build.xml follow changes below:
And full content of build.xml ater change:
<project name="helloworld" basedir="." default="main"> <property name="lib.dir" value="lib"/> <path id="classpath"> <fileset dir="${lib.dir}" includes="**/*.jar"/> </path> <property name="src.dir" value="src"/> <property name="build.dir" value="build"/> <property name="classes.dir" value="${build.dir}/classes"/> <property name="jar.dir" value="${build.dir}/jar"/> <property name="main-class" value="oata.helloworld"/> <target name="clean"> <delete dir="${build.dir}"/> </target> <target name="compile"> <mkdir dir="${classes.dir}"/> <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath"/> </target> <target name="jar" depends="compile"> <mkdir dir="${jar.dir}"/> <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}"> <manifest> <attribute name="Main-Class" value="${main-class}"/> </manifest> </jar> </target> <target name="run" depends="jar"> <java fork="true" classname="${main-class}"> <classpath> <path refid="classpath"/> <path location="${jar.dir}/${ant.project.name}.jar"/> </classpath> </java> </target> <target name="clean-build" depends="clean,jar"/> <target name="main" depends="clean,run"/> </project>
Using Eclipse to import Ant build
maven
refer: http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
Maven, a Yiddish word meaning accumulator of knowledge, was originally started as an attempt to simplify the build processes in the Jakarta Turbine project
Maven’s primary goal is to allow a developer to comprehend the complete state of a development effort in the shortest period of time. In order to attain this goal there are several areas of concern that Maven attempts to deal with:
- Making the build process easy
- Providing a uniform build system
- Providing quality project information
- Providing guidelines for best practices development
- Allowing transparent migration to new features
create simple project with maven
- Step1: prepare environment
set PATH=%PATH%;D:\tools\apache-maven-3.2.3\bin;D:\tools\jdk1.7.0_67\bin set JAVA_HOME=D:\tools\jdk1.7.0_67\ mvn --version
- Step2: create simple project
mvn archetype:generate -DgroupId=com.mycompany -DartifactId=simplemaven -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
And the maven will create the directory tree below:
simplemaven |-- pom.xml `-- src |-- main | `-- java | `-- com | `-- mycompany | `-- App.java `-- test `-- java `-- com `-- mycompany `-- AppTest.java
- Step3: Build
cd simplemaven mvn package
- Step4: Run
java -cp target\simplemaven-1.0-SNAPSHOT.jar com.mycompany.App
Output:
Hello World!
using external library log4j
edit config to use log4j in pom.xml
<dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.0</version> </dependency> </dependencies>
And below are full pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany</groupId> <artifactId>mavenexternaljar</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>mavenexternaljar</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.0</version> </dependency> </dependencies> </project>
Maven Phases(Or targets)
Although hardly a comprehensive list, these are the most common default lifecycle phases executed:
- validate: validate the project is correct and all necessary information is available
- compile: compile the source code of the project
- test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
- package: take the compiled code and package it in its distributable format, such as a JAR.
- integration-test: process and deploy the package if necessary into an environment where integration tests can be run
- verify: run any checks to verify the package is valid and meets quality criteria
- install: install the package into the local repository, for use as a dependency in other projects locally
- deploy: done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
There are two other Maven lifecycles of note beyond the default list above. They are:
- clean: cleans up artifacts created by prior builds
- site: generates site documentation for this project
Base on mavent phases, we can run command below to clean all files which are generated by maven by prior builds:
mvn clean
Maven 2.x Integration for Eclipse
refer: http://maven.apache.org/eclipse-plugin.html
Go to help→Install New Software: http://download.eclipse.org/technology/m2e/releases
M2E: The Maven Integration for Eclipse is the official Eclipse project aimed at integrating Maven within the Eclipse IDE. It is released under the EPL 1.0 license. Features include:
- Launching Maven builds from within Eclipse
- Dependency management for Eclipse build path based on Maven’s pom.xml
- Resolving Maven dependencies from the Eclipse workspace without installing to local Maven repository
- Automatic downloading of the required dependencies and sources from the remote Maven repositories
- Wizards for creating new Maven projects, pom.xml and to enable Maven support on existing projects
- Quick search for dependencies in remote Maven repositories
- Quick fixes in the Java editor for looking up required dependencies/jars by the class or package name
- Integration with other Eclipse tools, such as WTP, AJDT, Mylyn, Subclipse and others.
- M2E dynamically integrates with your Maven projects with Eclipse while you make changes in the IDE. As you change dependencies, or configurations of Maven plugins in your POMs M2E, will synchronize the Eclipse workspace with those changes.
maven attach source and javadoc artifacts
config maven source plugin in pom.xml
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin>
config maven javadoc plugin in pom.xml
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <executions> <execution> <id>attach-javadocs</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin>
wrap maven with full jar files
run command below:
mvn wrap