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 [2014/12/19 06:47] – [Enhance the build file] admin | java:build-java-with-mavent-and-ant [2015/08/07 01:16] (current) – removed admin | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== maven and ant ====== | ||
- | ===== 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 ==== | ||
- | * compile:< | ||
- | javac -cp " | ||
- | </ | ||
- | * 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 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 | ||
- | </ | ||
- | ==== 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 ==== | ||
- | - 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! | ||
- | </ | ||
- | |||
- | ==== 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, |
java/build-java-with-mavent-and-ant.1418971647.txt.gz · Last modified: 2022/10/29 16:15 (external edit)