使用Maven Multi Module创建Java多层应用程序

时间:2018-03-29 17:47:28

标签: java maven jboss7.x pom.xml ejb-3.2

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

  1. PresentationLayer-GUIModule(jsp / jsf页面的最顶层)
  2. PresentationLayer-GatewayModule(Web服务的最顶层)
  3. BusinessLayer-ServiceModule(中间层)
  4. DataAccessLayer(最下层)
  5. CommonLayer(可从所有图层访问的垂直图层)
  6. Maven模块结构:

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

    我创建了一个名为flashcard的项目,其中包含以下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:

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

    这些文件导致创建一个包含所有其他模块的ear文件,并在jboss application server eap-7.1中成功部署,但是此地址找不到Web服务: 的 http://localhost:8080/myflashcard/getflashcard

    RESTful网络服务:

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

答案 0 :(得分:0)

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