Software Development Engineer (JAVA) Intermediate exam syllabus-------IV: (iii) MAVEN Introduction and Learning

Source: Internet
Author: User
Tags jboss


MavenMAVEN is a software project management tool that is based on a Project object model (POM) that can manage the construction, reporting, and documentation of a project through a short description of the information. In addition to its program building capabilities, Maven offers advanced project management tools. Because MAVEN's default build rules are highly reusable, it is common to build scripts with two or three lines of Maven to build simple projects. Thanks to MAVEN's project-oriented approach, many Apache Jakarta projects use Maven, and the percentage of company projects that are MAVEN-based continues to grow.

MAVEN is a software project management tool that is based on a Project object model (POM) that can manage the construction, reporting, and documentation of a project through a short description of the information. The word maven comes from Yiddish, meaning the accumulation of knowledge, which was first used in the Jakata turbine project to try to simplify the construction process. There were many projects at the time, their ant build files were only slightly different, and the jar files were maintained by CVS. So the Maven founders started MAVEN, a clear definition of the project, including a convenient way to publish project information, and a way to share jars across multiple projects.

1featuresSo what's the difference between Maven and Ant? Before you answer this question, the first thing to emphasize is that Maven and Ant are two different aspects of building problems. ANT provides cross-platform build tasks for Java technology development projects. Maven itself describes the advanced aspects of the project, which borrows most of the build tasks from Ant. Therefore, since Maven and Ant represent two very different tools, the next step is to explain the difference between the two tools ' equivalent components, as shown in table 1

Maven Ant
Standard build File Project.xml and Maven.xml Build.xml
attribute processing order
  1. ${maven.home}/bin/ Driver.properties
  2. ${project.home}/project.properties
  3. Li class= "list-num-1-3 list-num-paddingleft-1" >${project.home}/build.properties
  4. ${user.home}/build.properties
  5. via-D The system attributes defined by the command-line options
Last definition are decisive.
  1. system attributes defined by the-d command-line option
First definition are processed first.
Building rules Build rules are more dynamic (similar to programming languages); they are executable XML based on Jelly. Build rules are more or less static, unless you use <script> tasks
Extended language Plugins are written in Jelly (XML). Plugins are written in the Java language.
Build Rule Extensibility The build goal is extensible by defining <preGoal> and <postGoal>. Building rules are difficult to scale, and can be simulated by using <script> tasks <preGoal> and <postGoal> roles.

MAVEN is a project management tool that contains a project object model, a set of standard collections, a Project life cycle (Project Lifecycle), a dependency management system (Dependency Management System), and the logic used to run the plug-in (plugin) target (goal) defined in the life cycle phase (phase). When you use MAVEN, you use a well-defined project object model to describe your project, and maven can apply crosscutting logic that comes from a set of shared (or custom) plug-ins. Maven has a life cycle that is called when you run the MVN install. This command tells Maven to perform a series of sequential steps until it reaches the life cycle you specify. One of the implications of traversing the life cycle journey is that Maven runs many of the default plug-in targets, which complete tasks like compiling and creating a JAR file. In addition, MAVEN can easily help you manage project reports, build sites, manage jar files, and more

