Learn and organize Maven

Source: Internet
Author: User
Tags sonatype
I. MAVEN Theory
1. What is Maven?

Maven can be translated into "accumulation of knowledge" or "expert" or "expert ". This book will introduce Maven
This cross-platform project management tool. As a fairly successful open-source project in the Apache organization, Maven mainly serves
Java platform project construction, dependency management, and project information management. Whether it's a small open-source class library project or a large enterprise
Industry-level applications; whether it is traditional waterfall development or popular agile mode, Maven can show its talents.

2. What is building?

Whether or not you realize that building is the work that every programmer is doing every day. Come to the company in the morning, what we do
The first thing is to check out the latest source code from the source code library and perform a unit test. If a failed test is found
Work with colleagues to debug and fix the error code. Then we will go back to our work and write our own unit tests and product code.
I am grateful to the compilation error prompt reported by IDE at any time.
At lunch time, the Code was almost written, and the test passed. Have a good lunch and have a rest. In the afternoon
The meeting was closed, and a cup of coffee continued to work after the meeting. At the meeting, the manager asked me to read the test report.
The tool was integrated into the IDE to generate a decent test coverage report, and then sent an email to the Manager, relieved.
The QA Team sent several bugs again, but there was no way to reproduce them locally. So I skillfully used ide to generate a war.
Package, deploy it to the Web container, and start the container. You can see the familiar interface, follow the bug report, and reproduce the bug step by step.
When I got off work, I fixed the bug, submitted the code, and notified the QA Team to finish the day's work in a pleasant period.

3. Do not reinvent the wheel

John is a programmer in a small private software company. His company wants to develop a new web project. After negotiation, we decided to use spring,
Ibatis and tapstry. Where can I find the jar package? It is estimated that no one in the company can use the jar packages used by spring, ibatis, and tapstry in many ways.
Find out. Your practice is to first go to the spring site to find a spring. With. dependencies, and then go to the ibatis website
Download the listed jar package and perform the same operations on tapstry and Apache commons. The project has not started yet.
There are nearly a hundred jar packages. If a jar package with a version number, without a version number, is useful, useless, or conflicted, how can it be a "messy" word!

During project development, Mr. Zhang finds version errors and version conflicts from time to time, and he can only solve them one by one. Half of project development,
The manager found that the size of the final deployed application was too large and asked Mr. Zhang to remove some useless jar packages. Therefore, Mr. Zhang had to work overtime one by one.
Delete ......

John implicitly thinks that these dependencies need to be managed by a framework or system.

Mr. Zhang liked to learn popular technologies. Ant was very popular a few years ago. He learned and became an expert in the company. John knows that ant is playing
The package is nothing more than creating directories, copying files, compiling source code, and using a bunch of tasks, such as copydir, fileset, classpath, ref, and target.
Then, package the jar, zip, and war files.

The Project Manager sent a message: "Boys, the new project is coming, John. You can write the ant script !"

"Yes, ensure that the task is completed !" Then, John continues to create a new XML file. Target clean; Target compile; Target jar ;...... No
I know whether he has ever wondered how many ant scripts he has written are repetitive work and how much code will be reproduced in one project after another.
Since it is almost the same, some are even the same, why should I rewrite it every time?

Finally, one day, James realized this problem and wanted to reuse the ant script. So he said during the meeting: "I will use ant in the future.
Script, the new project only needs to follow the directory structure defined by me ." The manager thought it was quite reasonable: "Well, it is indeed a progress ."

Now the new graduate student said, "Manager, use maven. This is very popular in the open-source community and is more convenient than ant ." James was surprised to hear that,
Is Maven really more powerful than its own "canonicalized ant? In fact, he does not know that he is just re-inventing the wheel. MAVEN already has a plug-in named ready-made by idea,
It is used all over the world. You do not need to write any code yourself!

Why does no one say "I write the most flexible code myself, so I don't need spring, I implement IOC myself; I don't need hibernate, I seal it myself
Install JDBC "?

2. Install Maven
4. decompress the installation file to the specified directory, for example:

D: \ bin> jar xvf "C: \ Users \ juven Xu \ downloads \ apache-maven-3.0--bin.zip"

The Maven installation directory here is D: \ bin \ apache-maven-3.0, and then you need to set environment variables, configure Maven installation to the Operating System
Environment.

