当前位置: 首页 > 知识库问答 >
问题:

使用Maven创建三层应用程序

司马耘豪
2023-03-14

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

  1. presentationlayer-guiModule(JSP/JSF页面的最顶层)
  2. PresentationLayer-GatewayModule(web服务的最顶层)
  3. BusinessLayer-ServiceModule(中间层)
  4. DataAccessLayer(最下层)
  5. 通用层(可从所有层访问的垂直层)
  • 根pom
    • 耳POM
    • Guimodule POM
    • 网关模块POM
    • 服务模块POM
    • 数据访问层POM
    • 通用层POM

    我创建了一个名为FlashCard项目,具有以下pom配置:

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

    通用层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文件,该文件包含所有其他模块,并成功地html" target="_blank">部署在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:

    <?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个答案

颜阳炎
2023-03-14

GatewayModule是一个ejb模块,但是对于web服务,这个模块必须是一个web模块。将其更改为web模块,然后再试一次,但要注意应用程序上下文,因为在这种情况下,您将有两个上下文:一个用于gui和一个用于网关。

 类似资料:
  • 问题内容: 如何使用Maven创建桌面(独立/ Swing)应用程序? 我正在使用Eclipse 3.6。 问题答案: 创建一个Maven项目,如下所示: 将以下条目添加到您的pom文件中: 将项目作为Maven项目导入到Eclipse,然后作为Java应用程序运行。

  • 我正试图使用Maven在Eclipse中创建一个Vaadin应用程序,但无法这样做。官方说明(https://vaadin.com/wiki/-/wiki/main/creating%20an%20Eclipse%20project)说要在Eclipse中创建一个新的Maven项目,在原型列表中创建“”vaadin-archetype-application“,我看不到这种类型的东西,然后,我添加

  • 我们现有的父项目使用Spring4.3.3.Release,我们试图将它的一部分作为Spring Boot应用程序提供,其他部分作为spring提供。 要做到这一点,我们似乎必须创建一个有两个父项目的Maven项目:我们现有的父项目和Spring Boot父项目。我们怎么能这么做?

  • 我将使用ADFS作为身份提供者(IDP)实现对java应用程序的单点登录。通过OneLogin找到了此解决方案SSO,并使用了其示例应用程序。除此之外,还有另一个解决方案Shibboleth。 我想知道与我的上下文匹配的最佳解决方案是什么。之间,这不是Spring应用程序。 谢谢

  • 我想创建一个应用程序使用:客户端向服务器发送,服务器将使用的大写字母,并发送回客户端进行显示。 当运行时。它显示以下错误: 我有搜索和做一些方法,但是。。你能帮助我吗。

  • Ember CLI,Ember的命令行界面提供了一个标准的项目结构,一组开发工具和一个插件系统。这允许Ember开发人员专注于构建应用程序,而不是构建使它们运行的​​支持结构。可通过ember --help显示Ember CLI提供的命令,或通过ember help <command-name>查看特定命令的信息。 创建应用程序 $ ember new super-rentals 上述ember