Create Three Tier Application using Maven

前提是你 提交于 2019-12-03 23:09:41

问题


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

  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 Module Structure:

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

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

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:

<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:

<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:

<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:

<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:

<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:

<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>

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 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:

<?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>

回答1:


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.



来源:https://stackoverflow.com/questions/49562268/create-three-tier-application-using-maven

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!