使用Maven Multi Module创建Java多层应用程序 [英] Create Java Multi Layer Application Using Maven Multi Module

查看:179
本文介绍了使用Maven Multi Module创建Java多层应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用maven创建一个多层java项目,如下所示:

I want to create a multi-layer java project using maven as follows:


  1. PresentationLayer-GUIModule(jsp /的最顶层) jsf pages)

  2. PresentationLayer-GatewayModule(Web服务的最顶层)

  3. BusinessLayer-ServiceModule(中间层)

  4. DataAccessLayer(最下层)

  5. CommonLayer(可从所有图层访问的垂直图层)

  1. PresentationLayer-GUIModule (top most layer for jsp/jsf pages)
  2. PresentationLayer-GatewayModule (topmost layer for web services)
  3. BusinessLayer-ServiceModule (middle layer)
  4. DataAccessLayer (lowermost layer)
  5. CommonLayer (vertical layer which is accessible from all layers)

Maven模块结构:

Maven Module Structure:


  • root pom


    • EAR pom

    • GUIModule pom

    • GatewaModule pom

    • ServiceModule pom

    • DataAccessLayer pom

    • CommonLayer pom

    • root pom
      • EAR pom
      • GUIModule pom
      • GatewaModule pom
      • ServiceModule pom
      • DataAccessLayer pom
      • CommonLayer pom

      我创建了一个名为 flashcard 的项目,其中包含以下pom配置:

      I have create a project called flashcard with the following pom configurations:

      root pom :

      root pom:

      <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      
      <groupId>com.flashcard</groupId>
      <artifactId>flashcard</artifactId>
      <version>1.0</version>
      <modules>
          <module>BusinessLayer-ServiceModule</module>
          <module>CommonLayer</module>
          <module>EAR</module>
          <module>PresentationLayer-GatewayModule</module>
          <module>PresentationLayer-GUIModule</module>
          <module>DataAccessLayer-ManagerModule</module>
      </modules>
      <packaging>pom</packaging>
      
      <name>flashcard</name>
      <url>http://maven.apache.org</url>
      
      <properties>
          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
          <maven.compiler.source>1.8</maven.compiler.source>
          <maven.compiler.target>1.8</maven.compiler.target>
          <project.version.num>1.0.0</project.version.num>
      </properties>
      
      <dependencyManagement>
          <dependencies>
              <dependency>
                  <groupId>javax.ejb</groupId>
                  <artifactId>javax.ejb-api</artifactId>
                  <version>3.2</version>
              </dependency>
              <dependency>
                  <groupId>javax</groupId>
                  <artifactId>javaee-web-api</artifactId>
                  <version>7.0</version>
              </dependency>
              <dependency>
                  <groupId>com.flashcard</groupId>
                  <artifactId>PresentationLayer-GatewayModule</artifactId>
                  <version>1.0</version>
                  <type>ejb</type>
                  <scope>compile</scope>
              </dependency>
              <dependency>
                  <groupId>com.flashcard</groupId>
                  <artifactId>PresentationLayer-GUIModule</artifactId>
                  <version>1.0</version>
                  <type>war</type>
                  <scope>compile</scope>
              </dependency>
              <dependency>
                  <groupId>com.flashcard</groupId>
                  <artifactId>BusinessLayer-ServiceModule</artifactId>
                  <version>1.0</version>
                  <type>ejb</type>
                  <scope>compile</scope>
              </dependency>
              <dependency>
                  <groupId>com.flashcard</groupId>
                  <artifactId>CommonLayer</artifactId>
                  <version>1.0</version>
                  <type>jar</type>
                  <scope>compile</scope>
              </dependency>
              <dependency>
                  <groupId>com.flashcard</groupId>
                  <artifactId>DataAccessLayer-ManagerModule</artifactId>
                  <version>1.0</version>
                  <type>ejb</type>
                  <scope>compile</scope>
              </dependency>
          </dependencies>
      </dependencyManagement>
      
      <build>
          <pluginManagement>
              <plugins>
                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-compiler-plugin</artifactId>
                      <version>3.1</version>
                      <configuration>
                          <source>1.8</source>
                          <target>1.8</target>
                      </configuration>
                  </plugin>
                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-war-plugin</artifactId>
                      <version>3.2.0</version>
                  </plugin>
                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-ejb-plugin</artifactId>
                      <version>3.0.0</version>
                      <configuration>
                          <archive>
                              <manifest>
                                  <addClasspath>true</addClasspath>
                              </manifest>
                          </archive>
                          <ejbVersion>3.2</ejbVersion>
                      </configuration>
                  </plugin>
              </plugins>
          </pluginManagement>
      </build>
      
      </project>
      

      EAR pom:

      EAR pom:

      <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <parent>
          <artifactId>flashcard</artifactId>
          <groupId>com.flashcard</groupId>
          <version>1.0</version>
      </parent>
      <modelVersion>4.0.0</modelVersion>
      
      <artifactId>EAR</artifactId>
      <packaging>ear</packaging>
      
      <name>EAR</name>
      <url>http://maven.apache.org</url>
      
      <properties>
          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
      
      <dependencies>
          <dependency>
              <groupId>com.flashcard</groupId>
              <artifactId>PresentationLayer-GatewayModule</artifactId>
              <version>1.0</version>
              <type>ejb</type>
              <scope>compile</scope>
          </dependency>
          <dependency>
              <groupId>com.flashcard</groupId>
              <artifactId>PresentationLayer-GUIModule</artifactId>
              <version>1.0</version>
              <type>war</type>
              <scope>compile</scope>
          </dependency>
          <dependency>
              <groupId>com.flashcard</groupId>
              <artifactId>BusinessLayer-ServiceModule</artifactId>
              <version>1.0</version>
              <type>ejb</type>
              <scope>compile</scope>
          </dependency>
          <dependency>
              <groupId>com.flashcard</groupId>
              <artifactId>CommonLayer</artifactId>
              <version>1.0</version>
              <type>jar</type>
              <scope>compile</scope>
          </dependency>
          <dependency>
              <groupId>com.flashcard</groupId>
              <artifactId>DataAccessLayer-ManagerModule</artifactId>
              <version>1.0</version>
              <type>ejb</type>
              <scope>compile</scope>
          </dependency>
      </dependencies>
      
      <build>
          <plugins>
              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-ear-plugin</artifactId>               
                      <configuration>
                      <defaultLibBundleDir>lib</defaultLibBundleDir>
                      <modules>
                          <webModule>
                              <groupId>com.flashcard</groupId>
                              <artifactId>PresentationLayer-GUIModule</artifactId>
                              <contextRoot>/myflashcard</contextRoot>
                          </webModule>
                          <ejbModule>
                              <groupId>com.flashcard</groupId>
                              <artifactId>BusinessLayer-ServiceModule</artifactId>
                          </ejbModule>
                          <ejbModule>
                              <groupId>com.flashcard</groupId>
                              <artifactId>DataAccessLayer-ManagerModule</artifactId>
                          </ejbModule>
                          <jarModule>
                              <groupId>com.flashcard</groupId>
                              <artifactId>CommonLayer</artifactId>
                          </jarModule>
                          <ejbModule>
                              <groupId>com.flashcard</groupId>
                              <artifactId>PresentationLayer-GatewayModule</artifactId>
                          </ejbModule>
                      </modules>
                  </configuration>
              </plugin>
              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-war-plugin</artifactId>
              </plugin>
              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-ejb-plugin</artifactId>
              </plugin>
              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-compiler-plugin</artifactId>
              </plugin>
          </plugins>
      </build>
      </project>
      

      GUIModule pom:

      GUIModule pom:

      <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <parent>
          <artifactId>flashcard</artifactId>
          <groupId>com.flashcard</groupId>
          <version>1.0</version>
      </parent>
      <modelVersion>4.0.0</modelVersion>
      <artifactId>PresentationLayer-GUIModule</artifactId>
      <packaging>war</packaging>
      <name>PresentationLayer-GUIModule Maven Webapp</name>
      <url>http://maven.apache.org</url>
      <dependencies>
          <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>3.8.1</version>
              <scope>test</scope>
          </dependency>
          <dependency>
              <groupId>com.flashcard</groupId>
              <artifactId>BusinessLayer-ServiceModule</artifactId>
              <version>1.0</version>
              <type>ejb</type>
              <scope>provided</scope>
          </dependency>
          <dependency>
              <groupId>com.flashcard</groupId>
              <artifactId>CommonLayer</artifactId>
              <version>1.0</version>
              <type>jar</type>
              <scope>provided</scope>
          </dependency>
      </dependencies>
      <build>
          <finalName>PresentationLayer-GUIModule</finalName>
          <plugins>
              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-war-plugin</artifactId>
              </plugin>
          </plugins>
      </build>
      </project>
      

      GatewayModule pom:

      GatewayModule pom:

      <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/xsd/maven-4.0.0.xsd">
      <parent>
          <artifactId>flashcard</artifactId>
          <groupId>com.flashcard</groupId>
          <version>1.0</version>
      </parent>
      <modelVersion>4.0.0</modelVersion>
      
      <artifactId>PresentationLayer-GatewayModule</artifactId>
      <packaging>ejb</packaging>
      
      <name>PresentationLayer-GatewayModule</name>
      <url>http://maven.apache.org</url>
      
      <properties>
          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
      
      <dependencies>
          <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>3.8.1</version>
              <scope>test</scope>
          </dependency>
          <dependency>
              <groupId>com.flashcard</groupId>
              <artifactId>BusinessLayer-ServiceModule</artifactId>
              <version>1.0</version>
              <type>ejb</type>
              <scope>provided</scope>
          </dependency>
          <dependency>
              <groupId>com.flashcard</groupId>
              <artifactId>CommonLayer</artifactId>
              <version>1.0</version>
              <type>jar</type>
              <scope>provided</scope>
          </dependency>
          <dependency>
              <groupId>javax</groupId>
              <artifactId>javaee-api</artifactId>
              <version>7.0</version>
          </dependency>
      </dependencies>
      </project>
      

      ServiceModule pom:

      ServiceModule pom:

      <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/xsd/maven-4.0.0.xsd">
      <parent>
          <artifactId>flashcard</artifactId>
          <groupId>com.flashcard</groupId>
          <version>1.0</version>
      </parent>
      <modelVersion>4.0.0</modelVersion>
      
      <artifactId>BusinessLayer-ServiceModule</artifactId>
      <packaging>ejb</packaging>
      
      <name>BusinessLayer-ServiceModule</name>
      <url>http://maven.apache.org</url>
      
      <properties>
          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
      
      <dependencies>
          <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>3.8.1</version>
              <scope>test</scope>
          </dependency>
          <dependency>
              <groupId>com.flashcard</groupId>
              <artifactId>DataAccessLayer-ManagerModule</artifactId>
              <version>1.0</version>
              <type>ejb</type>
              <scope>provided</scope>
          </dependency>
          <dependency>
              <groupId>com.flashcard</groupId>
              <artifactId>CommonLayer</artifactId>
              <version>1.0</version>
              <type>jar</type>
              <scope>provided</scope>
          </dependency>
      </dependencies>
      
      <build>
          <plugins>
              <plugin>
                  <artifactId>maven-ejb-plugin</artifactId>
                  <version>3.0.0</version>
                  <configuration>
                      <archive>
                          <manifest>
                              <addClasspath>true</addClasspath>
                          </manifest>
                      </archive>
                      <ejbVersion>3.2</ejbVersion>
                  </configuration>
              </plugin>
              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-compiler-plugin</artifactId>
                  <version>3.1</version>
                  <configuration>
                      <source>1.8</source>
                      <target>1.8</target>
                  </configuration>
              </plugin>
          </plugins>
      </build>
      </project>
      

      DataAccessLayer pom:

      DataAccessLayer pom:

      <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <parent>
          <artifactId>flashcard</artifactId>
          <groupId>com.flashcard</groupId>
          <version>1.0</version>
      </parent>
      <modelVersion>4.0.0</modelVersion>
      
      <artifactId>DataAccessLayer-ManagerModule</artifactId>
      <packaging>ejb</packaging>
      
      <name>DataAccessLayer-ManagerModule</name>
      <url>http://maven.apache.org</url>
      
      <properties>
          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
      
      <dependencies>
          <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>3.8.1</version>
              <scope>test</scope>
          </dependency>
          <dependency>
              <groupId>com.flashcard</groupId>
              <artifactId>CommonLayer</artifactId>
              <version>1.0</version>
              <type>jar</type>
              <scope>provided</scope>
          </dependency>
      </dependencies>
      
      <build>
          <plugins>
              <plugin>
                  <artifactId>maven-ejb-plugin</artifactId>
                  <version>3.0.0</version>
                  <configuration>
                      <archive>
                          <manifest>
                              <addClasspath>true</addClasspath>
                          </manifest>
                      </archive>
                      <ejbVersion>3.2</ejbVersion>
                  </configuration>
              </plugin>
              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-compiler-plugin</artifactId>
                  <version>3.1</version>
                  <configuration>
                      <source>1.8</source>
                      <target>1.8</target>
                  </configuration>
              </plugin>
          </plugins>
      </build>
      </project>
      

      CommonLayer pom:

      CommonLayer pom:

      <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/xsd/maven-4.0.0.xsd">
      <parent>
          <artifactId>flashcard</artifactId>
          <groupId>com.flashcard</groupId>
          <version>1.0</version>
      </parent>
      <modelVersion>4.0.0</modelVersion>
      
      <artifactId>CommonLayer</artifactId>
      <packaging>jar</packaging>
      
      <name>CommonLayer</name>
      <url>http://maven.apache.org</url>
      
      <properties>
          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
      
      <dependencies>
          <dependency>
              <groupId>javax</groupId>
              <artifactId>javaee-api</artifactId>
              <version>7.0</version>
              <scope>provided</scope>
          </dependency>
          <dependency>
              <groupId>com.google.code.gson</groupId>
              <artifactId>gson</artifactId>
              <version>2.2.4</version>
          </dependency>
          <dependency>
              <groupId>org.bouncycastle</groupId>
              <artifactId>bcprov-jdk15on</artifactId>
              <version>1.54</version>
          </dependency>
          <dependency>
              <groupId>joda-time</groupId>
              <artifactId>joda-time</artifactId>
              <version>2.7</version>
          </dependency>
          <dependency>
              <groupId>org.apache.logging.log4j</groupId>
              <artifactId>log4j-core</artifactId>
              <version>2.0.2</version>
          </dependency>
      </dependencies>
      </project>
      

      这些文件导致创建一个包含所有其他模块的ear文件,并在<$ c中成功部署$ c> jboss应用服务器eap-7.1 ,但此地址找不到网络服务:
      http:// localhost:8080 / myflashcard / getflashcard

      These files lead to create an ear file which consists all other modules and it deployed successfully in jboss application server eap-7.1, however web service not found by this address: http://localhost:8080/myflashcard/getflashcard

      RESTful网络服务:

      RESTful web service:

      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:

      application.xml:

      <?xml version="1.0" encoding="UTF-8"?>
      <application xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/application_7.xsd" version="7">
        <display-name>EAR</display-name>
        <module>
          <ejb>com.flashcard-PresentationLayer-GatewayModule-1.0.jar</ejb>
        </module>
        <module>
          <web>
            <web-uri>com.flashcard-PresentationLayer-GUIModule-1.0.war</web-uri>
            <context-root>/PresentationLayer-GUIModule</context-root>
          </web>
        </module>
        <module>
          <ejb>com.flashcard-BusinessLayer-ServiceModule-1.0.jar</ejb>
        </module>
        <module>
          <ejb>com.flashcard-DataAccessLayer-ManagerModule-1.0.jar</ejb>
        </module>
        <library-directory>lib</library-directory>
      </application>
      


      推荐答案

      GatewayModule是一个ejb模块,但是对于Web服务该模块必须是一个Web模块。将其更改为Web模块并重试,但要注意应用程序上下文,因为在这种情况下,您将有2个上下文:一个用于gui,一个用于网关。

      The GatewayModule is an ejb module, but for web services this module has to be a web module. Change it to a web module and try again, but pay attention to application context because in this case you will have 2 contexts: one for gui and one for gateway.

      这篇关于使用Maven Multi Module创建Java多层应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