java:build-java-with-mavent-and-ant
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| java:build-java-with-mavent-and-ant [2015/08/07 01:09] – [create simple project with maven] admin | java:build-java-with-mavent-and-ant [2015/08/07 01:16] (current) – removed admin | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== maven and ant ====== | ||
| - | ===== prepare environment variables for building java with ant and maven ====== | ||
| - | * add below paths to PATH environment variable in windows< | ||
| - | d: | ||
| - | </ | ||
| - | * add new environment variable JAVA_HOME< | ||
| - | d: | ||
| - | </ | ||
| - | ===== 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 | ||
| - | </ | ||
| - | - step2: create simple application **src\oata\helloworld.java**:< | ||
| - | package oata; | ||
| - | public class helloworld { | ||
| - | public static void main(String[] args) { | ||
| - | System.out.println(" | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | - step3:build and run<code bat> | ||
| - | set PATH=%PATH%; | ||
| - | 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> | ||
| - | 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(" | ||
| - | logger.error(" | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | * compile:< | ||
| - | D: | ||
| - | </ | ||
| - | * run:< | ||
| - | java -cp " | ||
| - | </ | ||
| - | ===== Ant ===== | ||
| - | refer: | ||
| - | * http:// | ||
| - | * refer: https:// | ||
| - | 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: | ||
| - | * Archive Tasks | ||
| - | * Audit/ | ||
| - | * 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)< | ||
| - | < | ||
| - | <target name=" | ||
| - | <delete dir=" | ||
| - | </ | ||
| - | <target name=" | ||
| - | <mkdir dir=" | ||
| - | <javac srcdir=" | ||
| - | </ | ||
| - | <target name=" | ||
| - | <mkdir dir=" | ||
| - | <jar destfile=" | ||
| - | < | ||
| - | < | ||
| - | </ | ||
| - | </ | ||
| - | </ | ||
| - | <target name=" | ||
| - | <java jar=" | ||
| - | </ | ||
| - | </ | ||
| - | </ | ||
| - | clean, compile, jar, run | ||
| - | </ | ||
| - | delete, mkdir, javac, jar, java | ||
| - | </ | ||
| - | - step2: build with ant and run<code bat> | ||
| - | set PATH=%PATH%; | ||
| - | ant compile | ||
| - | ant jar | ||
| - | ant run | ||
| - | </ | ||
| - | ==== Enhance the build file ==== | ||
| - | - Step1: custom enhance-build.xml with content below: <code xml> | ||
| - | <project name=" | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | <target name=" | ||
| - | <delete dir=" | ||
| - | </ | ||
| - | <target name=" | ||
| - | <mkdir dir=" | ||
| - | <javac srcdir=" | ||
| - | </ | ||
| - | <target name=" | ||
| - | <mkdir dir=" | ||
| - | <jar destfile=" | ||
| - | < | ||
| - | < | ||
| - | </ | ||
| - | </ | ||
| - | </ | ||
| - | <target name=" | ||
| - | <java jar=" | ||
| - | </ | ||
| - | <target name=" | ||
| - | <target name=" | ||
| - | </ | ||
| - | </ | ||
| - | - Step2: to build and run, we only run command below:< | ||
| - | ant -buildfile enhance-build.xml | ||
| - | </ | ||
| - | Buildfile: D: | ||
| - | |||
| - | clean: | ||
| - | | ||
| - | |||
| - | compile: | ||
| - | [mkdir] Created dir: D: | ||
| - | [javac] D: | ||
| - | g to build.sysclasspath=last; | ||
| - | [javac] Compiling 1 source file to D: | ||
| - | |||
| - | jar: | ||
| - | [mkdir] Created dir: D: | ||
| - | [jar] Building jar: D: | ||
| - | |||
| - | run: | ||
| - | | ||
| - | |||
| - | 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(" | ||
| - | logger.error(" | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | to use external lib log4j, we update the build.xml follow changes below: | ||
| - | {{: | ||
| - | And full content of build.xml ater change:< | ||
| - | <project name=" | ||
| - | < | ||
| - | <path id=" | ||
| - | <fileset dir=" | ||
| - | </ | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | <target name=" | ||
| - | <delete dir=" | ||
| - | </ | ||
| - | <target name=" | ||
| - | <mkdir dir=" | ||
| - | <javac srcdir=" | ||
| - | </ | ||
| - | <target name=" | ||
| - | <mkdir dir=" | ||
| - | <jar destfile=" | ||
| - | < | ||
| - | < | ||
| - | </ | ||
| - | </ | ||
| - | </ | ||
| - | <target name=" | ||
| - | <java fork=" | ||
| - | < | ||
| - | <path refid=" | ||
| - | <path location=" | ||
| - | </ | ||
| - | </ | ||
| - | </ | ||
| - | <target name=" | ||
| - | <target name=" | ||
| - | </ | ||
| - | </ | ||
| - | ==== Using Eclipse to import Ant build ==== | ||
| - | {{: | ||
| - | ===== maven ===== | ||
| - | refer: http:// | ||
| - | 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 ==== | ||
| - | Create basic simple maven: | ||
| - | - Step1: prepare environment< | ||
| - | set PATH=%PATH%; | ||
| - | set JAVA_HOME=D: | ||
| - | mvn --version | ||
| - | </ | ||
| - | - Step2: create simple project< | ||
| - | mvn archetype: | ||
| - | </ | ||
| - | simplemaven | ||
| - | |-- pom.xml | ||
| - | `-- src | ||
| - | |-- main | ||
| - | | `-- java | ||
| - | | `-- com | ||
| - | | `-- mycompany | ||
| - | | `-- App.java | ||
| - | `-- test | ||
| - | `-- java | ||
| - | `-- com | ||
| - | `-- mycompany | ||
| - | `-- AppTest.java | ||
| - | </ | ||
| - | - Step3: Build< | ||
| - | cd simplemaven | ||
| - | mvn package | ||
| - | </ | ||
| - | - Step4: Run<code bat> | ||
| - | java -cp target\simplemaven-1.0-SNAPSHOT.jar com.mycompany.App | ||
| - | </ | ||
| - | Hello World! | ||
| - | </ | ||
| - | Create new class and run it in simple maven: | ||
| - | - Step1: Create new class Helloworld.java in **simplemaven\src\main\java\com\mycompany\**< | ||
| - | package com.mycompany; | ||
| - | |||
| - | /** | ||
| - | * Hello world! | ||
| - | * | ||
| - | */ | ||
| - | public class Helloworld | ||
| - | { | ||
| - | public static void main( String[] args ) | ||
| - | { | ||
| - | System.out.println( "Hello World!" | ||
| - | } | ||
| - | } | ||
| - | |||
| - | </ | ||
| - | - Step2: Rebuild the simplemaven:< | ||
| - | mvn package | ||
| - | </ | ||
| - | - Step3: Run Helloworld:< | ||
| - | java -cp target\simplemaven-1.0-SNAPSHOT.jar com.mycompany.Helloworld | ||
| - | </ | ||
| - | Hello World! | ||
| - | </ | ||
| - | ==== using external library log4j ==== | ||
| - | edit config to use log4j in pom.xml< | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | </ | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | </ | ||
| - | </ | ||
| - | </ | ||
| - | And below are full pom.xml:< | ||
| - | <project xmlns=" | ||
| - | xsi: | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | </ | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | </ | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | </ | ||
| - | </ | ||
| - | </ | ||
| - | </ | ||
| - | ==== Maven Phases(Or targets) ==== | ||
| - | Although hardly a comprehensive list, these are the most common default lifecycle phases executed: | ||
| - | * **validate**: | ||
| - | * **compile**: | ||
| - | * **test**: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed | ||
| - | * **package**: | ||
| - | * **integration-test**: | ||
| - | * **verify**: run any checks to verify the package is valid and meets quality criteria | ||
| - | * **install**: | ||
| - | * **deploy**: done in an integration or release environment, | ||
| - | 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:// | ||
| - | Go to help-> | ||
| - | |||
| - | 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/ | ||
| - | * 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, | ||
| - | ==== maven attach source and javadoc artifacts ==== | ||
| - | config maven source plugin in pom.xml< | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | </ | ||
| - | </ | ||
| - | </ | ||
| - | </ | ||
| - | </ | ||
| - | config maven javadoc plugin in pom.xml< | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | </ | ||
| - | </ | ||
| - | </ | ||
| - | </ | ||
| - | </ | ||
| - | ==== wrap maven with full jar files ==== | ||
| - | run command below:< | ||
| - | mvn wrap | ||
| - | </ | ||
java/build-java-with-mavent-and-ant.1438909799.txt.gz · Last modified: (external edit)
