Getting Started with maven (in Eclipse IDE)

Source: Internet
Author: User
Tags maven central

NetBeans version of Portal: http://tieba.baidu.com/p/2835875340
Preparation matters:
Eclipse or myeclipse One, need to install MAVEN plugin
If you do not install the Maven plugin and download the maven version yourself locally, eclipse/myeclipse can also use a non-plug-in version of Maven.
Attached to maven: http://maven.apache.org/download.cgi
PS If you use a non-plug-in version of MAVEN, you need to configure the MAVEN environment variable, configured as follows
Variable name: m2_home
Variable Value: C:\apache-maven-3.0.5 (This is my decompression directory, please modify your own)
Variable Name: path
Value:%m2_home%\bin (Learn Java should know that there are many other values in the path, please append yourself)
After configuration, open cmd, enter mvn-v
If the following screen appears, the MAVEN environment variable is configured successfully

Open Eclipse or myeclipse configuration maven plugin, I use the myeclipse version, Eclipse should be the same location, find the MAVEN option.
You see my myeclipse comes with a version of 3.0.2 maven, but what I really use is to download and unzip it to the C drive maven3.0.5

Here's how to build a simple Maven project with eclipse
Right-click New, select Other, enter Maven in the filter, the following screen appears, select Maven Project, then click Next

After the next step, choose to use the default workspace (which you can change, of course), and then next

A package type appears, and if it is a normal project, it needs to be packaged into a jar file, select Quickstar (also the option that is selected by default).
If it is a Web project and needs to be packaged into a war package, select WebApp.
Here for a simple demonstration, choose the default


After the selection, next appears in the project configuration.
The group ID is usually the organization name, and the company's general naming method is the company domain name suffix + company Name
Like Com.baidu com.google org.spring or something like that.
The artifact ID is typically the project name and the name of the package that appears after the project is packaged
Version number that is packaged for the project, default is 0.0.1 snapshot version


Dot finish will automatically build a MAVEN project, structured as follows.
With one Main/java main package and one Test/java test package
A pom.xml file

then let's open up this maven core configuration file Pom.xml see what's inside, open the way Maven Pom Editor can see the following
Note there are 5 options tabs below
Overview, which is the project information Overview
dependencies, is a project introduction to the Jar package
dependency hierarchy, which is the mutual dependency of the jar packages introduced by the project
effective Pom, to be honest, I don't know what this is, but I use less ...
Pom.xml, which is the entire contents of this configuration file


below shows you how to add a jar package to your project in 2 ways, first of all.
Open the Dependencies tab, select the Middle Add button


The pop-up window appears, and he lets you fill in the information about the jar package that needs to be introduced, so how do you know what information you want to bring in the jar?

This is the time to find Maven's central repository.
Attach Portal: http://www.mvnrepository.com/
For example, I'm going to introduce Sping's package, just search the top of the spring
and find the jar package you need, like spring Context, Click to enter (Ads please ignore ....)


You will then be able to see all the officially released versions of the jar. I think there's a new 4.0.0. Looks good, just you. Click to enter.


You can see this screen, where you can find all the information about this jar, such as Groupid,artifactid,version

and the required packages on which the jar package is dependent, and the minimum version of the package


Then go back to eclipse, fill in the information, click OK

, and then you can see the console, and Maven will go through the process of downloading the jar package for us and the other jar packages that the jar depends on


. Can see that we only fill in a jar of information, MAVEN will automatically download the minimum dependent jar package, and automatically introduced into the project.


Here's a second way to add a jar package, and it's the most common way.
is to copy this information.
Then in Eclipse, open the pom.xml tag, paste it directly, save ...

The following describes the common commands for compiling packaged projects using Maven
Select Maven project, right-run, select Maven Build, and the following dialog box appears