2Common CommandsMVN archetype:create Creating a maven project MVN compile compiling source code mvn deploy release project MVN test-compile compiling test source code MVN test run the unit test in the application MVN site build project related Information about the site MVN clean clears the build results in the project directory MVN package installs JARMVN eclipse:eclipse in the local repository based on the JARMVN install generated by the project Build the Eclipse project file Mvnjetty:run start the Jetty service Mvntomcat:run start the Tomcat service mvn clean package-dmaven.test.skip=true: RePack the previous package, Skip Test class
3Recommended BooksThe publication of the Chinese version of Maven introduced a comparison of the book is "Maven Combat"4 Win7 ConfigurationThe first thing to install is Jdk1.6 and Myeclipse7.5. 1. MAVEN has an important document "Settings.xml", this file from apache-maven-2.0.8/conf/, to be placed under window, Win7 and WinXP still a little different, Win7---C:\Users\ User name \.m2, WINXP---C:\Documents and settings\ user name \.m2. 2. Set up the MAVEN environment variable, basically similar to setting the JDK environment variable, first set maven_home, then add path. 3. Modify the "settings.xml" file to find the <localRepository>d:/work</localRepository> in this file and make changes. This line represents the location of MAVEN's jar-Package warehouse. Work is decentralized with the jar packages that the project uses, and each project can share one work. 4. Myeclipse7.5 and above itself comes with the Maven plugin. If you need to install a plugin using a lower version of MyEclipse, select software Updates from the Help menu, select Find and install, select Search for new features to install-select New remote Site Write Name:maven url:http://M2eclipse.codehaus. org/update/to download the relevant jar packages and plugins. 5. In MyEclipse windows-perferences, in the location, see if the path to the users settings is correct. 6. Build maven Project,file--new-other-java maven Project. 7. Then open project Pom.xml file,
<projectxmlns= "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.0http://maven.apache.org/maven-v4_0_0.xsd" ><modelversion >4.0.0</modelversion><groupid>maventest</groupid><artifactid>maventest</ Artifactid><packaging>jar</packaging><version>0.0.1-snapshot</version><name> mavenquickstartarchetype</name><url>http://maven.apache.org</url><dependencies>< Dependency><groupid>junit</groupid><artifactid>junit</artifactid><version> 3.8.1</version><scope>test</scope></dependency></dependencies></project>

Add the name of the jar that Maven relies on in this XML file, which is the Add <dependency></dependency> node. For example: Add struts jar packages that you commonly use
<dependency><groupid>org.apache.struts</groupid><artifactid>struts2-core</ Artifactid><version>2.0.11</version></dependency>

8. By the time of posting, the environment has been settled. The next step is to apply MAVEN to package project for everyone. command in CMD: A. MVN Eclipse:clean Clear the previous compilation in Project, re-come to B. MVN Eclipse:eclipse starts compiling MAVEN's project in MyEclipse: A. Select Maven Project Right-click in the Run as to select Maven Cleanb. In MyEclipse, Project-clean starts compiling C. Selecting Maven Project Right-clicking in run as selects Maven install to finish these steps, and if nothing happens, it will generate a target folder in Project, which is what Maven has packaged and released.5Life cycleThe life cycle of MAVEN is to abstract all the building processes and facilitate unification. CleanThis life cycle is designed to clean up the project, which consists mainly of the following stages: Pre-clean-Perform the work required before the project is cleaned up. Clean-Clean up the files generated by the last build project. Post-clean-Perform the work required to complete the project cleanup. Default Validate-Verify that the project is correct and that all necessary information is available. Initialize-Initialize the build work, such as: Set parameters, create directories, etc. generate-sources-Generate source code for code that is contained within the compilation scope. process-sources-process source code, such as filter values. generate-resources- process-resources-Copy and process the resource files to the target directory and prepare the package. Compile-Compile the source code in the project. process-classes-post-work for compiling generated files, such as bytecode enhancement for Java classes. generate-test-sources-Generate test source code for compiled content. process-test-sources-Process the test source code. generate-test-resources- process-test-resources-Copy and process the resource file to the target test directory. Test-compile-Compile the required test source code to the path. In general, it is the test classpath directory that compiles the Java files in the/src/test/java directory to the target output. process-test-classes- Test-Run tests using the appropriate unit test framework. These test codes are not packaged or deployed. Prepare-package- Package-Accept compiled code and package it in a ready-to-publish format, such as a JAR. pre-integration-test- integration-test-Deploy the release package to the running environment on demand. post-integration-test- Verify- Install-Installs the package to the local repository and provides dependencies to other local references. Deploy-Complete integration and release work and copy the final package to the remote repository for sharing with other developers. Site (sites) Pre-site-Perform some preparatory work before building the project site. site-Generate documentation for the project site. Post-site-Perform the work required to complete site builds, such as the preparation of your site deployment. Site-deploy-Generate files to the deployed Web server deployment site.


Maven Tutorials

