Eclipse uses MAVEN to build Java Web projects and deploy Tomcat directly

Source: Internet
Author: User
Tags java web

1. Environment:

Windows 10

Java 1.8

Maven 3.3.9

Eclipse IDE for Java EE developers

2. Prepare:

Don't go into the eclipse environment or anything, MAVEN environment is still needed

Download maven First, address: http://maven.apache.org/download.cgi

Direct point Apache-maven-3.3.9-bin.zip Download, and then extract to whatever directory

Next, configure the environment variables and create new ones in the system variables:

Variable name: M2_home variable Value: C:\Program files\maven   (your Maven directory)

Then, at the end of the path variable, insert:

%m2_home%\bin

Note: And the front should be there;

After completion, test on the command line: mvn-v

3. Integrate Eclipse, Maven:

Now open eclipse--window--preferences--maven--installations.

Dot add...-->>directory ... Select your Maven directory and finish

Then continue to select Maven--user Settings on the left, and set the two profile directories as MAVEN directories \conf\settings.xml

Then click Update Settings to renew the configuration and click OK to complete the integration of MAVEN and Eclipse.

4. Set up and configure the MAVEN project:

File--new--other ...

Choose Maven Project,next under Maven

Leave the default, Next

Choose Webapp,next Here

Enter the package name, project name, packages can not fill, Finish

Right-click the project--properties--project Facets after you've built it.

Here first remove the dynamic WEB services, change the Java version to 1.8, click Apply

Now check the dynamic Web services again, change the version to 3.1, and a line will appear below, click on him!

Modify inside Content directory for Src/main/webapp, and will generate ... Tick, click OK

You can see the runtimes option on the right, click, select one of your tomcat and click OK to end the setting

Next, modify the Web. xml file

Change all the code inside to the following, save exit

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns: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>

Then edit the Pom.xml file

<build>
<plugins>
<!--jetty Commissioning Jetty:run--
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.8.v20171121</version>
<configuration>
<!--<war>${project.basedir}/target/pa-appmanage.war</war>--
<!--specify a folder for Web pages--
<webAppSourceDirectory>${project.basedir}/src/main/webapp</webAppSourceDirectory>
<stopPort>8</stopPort>
<stopKey>stop</stopKey>
<stopWait>5</stopWait>
<port>88</port>
<webAppConfig>
<!--configuration Engineering virtual directory name--
<contextPath>/${project.artifactId}</contextPath>
</webAppConfig>
</configuration>
</plugin>
</plugins>

<finalName>mytest</finalName>
</build>

==================================================== or start ================================================ with Tomcat ====

Change the JUnit version to 4.12 First, and then add the following code to <dependencies></dependencies> to support the servlet

    <dependency>      <groupId>javax.servlet</groupId>      <artifactid>javax.servlet-api </artifactId>      <version>3.1.0</version>    </dependency>

Then add the following code to the <build></build> to use MAVEN to deploy Tomcat directly and configure the JDK version

  <plugins>      <plugin>        <groupId>org.apache.maven.plugins</groupId>        < artifactid>maven-compiler-plugin</artifactid>        <version>3.6.1</version>        < configuration>          <source>1.8</source>          <target>1.8</target>        </ configuration>      </plugin>      <plugin>        <groupid>org.apache.tomcat.maven</ groupid>        <artifactId>tomcat7-maven-plugin</artifactId>        <version>2.2</version >        <configuration>          <url>http://localhost:8080/manager/text</url>          < username>tomcat</username>          <password>tomcat</password>        </configuration>      </plugin>    </plugins>

Where <username>tomcat</username> and <password>tomcat</password> are the passwords configured in Tomcat and will continue to explain later

Save and exit, right-click on Project--maven--update poject ... Update configuration, pop-up box click OK

5. Configure Tomcat:

This configuration can only be configured once, not every project needs to be configured

Edit Tomcat directory under/conf/tomcat-users.xml

After adding the following code to the <tomcat-users></tomcat-users> tab, save the exit

<role rolename= "Manager-gui"/><role rolename= "Manager-script"/><user username= "Tomcat" password= " Tomcat "roles=" Manager-gui,manager-script "/>

The user name and password here correspond to the configuration in maven above

6. Deploy the Running project:

Run the Tomcat directory first/bin/startup.bat clean install Tomcat7:redeploy

Then right-click on the project run As--maven build and enter in goals: Clean install Tomcat7:redeploy

Run the project by clicking Run, and then simply click Maven Build to run it automatically.

You can see it in the HTTP://LOCALHOST:8080/project name

Hello world!

If the project has an error, you can change the JRE in eclipse

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

This article permanently updates the link address : http://www.linuxidc.com/Linux/2017-05/143308.htm

Quote Original: Http://www.linuxidc.com/Linux/2017-05/143308.htm

Blog is to remember that they are easy to forget things, but also a summary of their work, the article can be reproduced, without copyright. Hope to do their own efforts to do better, we work together to improve!

If there is any problem, welcome to discuss together, code if there is a problem, you are welcome to the great God!

Eclipse uses MAVEN to build Java Web projects and deploy Tomcat directly

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.