Getting started with Maven 1-Create a new MAVEN Web project in Eclipse

Source: Internet
Author: User
Tags aliyun

Create a new MAVEN Web project in Eclipse

Most of the time the development is inefficient, most of the reason is the IDE environment is unfamiliar, configuration is not configured, so before learning a skill, it is necessary to understand the basic environment configuration, is called AX. This article focuses on the first contact with Maven, a researcher who is not familiar with the configuration.

1. Maven Configuration and introduction

MAVEN Official website: http://maven.apache.org/

When I was developing Java Web Engineering, we had to import a lot of dependencies, but as the project became larger and more packages were being managed, it was necessary to use tools to manage the packages so that we didn't have to import them manually; Maven was just trying to solve the problem of managing jars.

as follows, a classic import of SPRINGMVC relies on the configuration:

<Dependencies>    <Dependency>            <groupId>Org.springframework</groupId>            <Artifactid>Spring-webmvc</Artifactid>            <version>3.2.0.RELEASE</version>    </Dependency><Dependencies>

By three coordinate values, we can locate the dependent packages we need;

Introduction to POM file, Official document: http://maven.apache.org/guides/introduction/introduction-to-the-pom.html

The following describes how to configure Maven:

  1) Download maven

HTTP://MAVEN.APACHE.ORG/DOWNLOAD.CGI official website Download Apache-maven-3.5.0-bin.zip, to the local directory, and then unzip, such as put into the E:\JavaWeb directory.

  2) Configure MAVEN environment variables

Many tutorials on this web are the same as configuring Java environment variables. After configuration, enter mvn-v in the command line window: If the version of Maven is displayed, the environment variable is configured successfully;

  3) Maven Basic configuration

Copy the Conf-->settings.xml file from the Maven directory to the C drive and the user-->.m2 folder

  Why did you do it? There are two reasons, 1. The default load MAVEN configuration for Eclipse is to load the settings file under this path; 2. If this is configured later, the configuration will not need to be modified if the version of Maven is later updated. As shown, the second is where the default downloaded package exists, which is the local repository.

    To configure the Settings.xml file:

<!--official recommendations but not available, you need to configure agents <mirror> <id>UK</id> <mirrorOf>central</mirrorOf> <na Me>uk central</name> <url>http://uk.maven.org.maven2</url></mirror> -<!--use a domestic mirror - <Mirror>      <ID>Alimaven</ID>      <name>Aliyun maven</name>      <URL>http://maven.aliyun.com/nexus/content/groups/public/</URL>      <mirrorof>Central</mirrorof>        </Mirror>

  This basic configuration is done, and more on the next article;

  4) Use the command to create and publish a Web project

    Go to the directory where we want to create a new project, this is E:\JavaWeb\eclipse-neon\workspace

MVN archetype:generate-dgroupid=ecnu.liuning-dartifactid=springmvc-study-darchetypeartifactid= Maven-archetype-webapp

    In the middle of the message, click Enter, if there is an error, a large part of the reason is because the mirror configuration error, unable to download the dependency package, search for the wrong content to find a solution.

If the last hint of build success proves the new success;

This is the same as using Eclipse new, you can import the test with Eclipse, if the following error occurs after importing eclipse:

    

    Because there is no running environment to add Tomcat server: Right click on the project, select Config BuildPath and then follow the selection; after adding, modify the contents of the file and save again , let him reload.

Switch to the Springmvc-study directory at the command line, execute: MVN The package was successfully built, a target directory was added to the Springmvc-study directory, This directory will be packaged into My-web-app.war, and the war package can be copied to Tomcat's release directory to run
2. Create a new MAVEN Web project in Eclipse

  As described above, using the command to create a new MAVEN Web project, you can see a standard Web project like this:

My-web-app is the project name:

  

    If it's a developer who's been transferred from MyEclipse, it might not be a good fit, but it doesn't matter, a form of code organization; it's the same as using the MyEclipse new Webroot release when you actually publish to Tomcat:

 To create a new MAVEN project in Eclipse, you first need a few basic configurations:

  1) set up our own Maven path for installation

Two places, installations and user settings,user settings inside configure us. m2 The following settings.xml files, as well as the local warehouse address, by default in the C-drive user directory:

  

 

  2) New Project