Open the System Properties Panel (right-click my computer> properties on the desktop), click Advanced System settings, and then click environment variables in the system
Create a new variable named m2_home. The variable value is the maven Installation Directory D: \ bin \ apache-maven-3.0. Click OK,
Find a variable named path in the system variable and add % m2_home % \ bin at the end of the variable value.
Separate them with semicolons and click OK. Now, environment variable settings are complete.

Here we need to mention the PATH environment variable. When we enter the command in cmd, Windows will first find the executable in the current directory
If the row file or script is not found, Windows will traverse the path defined in the environment variable path. Because we set % m2_home % \ bin
Added to the path, and here % m2_home % actually references another variable we previously defined. Its value is the maven installation directory.
Therefore, Windows searches for the Directory D: \ bin \ apache-maven-3.0 \ bin while executing the command, where MVN executes the script.

Understand the role of environment variables, and now open a new cmd window (the new window is emphasized here because the new environment variable configuration requires a new
), Run the following command to check the installation of Maven:

C: \ Users \ juven Xu> echo % m2_home %

C: \ Users \ juven Xu> MVN-V

5 .~ /. M2

Before proceeding to this section, run a simple command: MVN help: system. This command prints all Java System attributes.
And environment variables, this information is very helpful for our daily programming work. The help: System syntax is not explained here.
The purpose is to allow Maven to execute a real task. We can see from the command line output that MAVEN will download Maven-help-plugin, including
Pom file and JAR file. These files are downloaded to the maven local repository.

Open the user directory now. For example, the current user directory is c: \ Users \ juven Xu \. You can find similar
User directory. For earlier Windows versions, the directory should be similar to c: \ document and Settings \ juven Xu \. In UNIX-based
On the system, enter the CD and press enter to go to the user directory. This book uses symbols for convenience ~ Refers to the user directory.

In the user directory, we can find the. m2 folder. By default, this folder contains the maven local repository. m2/Repository.
All Maven components (artifact) are stored in this repository for reuse. We can
~ /. M2/Repository/org/Apache/Maven/plugins/Maven-help-plugins/find the downloaded Maven-help-plugin pom File
And jar files. Maven determines the location of any component in the repository based on a set of rules. Chapter 1 of this book will be detailed. By
The Maven repository is transparently presented to Maven users through a simple file system. In some cases, you can directly view or modify the repository by bypassing Maven.
Files, which are often useful when encountering problems.

By default ,~ /. M2 directory has no other directories and files except the repository, but most Maven users need
Copy the m2_home/CONF/settings. xml file ~ /. M2/settings. xml. This is a best practice. We will explain it in detail in the last section of this chapter.
Release.

3. Configure Maven in eclipse
6. Now I will take eclipse 3.6 as an example to explain how to install m2eclipse gradually.

After eclipse is started, select help in the menu bar, and then select
Install new software ..., Then you will see an install dialog box. Click the Add button next to the "Work with:" field.
New add repository dialog box. In the Name field, enter m2e and location fields.
Http://m2eclipse.sonatype.org/sites/m2e, then click OK. Eclipse downloads the Resource Information on the m2eclipse installation site. Awaiting funding
After the source is loaded, we can expand it all to see the interface shown in the figure:

Shows Maven integration for eclipse (required), the core module of m2eclipse, and click Next>. eclipse will
Calculate the dependencies between modules, and then provide a list of modules to be installed. After confirming the correctness, click Next>. Then we will see the license.
Information: The open source license used by m2eclipse is eclipse Public License V1.0. Select I accept the terms of the license agreements,
Click Finish, and then wait patiently for eclipse to download and install these modules.

Finally, for m2eclipse installation, you may encounter errors similar to this when using m2eclipse:

09-10-6 01:14:49 A.M.: Eclipse is running in a JRE, but a JDK is required
Some Maven plugins may not work when importing projects or updating source folders.

This is because eclipse runs on JRE by default, while some functions of m2eclipse require JDK. The solution is to configure eclipse
In the eclipse. ini file of the installation directory, add the VM configuration to point to JDK, for example:

-- Launcher. xxmaxpermsize

256 m

-VM

D: \ Java \ jdk1.6.0 _ 07 \ bin \ javaw.exe

 

7. Configure Maven web project in eclipse

Maven is a project management tool that integrates various functions. The following describes the configuration of Maven web project in eclipse and tomcat integration. After the configuration is successful, you can debug it like a common web project.

 

I. Prerequisites

1. Install and download JDK

Jdk1.6 is used as an example.

 

2. Install eclipse

Download Eclipse IDE for Java EE developers from the eclipse official website.

Http://www.eclipse.org/

 