Maven is an open source project management tool written in pure java. MAVEN uses a concept called Project object Model (POM) to manage projects, and all project configuration information is defined in a file called Pom.xml, through which Maven can manage the entire declaration cycle of a project, including compiling, building, testing , publishing, reporting, and so on. Currently, most of Apache's projects have been managed using MAVEN. MAVEN itself also supports a variety of plugins that allow for more flexible control of the project.

http://maven.apache.org/download.html Download the latest version of Maven 3.0.2 (Binary Zip)

2: Unzip to D:


3: Configure Environment variables
maven_home:d:\apache-maven-3.0.2
MAVEN:%maven_home%\bin
(optional) Maven_opts:-xms256m-xmx512m

Add%maven% in front of path;


4: Verify the installation is successful
On the command line, enter: mvn-version; Enter, as shown below to indicate successful installation:

5: Create Project
On the command line, enter: MVN Archetype:create-dgroupid=com.mycompany.app-dartifactid=my-app Enter,

If you are running the command for the first time (goal), MAVEN will take some time to download the latest toolkit (MAVEN calls it artifacts) to your local repository.

After the command executes you will see that MAVEN has generated a directory named My-app, which is the artifactid you specified in the command, and you will find the following standard project structure:

Where: The Src/main/java directory contains the project's source code, the Src/test/java directory contains the test code for the project, and Pom.xml is the Project object Model (project project or POM).

6:pom
The Pom.xml file is a core configuration of Maven for a project that will contain most of the configuration information you want to build your project. Pom is big and complex, but you don't have to know all about it, just use some common configurations. The contents of this pom are listed below:

Reference


<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/xsd/maven-4.0.0.xsd" > < Modelversion>4.0.0</modelversion> <groupId>com.mycompany.app</groupId> <artifactId> My-app</artifactid> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> < Name>my-app</name> <url>http://maven.apache.org</url> <properties> < Project.build.sourceencoding>utf-8</project.build.sourceencoding> </properties> <dependencies      > <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies>&lt ;/project>

7:5th Step what we have done

Reference

MVN Archetype:create-dgroupid=com.mycompany.app-dartifactid=my-app

Executes the MAVEN command (goal) Archetype:create, and sets some parameters (-dgroupid=com.mycompany.app-dartifactid=my-app)

In this command, the prefix archetype is a maven plugin that contains the Create command. This goal command builds a simple project based on the project prototype (MAVEN-compliant project template).

It is now OK to say that a MAVEN plugin is a collection of goals commands with the same purpose, such as the Jboss-maven-plugin plugin, to handle a variety of JBoss-related tasks.

8:build Project
On the command line enter: CD My-app Enter, enter under Project path


Then enter MVN package Enter the command line will print a variety of actions, and end with the following paragraph of information:

With the first execution of the command ( that's a goal)

Reference
Archetype:create
Different, this is just a simple command---package. Unlike the goal, This is a phase (stage), a phase refers to a stage in the construction life cycle, which refers to an orderly series of phase. When given a phase,maven will perform all the phase and its own before this phase, for example, if we perform the compile phase, the actual execution phase is:

Reference


You can use the following commands to test the newly compiled and packaged jar packages.

Reference
JAVA-CP Target/my-app-1.0-snapshot.jar com.mycompany.app.App

This will print out the most classic:
Reference

9: Run the Maven tool

Although it is difficult to list a very comprehensive table, here you can first list the most common default life cycle stages:

Reference
Validate: Verify that the project is correct and that all required resources are available. Compile: Compile the source code for the project.  


The individual phases (stages) listed above actually correspond to potential goals, and special goals are determined by the type of project, for example, if the type of the project is the Jar,package stage, Jar:jar goals will be executed by default. If the project type is war, then the goals of the package phase will be war:war.
One interesting thing to note is that phases and goals need to be executed in a certain order.
MVN Clean Dependency:copy-dependencies Package
This command clears the project first, then copies the dependencies, finally packages the project, and, of course, executes the previous phase of the phase before packaging. such as Compile,test.
Build site
MVN site
This phase generates project information based on the Pom.xml configuration. You can see the resulting document in the Target/site directory.

Software Development Engineer (JAVA) Intermediate exam syllabus-------IV: (iii) MAVEN Introduction and Learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.