There are a lot of tutorials on the web, but there are a few things to watch out for:

(1) When the new choice:

    (2) Right-click Project--Properties, where you can modify several places;

Runtimes selected, then the Java version is changed to version 1.8, the Dynamic Web module can be removed, click Apply, and then tick the dynamic web Module this time to 3.0, there will be a further configuration of the blue connection, point in, modify the content directory for Src/main/webapp, and will generate ... Tick, click OK.

  (3) Right-click Project-->build path-->config Build Path

You can add the missing folder to the

3) configuration Items 

Click to open the Pom.xml file to add the basic servlet and JUnit dependencies

<Dependencies>    <Dependency>       <groupId>Junit</groupId>       <Artifactid>Junit</Artifactid>       <version>3.8.1</version>       <Scope>Test</Scope>    </Dependency>    <Dependency>       <groupId>Javax.servlet</groupId>       <Artifactid>Javax.servlet-api</Artifactid>       <version>3.1.0</version>    </Dependency></Dependencies>

   Click Open Web. xml file, because this file is automatically generated, no head; You can now search the latest template for use, or you can use the following:

<?XML version= "1.0" encoding= "UTF-8"?><Web-appXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://xmlns.jcp.org/xml/ns/javaee"xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"ID= "webapp_id"version= "3.1">  <Display-name>Demo</Display-name></Web-app>

  4) Configure the plugin below to publish the MAVEN project

First, add the following code to the Pom.xml file: The build tag is parallel to dependencies, and a < Click alt+/will have a label prompt.

  <Build>      <Plugins>          <plugin>            <groupId>Org.apache.tomcat.maven</groupId>            <Artifactid>Tomcat7-maven-plugin</Artifactid>            <version>2.1</version>            <Configuration>                <!--Note the URL here and modify it to a tomcat-monitored port -                <URL>Http://localhost:8080/manager/text</URL>                <Server>Tomcat7</Server> <!--the name here must match the ID configured in Setting.xml -                <Path>/${project.artifactid}</Path> <!--The name here is the project name -                <Server>Tomcat7</Server><username>admin</username>
<password>password</password> </configuration>&l t;/plugin></plugins></ Build>

Configure Tomcat

  edit the tomcat directory/conf/tomcat-users.xml under  Span style= "color: #0000ff;" ><  tomcat-users  ></ Span style= "color: #800000;" >tomcat-users  >  tag after you add the following code, save exit  

<role rolename= "Admin-gui"/>
<role rolename= "Admin-script"/>
<role rolename= "Manager-gui"/>
<role rolename= "Manager-script"/>
<role rolename= "Manager-jmx"/>
<role rolename= "Manager-status"/>
<user username= "admin" password= "password" roles= "Manager-gui,manager-script,manager-jmx,manager-status, Admin-script,admin-gui "/>

  If an error occurs:

Try to change the JRE in eclipse

Window--preferences--java--installed JREs, select the JRE in the JDK directory and click OK.

If an unauthorized error is reported, there may be a two-place configuration username password that does not correspond to: User

Last visit:

3. Summary

in contact with maven always conflict, always love the way of manual guide package, in fact, I have encountered the error is always to avoid, static not mind to go to deal with, the online tutorial, but the version is not the same, the focus is different, will lead to solve our problems, in short, to find a solution.

1) When configuring MAVEN, the mirror is configured correctly, otherwise it will report errors that can not be found by various plugins.

2) using Eclipse is primarily a configuration inside the Properties and config build path.

3) Configure the plugin to match the password.

Summarize the points to note:

1) to search, two aspects of searching, the first search you this problem how to solve, the second search log error information, search the content quality is also mixed, to learn to choose!

2) usually read more blog forum, help others solve problems, accumulate related technology circle, problem solving circle, wait until the real bug can quickly locate the problem!

3) to understand the different Ides, this problem can be replaced by another;

工欲善其事 its prerequisite, development environment is very necessary to be familiar with!

4. Reference documents

http://www.linuxidc.com/Linux/2017-05/143308.htm

MAVEN Official website: http://maven.apache.org/

Getting started with Maven 1-Create a new MAVEN Web project in Eclipse

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.