Where goals can fill in some of their own parameters.
such as testing, installation, cleaning, release, packaging.
Testing: Test. MAVEN will run all the code below using Test/java to test if the project is correct (if you write a method/class, you need to write the corresponding test method)
Cleanup: clean. Maven cleans up all the class files that are generated by the project.
Packaging: Package. MAVEN generates the corresponding package based on the type of project. If Quickstar is a jar package, WebApp is a war package.
Installation: Install. Install the jar package for this project in the local warehouse. This is usually done to facilitate other projects to reference the project.
Release: Deploy. If a MAVEN local central warehousing management system such as Nexus is used, the command will be used to publish the project's package to the Nexus remote repository. Make it easier for others to update your published packages. (This is one of the most commonly used commands within the company, after all, it is a multi-person development project)
Test yourself, play with the clean and the package is almostNow that I've mentioned the Nexus, I'm going to talk to you about it ....
Said a bunch of maven benefits, but Maven still has a few minor drawbacks.
The first one is all the jars you have to download from the central repository of MAVEN, a person's words actually does not matter, anyway to download, but if it is a company, hundreds of developers, You update the pom.xml in the project, and all the other developers have to connect to the MAVEN Remote central repository to download the jar, which is a huge waste of bandwidth.
The second is the bigger problem, the company's internal projects, want to use MAVEN to manage, and need to publish to a warehouse for other departments or other project groups of people to use, this time what to do? It's not always possible to package a project into a MAVEN public repository (MAVEN's public warehouse doesn't allow you to publish it).
The need for a management tool that replaces the MAVEN central repository's local repository can be a perfect replacement for the MAVEN central repository. It's the Nexus.
Attach the portal first: Http://www.sonatype.org/nexus/go
After downloading, unzip to any directory, and then use CMD to enter the C:\nexus-2.6.4-02\bin directory (directory to change)
Input Nexus.bat
Can see the following screen

It will prompt you to use the operation, then first we need to install the Nexus Service (Win7 above system please start cmd with administrator privileges, otherwise the service will not install)
Command format Nexus.bat Install

When the Nexus service is installed, open the system service and see if the service exists.

Can see that the service is already installed, and the default is automatically started, you need to feel unnecessary, you can change to manually start, when needed to start the Nexus
And then say a few other commands.
Console means to start the Nexus service as a console (you can see the output log)
Install means the Nexus service is installed
Start means the Nexus service is started
Stop means the Nexus service is stopped
Uninstall means uninstalling the Nexus service

After the service is started, you can see the Nexus Warehouse by entering the following address in the browser
http://localhost:8081/nexus/


At this point, notice the upper right corner, there is a log in
The default user name password is admin admin123

Log in as Administrator you will see a few changes to the left label, with some new management features



The first repositories in the point, you can see the look of the warehouse you managed with the Nexus.

This time the warehouse is an empty warehouse, if the use of the server to do the entire project team of the Unified warehouse, it is necessary to synchronize the Nexus and Maven central warehouse, so that the project team by configuring local MAVEN files will be the first to download the jar package from this Nexus warehouse, saving bandwidth, The project's jar package is also posted to the Nexus by default. (How to configure Maven configuration file later)

To synchronize the Maven Central warehouse method, select public repositories, select the configuration in the following tab


The default is to subscribe to these 4 maven remote repositories, and simply click Save.

Then select Central, choose Brownse Storage, and expand to see all the jar packages from the MAVEN remote repository. Here's how to configure local maven to connect Maven to the Nexus Repository.


You remember this location, there is a location for the configuration file used below. OK, open the goods.
and find the mirrors tag.


This default is commented out, let's open this comment and configure the image.
There are several labels to fill in.
ID is the ID of this image, just fill in a line, here we fill the Nexus
Name is the image of this mirror, random
Mirrorof which jars need to be downloaded from this image, fill in *
URL is the path of the mirror, fill http://localhost:8081/nexus/content/groups/public, if it is a company server, please modify the IP and port
Here's how to publish your project to the Nexus server
First, you need to build your own warehouse in the Nexus.
Click Add, choose a new hosted repository


In the following deployment policy select Allow Redeploy
Repository Policy Select Snapshot
Click Save to create the complete.
Modify the local MAVEN configuration file


Add server ID configuration, user name and password
Add the following configuration to the pom.xml of the project


Note that the ID of this published warehouse is consistent with the Maven profile ID, and the URL source for this repository is the Nexus,

Once configured, select the MAVEN project, right-run, select Maven Build, and the following dialog box appears

Because we need to publish the project to the Nexus, the parameters are filled with the deploy

Look at the console run out of information, print out the following information is a successful release.
Can see some of the uploaded information

Then we go to the Nexus warehouse and see if it's posted to the warehouse.


At this point, check out our warehouse and find out that the jar package for the project has been published to the server, and the jar package will be displayed on the right to the configuration of the MAVEN reference XML for the jar package. This is the time to copy this configuration to another project's Pom.xml file to automatically download the project's jar package and reference it.

Organized from: http://tieba.baidu.com/p/2836671913

Getting Started with maven (in Eclipse IDE)

Related Article

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.