3. install Tomcat 6

 

4. Install Maven

 

5. Install the eclipse Maven plug-in

Here, the installation address is in the form of online installation: http://m2eclipse.sonatype.org/sites/m2e

 

Ii. Configuration

1. Configure the JDK installation location, tomcat installation location, and Maven installation location in eclipse, and specify JDK for Tomcat

Not detailed here

2. Create a Maven project in eclipse

2-1. Create a Maven project and select create a simple project...

 

 

2-2,

Click Next to enter the next

 

Enter the group ID and artifact ID in this window. You can write one at will and select the war type in packaging.

Click Next. In the following steps, continue to next until you finally click Finish.

 

2-3,

Right-click the project and select properites. The following dialog box is displayed.

 

 

Select project facets in the navigation bar on the right of the page, and click the convert faceted from hyperlink to go to the following page.

2-4,

 

 

 

Select m in configuration

Select version 2.5 in the dynamic Web module of Project facet below

Select 1.6 in Java

Note: These options may vary according to tomcat version changes. For Tomcat 6, you can select the above options.

This step is very important. Only after this step is performed will the deployment Assembly link be available in the right navigation bar.

2-5

Next, click the runtime panel on the right.

 

Tomcat is shown below. If no, click New below to create a new one. Select the check box after the new one is created, and then apply and OK.

 

 

2-6,

On the project properties panel, select the deployment Assembly option in the "work" navigation bar and click "Web deployment assembly" on the right.

If the following figure is displayed, the configuration is complete.

 

The above folder is explained here

Src/main/Java

This folder stores the Java source code and copies the class file under this folder to the WEB-INF/classes directory when releasing the project.

 

Src/main/Resources

This folder is usually placed with configuration files, such as XML and properties files. In fact, java files can also be placed, just as agreed.

Files in this folder will also be copied to the WEB-INF/class

As for test, there are some similarities, but these are test code. If you have used Maven, you should know this.

 

Src/main/webapp

In Maven, you must regard this folder as the webroot directory of a common web project. Check the deploy path on the right.

Is released to the root directory. This folder has no WEB-INF/classes, WEB-INF/lib folder in the built Maven WEB Project

Manual setup required

 

Note: For some reason, the preceding view you open may be as follows,

 

In fact, this can also run the project and debug the project. However, if you run the Pom. xml file of the project, an error is reported. Why,

Because Maven regards the src/main/webapp file as the webroot in a common web project, but in your configuration

If no configuration is available, an error is returned.

What should I do? Two Steps

1. Select webcontent and remove it.

2. Create a new file. The source folder is src/main/webapp, And the deploy path is/

Click Apply and OK.

 

Finally, you must map the maven Library to the WEB-INF/lib, the specific operation is as follows, click the Add button to enter

 

Select Java build path entries and click Next to enter

 

 

 

Select Maven dependencies and click Finish, as shown in figure


 

 

If you do not map Maven dependencies to the WEB-INF/lib, you will be prompted not to find the class when the server, such as the library in Maven, is used in the servlet (although you did not have the red XX when writing the code, but the class will not be found when running the program)

 

 

 

Iii. Running

In the Server View of Eclipse, add your project, right-click the Tomcat server, select add and remove, and add the newly created web project. The result is as follows:

Create a servlet in src/main/Java and a JSP in src/main/webapp

 

Start Tomcat and access your Servlet and JSP. In the servlet, you can set breakpoints and debug them.

8. Eclipse import Maven Project

Right-click Eclipse project explorer and select import in the pop-up box, as shown in:

 

Select existing Maven projects and click Next. The displayed dialog box is displayed:



 

Select a created Maven project and click Finish.

Therefore, the maven project is imported successfully.

 

 

9. Run the maven command

Right-click the project and click Run as, for example:

 

 

You can see many existing Maven commands, click to run them, and you can see the running information on the console.

If the maven command you want to run is not found here, click Maven build to create a new command, as shown in:


 

Enter the maven command and click Run.


 

The newly added Maven command can be found as follows and run again:


 

 

Clean package

Common commands

MVN archetype: Create to create a Maven Project
MVN compile source code
MVN test-compile test source code
MVN test run the unit test in the Application
MVN site: the website that generates project-related information
MVN clean clears the generated results in the project directory
MVN package: jar generated by project
MVN install jar in local respository
MVN Eclipse: Eclipse generates Eclipse project files
MVN jetty: Run to start the jetty Service
MVN Tomcat: Run to start the Tomcat Service

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.