建议看完上一篇再来看这一篇哈。点这里查看
上一篇已经初步把工程搭起来了,这篇继续讲怎么把他们集成起来发布。以及更好的发布方式。
好了,开始吧。
在test-dao中新建一个HelloDao
package com.rice.test.dao;
public class HelloDao {
public String hello(String str) {
return str + " from dao";
}
}
在test-service中新建一个HelloService
package com.rice.test.service;
import com.rice.test.dao.HelloDao;
public class HelloService {
private HelloDao dao = new HelloDao();
public String sayHello(String str) {
return " service " + dao.hello(str);
}
}
在test-portal中新建一个HelloServlet
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.rice.test.service.HelloService;
/**
* Servlet implementation class HelloServlet
*/
@WebServlet(description = "test", urlPatterns = { "/helloServlet" })
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private HelloService service = new HelloService();
/**
* Default constructor.
*/
public HelloServlet() {}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String str = service.sayHello("rice");
response.getWriter().print("hello " + str);
}
}
好了,portal调service,再调dao,这就是我们一般业务系统的正常流程。看看怎么把它跑起来吧。
开始构建。test-parent ->右键 ->Run As ->Maven install
[INFO] Reactor Summary:
[INFO]
[INFO] test .............................................. SUCCESS [ 0.722 s]
[INFO] test-dao .......................................... SUCCESS [ 3.123 s]
[INFO] test-util ......................................... SUCCESS [ 0.468 s]
[INFO] test-service ...................................... SUCCESS [ 18.642 s]
[INFO] test-portal Maven Webapp .......................... SUCCESS [ 1.044 s]
[INFO] test-console Maven Webapp ......................... SUCCESS [ 0.153 s]
[INFO] test-api Maven Webapp ............................. SUCCESS [ 0.141 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 24.476 s
[INFO] Finished at: 2017-04-03T00:54:07+08:00
[INFO] Final Memory: 14M/168M
[INFO] ------------------------------------------------------------------------
构建成功!这就是建立父模块的好处,统一构建,不需要单独构建每一个模块。
这时可以看到target下生成了对应的jar包或war包。
maven-archetype-quickstart默认是jar包的格式,maven-archetype-webapp默认是war包。当然这也是可以改的,pom里的<packaging>war</packaging>
就是指定构建的格式。
除了target目录外,maven仓库也会生成相应的jar包或war包。我们把test-portal-0.0.1-SNAPSHOT.war
拷贝到tomcat的webapps目录下,并重命名为test-portal.war,然后启动tomcat。访问http://localhost:8080/test-portal/helloServlet,可以看到打印的hello service rice from dao。我们成功了!!
这时我们再看看tomcat的webapps目录,会发现tomcat自动解压了test-portal.war,点进去WEB-INF\lib下,发现maven已经自动的把依赖的其他jar包都打包进来了。
至此,我们已经成功了一小步!!但是!有一个问题是,我们总不能每次构建都去复制,然后重命名到webapps下吧!当然不能,那就看看maven是怎么自动部署的吧。
先写到这里了,睡觉了。明天继续哈。