Java Spring MVC (i) Configuring the MAVEN environment

Source: Internet
Author: User
Tags object model maven central

MAVEN is a project management tool that includes a project object model, a standard set, a project life cycle, a dependency management system


1. Download maven:http://maven.apache.org/download.cgi, and unzip, my version is apache-maven-3.3.3

Setting environment variables

Create M2_home: Unpack the path of the package

Edit path Plus:%maven_home%\bin;

Adhere to whether the configuration is successful, in the terminal input mvn-v, display the MAVEN version number and other information to prove the successful installation.



The default repository for MAVEN is in the. M2 folder in the home directory.




It's best to set up your own warehouse address to avoid taking up too much space on the system disk, my D:\maven\repos. This requires modifying the settings.xml under apache-maven-3.3.3\conf:

The line with the red label


<!--localrepository   | The path to the local repository maven would use to store artifacts.   Default: ${user.home}/.m2/repository  <localRepository>/path/to/local/repo</localRepository>  --><span style= "color: #ff0000;" >  <localRepository>D:\maven\repos</localRepository></span>


The mirror source for Maven adding resources by default is the MAVEN central repository, and we can also add other image sources that are faster to download at settings.xml:

  <mirrors>    <!--mirror     | Specifies a repository mirror site to use instead of a given repository. The repository that | This mirror serves have an ID, that matches, the     mirrorof element of this mirror. IDs is used     | For inheritance and direct lookup purposes, and must is unique across the set of mirrors.     |    <mirror>      <id>mirrorId</id>      <mirrorOf>repositoryId</mirrorOf>      < Name>human readable Name for this mirror.</name>      <url>http://my.repository.com/repo/path</ url>    </mirror>    <span style= "color: #ff0000;" ><mirror>      <id>UK</id>      <name>uk central</name>      <url>http:/ /uk.maven.org/maven2</url>      <mirrorOf>central</mirrorOf>    </mirror></span >  </mirrors>

After the modification, copy the Setting.xml to your warehouse address.


2. Create the first MAVEN project HelloWorld. In the terminal switch to your working directory (preferably Eclipse workspace, easy to see), my is D: \workspace\mavenpro

Input:

MVN archetype:generate-dgroupid=com.mycompany.firstpro-dartifactid=helloworld-dpackage= Com.mycompany.firstpro.helloworld-dversion=1.0-snapshot-dinteractivemode=false-darchetypecatalog=internal

The order is too long to see the blindfolded? Explain:

Archetype:generate command to generate a project, archetype is the name of a plugin.

GroupId: A factory's unique identifier in the global, usually the project name.

Artifactid: Define a module in the actual project

Package: Indicates a packaging task, typically a project name + module name

Version: versions, stable and snapshot versions, here is the snapshot version

Interactivemode:

Archetypecatalog: Indicates that the plug-in uses chetype metadata, the default value is Remote,local, which is the central warehouse archetype metadata plus the plug-in metadata. Setting it to internal specifies that you do not fetch the catalog from a remote server, allowing you to quickly build projects in seconds. Otherwise, if your network is poor, it will be stuck in the [INFO] generating project in Interactive mode this prompt command.

It takes longer to create a project for the first time because you want to download a lot of dependent jar packages to your warehouse.

See the following information to prove that your project was created successfully

You can open the working directory to see what files maven has created for us.

Let's use Eclipse to import this project to see what kind of structure

The MAVEN project uses the " contract better than the configuration " principle,Src/main/java conventions for storing source code,src/main/test for unit test code,src/ Target is used to store compiled, packaged output files. This is a fixed structure.


3. Compiling the project

Enter the HelloWorld project at the terminal, input mvn clean compile

The class file is automatically generated in the target directory after compilation.


4. Unit Testing

MVN Clean Test

From the output can be found, before test, will be executed compile, that is, compile first, then perform unit test.

Interested friends, can modify the contents of the next/src/test/java/com.mycompany.helloworld/apptest.java, put the TestApp () method in the Asserttrue (true); Change to Asserttrue (false); Run the unit test again to see what's different.


5. Project Packaging

Typically we package Java projects into jar packages or war packages, and the commands packaged in Maven are

MVN Package

After running, the jar package is generated in the target directory

From the output can be found, the package will be executed before the compile, then execute test, and finally packaging packages.


6. Integration with Maven in eclipse

Installing the M2E Plugin

Select Help--install New Software in the Eclipse toolbar, click Add

Name:m2e

Location:http://download.eclipse.org/technology/m2e/releases

Click Next to select Maven integration for eclipse,next--next--consent agreement--finish

Wait for the installation to restart Eclipse. Perferences, MAVEN---installations, window---Add your Maven decompression path.

Create Project----New--Maven project, file---Next

Select the default Maven project, direct next

Here is the information for the MAVEN project, which corresponds to the previous command line.

It was created the same as our command line after the creation was completed.

Right-click Project Run As--maven Build ... Enter the clean compile in the goal to compile the

Input test, package is tested, packaged.





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Spring MVC (i) Configuring the MAVEN environment

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.