Maven learning (2): create a simple maven project

 

                                    Maven learning (2): Customize a maven web project

1. Create a simple web project

     Maven 3.0 and above have abandoned create, enable generate to create projects

 1. Execute the command to create a project: mvn archetype:generate -DgroupId=org.zcf -DartifactId=webtest -DpackageName=com.zcf.webtest -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeCatalog=internal 

       groupId

     d Group, company, group, organization, project, or other group. The convention for community identification is that it begins with the reverse domain name of the organization that created the project. Projects from Sonatype have a groupId starting with com.sonatype and projects from Apache Software have a groupId starting with org.apache 

       ArtifactID under groupId represents a unique identifier for an individual item.

(The speed of maven skeleton generation project is horribly slow. They are all waiting in Generating project in Batch mode. The Idea status display bar is still not running, and it is not stuck. Check the debug information and find that it is caused by maven obtaining archetype-catalog.xml. (Open http://repo1.maven.org/maven2/archetype-catalog.xml with a browser , it takes a long time to get it.)

Solution: Add the -DarchetypeCatalog=internal running parameter, and obtain the archetype-catalog.xml locally. )

      The following interface will appear

     

      The resulting directory structure is as follows:

     

 

         

     Note that the packaging element contains the value war . This packaging type configuration allows Maven to generate a web application as a WAR file. A project with a packaging type of war will create a WAR file in the target/ directory. The default name of this file is content-zh-0.6-SNAPSHOT.war . For this project, the default WAR file is target/webtest-1.0-SNAPSHOT.war . In this webtest project, we have customized this generated by adding the finalName element to the project's build configuration

The name of the WAR file. According to the finalName of webtest, the WAR file generated in the package phase is target/webtest.war .

2. Configure the jetty plugin and run

  After you've compiled, tested, and packaged your web application, you'll want to deploy it into a servlet container and test the index.jsp created by the Maven Archetype plugin. Typically, you need to download Jetty or Apache Tomcat, extract the distribution, copy your application WAR file to the webapps/ directory, and start your container. Now, to achieve the same purpose, you no longer have to do these things. Instead, you can use the Maven Jetty plugin to run your web application in Maven. To do this, you need to configure the Maven Jetty plugin in your project's pom.xml. Add the following plugin element to your project's build configuration:

 

<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
</plugin>
</plugins>

 After configuring the Maven Jetty plugin in the project's pom.xml, you can invoke the Jetty plugin's Run goal to launch your web application in the Jetty Servlet container. Run mvn jetty:run as follows:

Load the URL http://localhost:8080/webtest/ in the browser, you will see the helloword interface

3. Configure a simple servlet

   1. Create a subdirectory of src/main/java/org/zcf in the project directory, create a TestServlet.java file in the subdirectory, and insert the following code

   

package org.zcf;
import java.io. *;
import javax.servlet.*;
import javax.servlet.http.*;
public class TestServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
	PrintWriter out = response.getWriter();
	out.println( "SimpleServlet Executed" );
	out.flush();
	out.close();
 }
}
   2. Then modify the web.xml directory under src/main/webapp/ and configure the servlet

 

   

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>]
	  <servlet>
	<servlet-name>test</servlet-name>
	<servlet-class>org.zcfTestServlet</servlet-class>
	</servlet>
	<servlet-mapping>
	<servlet-name>test</servlet-name>
	<url-pattern>/test</url-pattern>
	</servlet-mapping>
</web-app>
   3. Execute compilation: mvn compile, the following error will be reported, because the dependency of servlet api is missing 

 

 

[ERROR] /C:/Users/Administrator/webtest/src/main/java/org/zcf/TestServlet.java:[
5,8] The class SimpleServlet is public and should be declared in a file named SimpleServlet.java
[ERROR] /C:/Users/Administrator/webtest/src/main/java/org/zcf/TestServlet.java:[
3,1] Package javax.servlet does not exist
[ERROR] /C:/Users/Administrator/webtest/src/main/java/org/zcf/TestServlet.java:[
4,1] Package javax.servlet.http does not exist
[ERROR] /C:/Users/Administrator/webtest/src/main/java/org/zcf/TestServlet.java:[
5,36] Symbol not found
  Symbol: class HttpServlet
[ERROR] /C:/Users/Administrator/webtest/src/main/java/org/zcf/TestServlet.java:[
6,19] Symbol not found
  Symbol: class HttpServletRequest
  Location: class org.sonatype.mavenbook.web.SimpleServlet
[ERROR] /C:/Users/Administrator/webtest/src/main/java/org/zcf/TestServlet.java:[
7,1] Symbol not found
  Symbol: class HttpServletResponse
  Location: class org.sonatype.mavenbook.web.SimpleServlet
[ERROR] /C:/Users/Administrator/webtest/src/main/java/org/zcf/TestServlet.java:[
8,8] Symbol not found
  Symbol: class ServletException
  Location: class org.sonatype.mavenbook.web.SimpleServlet
[INFO] 7 errors
    Just add the servlet dependency in the pom file. It is also necessary to point out that our dependency uses the provided scope. This scope tells Maven that the jar file has been "provided" by the container and therefore no longer needs to be included in the war.

 

 

	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>servlet-api</artifactId>
		<version>3.0-alpha-1</version>
                <scope>provided</scope>
	</dependency>
    http://localhost:8080/webtest/test open the address to see the results generated by the servlet

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326450102&siteId=291194637