Maven learning (3): create a maven multi-module project

1. Use eclipse to create a maven multi-module project 

     1. First create a simple-parent project, here we start to create projects with eclipse

 

 

 2. Start to create simple-webapp and simple-util modules

   

 

 2. Start to modify the configuration file and write some simple running classes

       1.1 Modify the pom file of simple-util first

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<artifactId>simple-util</artifactId>
	<packaging>jar</packaging>
	<name>simple-util</name>
	<parent>
		<groupId>org.zcf</groupId>
		<artifactId>simple-parent</artifactId>
		<version>1.0</version>
	</parent>
	<build>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-surefire-plugin</artifactId>
					<configuration>
						<testFailureIgnore>true</testFailureIgnore>
					</configuration>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>
</project>

        1.2 Add a tool class to the simple-util project

package com.zcf.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class CommonUtils {
   
	/**
	 * Check if list is empty
	 * @param list
	 * @return
	 */
	public static boolean ListNotNull(List list){		
		if(list == null || list.size() < 1){
			return false;
		}
		return true;
	}
	
	/**
	 * Check if the array is empty
	 * @param object
	 * @return
	 */
    public static boolean ArrayNotNull(Object object[]){
    	if( null == object || object.length < 1){
    		return true;
    	}
    	return false;
    }
	
	
	/**
	 * Get a random number with a random specified maximum value
	 * @param max
	 * @return
	 */
	public static int getRandomInt(int max){
		Random random =  new Random();
		return random.nextInt(max);	
	}
	
	
  
	/**
	 * Get 17-digit order number
	 * @return
	 */
	public static String getOrderNo(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
		// three random numbers
		int random = CommonUtils.getRandomInt(900)+100;
		return sdf.format(new Date()) + random;
		
	}
	/**
	 * Get a 15-digit recharge order number
	 * @return
	 */
	public static String getRechargeNo(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyMMddhhmmss");
		// three random numbers
		int random = CommonUtils.getRandomInt(900)+100;
		return sdf.format(new Date()) + random;
		
	}

}

    2.1 Modify the pom file of simple-webapp

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
  <artifactId>simple-webapp</artifactId>
  <packaging>war</packaging>
  <parent>
		<groupId>org.zcf</groupId>
		<artifactId>simple-parent</artifactId>
		<version>1.0</version>
	</parent>
  <name>webapp</name>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>servlet-api</artifactId>
		<version>3.0-alpha-1</version>
		<scope>provided</scope>
	</dependency>
	<dependency>
		<groupId>org.zcf</groupId>
		<artifactId>simple-util</artifactId>
		<version>1.0</version>
	</dependency>
  </dependencies>
<build>
		<pluginManagement>
 <plugins>
		<plugin>
		<groupId>org.mortbay.jetty</groupId>
		<artifactId>maven-jetty-plugin</artifactId>
		</plugin>
		</plugins>
		</pluginManagement>
	</build>
</project>

  2,2 Add a servlet (the tool class of the util project is used here) and configure it in web.xml

package com.zcf.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zcf.util.CommonUtils;

public class TestServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		out.println("SimpleServlet Executed"+CommonUtils.getOrderNo());
		out.flush();
		out.close();
	}
}

 

<!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>com.zcf.web.TestServlet</servlet-class>
	</servlet>
	<servlet-mapping>
	<servlet-name>test</servlet-name>
	<url-pattern>/test</url-pattern>
	</servlet-mapping>
</web-app>
 
 

   3.1 Modify the pom file of simple-parent

   

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>org.zcf</groupId>
	<artifactId>simple-parent</artifactId>
	<packaging>pom</packaging>
	<version>1.0</version>
	<name>simple-parent</name>
	<modules>
		<module>simple-util</module>
		<module>simple-webapp</module>
	</modules>
	<build>
		<pluginManagement>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-compiler-plugin</artifactId>
					<configuration>
					<source>1.7</source>
					<target>1.7</target>
					</configuration>
			</plugin>
		</plugins>
		</pluginManagement>
	</build>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>

 3. Run the web application

     1. Execute maven clean install on simple-parent

     2. Switch to the simple-webapp directory and execute maven jetty:run

     http://localhost:8080/simple-webapp/test

    Output: SimpleServlet Executed20160919034016452

Guess you like

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