使用Maven Multi Module创建Java多层应用程序
我想使用maven创建一个多层java项目,如下所示:
- PresentationLayer-GUIModule(jsp / jsf页面的最顶层)
- PresentationLayer-GatewayModule(Web服务的最顶层)
- BusinessLayer-ServiceModule(中间层)
- DataAccessLayer(最下层)
- CommonLayer(可从所有图层访问的垂直图层)
Maven模块结构:
- 根pom
- EAR pom
- GUIModule pom
- GatewaModule pom
- ServiceModule pom
- DataAccessLayer pom
- CommonLayer pom
我创建了一个名为flashcard
的项目,其中包含以下pom配置:
根pom:
4.0.0 com.flashcard flashcard 1.0 BusinessLayer-ServiceModule CommonLayer EAR PresentationLayer-GatewayModule PresentationLayer-GUIModule DataAccessLayer-ManagerModule pom flashcard http://maven.apache.org UTF-8 1.8 1.8 1.0.0 javax.ejb javax.ejb-api 3.2 javax javaee-web-api 7.0 com.flashcard PresentationLayer-GatewayModule 1.0 ejb compile com.flashcard PresentationLayer-GUIModule 1.0 war compile com.flashcard BusinessLayer-ServiceModule 1.0 ejb compile com.flashcard CommonLayer 1.0 jar compile com.flashcard DataAccessLayer-ManagerModule 1.0 ejb compile org.apache.maven.plugins maven-compiler-plugin 3.1 1.8 1.8 org.apache.maven.plugins maven-war-plugin 3.2.0 org.apache.maven.plugins maven-ejb-plugin 3.0.0 true 3.2
EAR pom:
flashcard com.flashcard 1.0 4.0.0 EAR ear EAR http://maven.apache.org UTF-8 com.flashcard PresentationLayer-GatewayModule 1.0 ejb compile com.flashcard PresentationLayer-GUIModule 1.0 war compile com.flashcard BusinessLayer-ServiceModule 1.0 ejb compile com.flashcard CommonLayer 1.0 jar compile com.flashcard DataAccessLayer-ManagerModule 1.0 ejb compile org.apache.maven.plugins maven-ear-plugin lib com.flashcard PresentationLayer-GUIModule /myflashcard com.flashcard BusinessLayer-ServiceModule com.flashcard DataAccessLayer-ManagerModule com.flashcard CommonLayer com.flashcard PresentationLayer-GatewayModule org.apache.maven.plugins maven-war-plugin org.apache.maven.plugins maven-ejb-plugin org.apache.maven.plugins maven-compiler-plugin
GUIModule pom:
flashcard com.flashcard 1.0 4.0.0 PresentationLayer-GUIModule war PresentationLayer-GUIModule Maven Webapp http://maven.apache.org junit junit 3.8.1 test com.flashcard BusinessLayer-ServiceModule 1.0 ejb provided com.flashcard CommonLayer 1.0 jar provided PresentationLayer-GUIModule org.apache.maven.plugins maven-war-plugin
GatewayModule pom:
flashcard com.flashcard 1.0 4.0.0 PresentationLayer-GatewayModule ejb PresentationLayer-GatewayModule http://maven.apache.org UTF-8 junit junit 3.8.1 test com.flashcard BusinessLayer-ServiceModule 1.0 ejb provided com.flashcard CommonLayer 1.0 jar provided javax javaee-api 7.0
ServiceModule pom:
flashcard com.flashcard 1.0 4.0.0 BusinessLayer-ServiceModule ejb BusinessLayer-ServiceModule http://maven.apache.org UTF-8 junit junit 3.8.1 test com.flashcard DataAccessLayer-ManagerModule 1.0 ejb provided com.flashcard CommonLayer 1.0 jar provided maven-ejb-plugin 3.0.0 true 3.2 org.apache.maven.plugins maven-compiler-plugin 3.1 1.8 1.8
DataAccessLayer pom:
flashcard com.flashcard 1.0 4.0.0 DataAccessLayer-ManagerModule ejb DataAccessLayer-ManagerModule http://maven.apache.org UTF-8 junit junit 3.8.1 test com.flashcard CommonLayer 1.0 jar provided maven-ejb-plugin 3.0.0 true 3.2 org.apache.maven.plugins maven-compiler-plugin 3.1 1.8 1.8
CommonLayer pom:
flashcard com.flashcard 1.0 4.0.0 CommonLayer jar CommonLayer http://maven.apache.org UTF-8 javax javaee-api 7.0 provided com.google.code.gson gson 2.2.4 org.bouncycastle bcprov-jdk15on 1.54 joda-time joda-time 2.7 org.apache.logging.log4j log4j-core 2.0.2
这些文件导致创建一个包含所有其他模块的ear文件,并在jboss application server eap-7.1
成功部署,但是此地址找不到Web服务: http://localhost:8080/myflashcard/getflashcard
RESTful Web服务:
package com.flashcard; import com.google.gson.Gson; import org.json.JSONObject; import javax.ejb.Stateless; import javax.interceptor.Interceptors; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Stateless @Path("/getflashcard") public class GetFlashcard { @Interceptors(Validator.class) @GET @Produces(MediaType.APPLICATION_JSON) public String getFlashcard() { String jsonString = new JSONObject().put("ip", "192.167.1.15").toString(); return jsonString; } }
application.xml中:
EAR com.flashcard-PresentationLayer-GatewayModule-1.0.jar com.flashcard-PresentationLayer-GUIModule-1.0.war /PresentationLayer-GUIModule com.flashcard-BusinessLayer-ServiceModule-1.0.jar com.flashcard-DataAccessLayer-ManagerModule-1.0.jar lib
GatewayModule是一个ejb模块,但对于Web服务,该模块必须是一个Web模块。 将其更改为Web模块并重试,但要注意应用程序上下文,因为在这种情况下,您将有2个上下文:一个用于gui,一个用于网关。