【问题标题】:Maven JavaEE application with separated frontend and backend具有分离的前端和后端的 Maven JavaEE 应用程序
【发布时间】:2018-08-10 21:47:01
【问题描述】:

我想创建一个具有完全分离的FrontEndBackEnd 的Java EE 应用程序。我发现一些教程将这两个模块打包到一个 EAR 文件中。我可以创建它,并将我的应用程序部署到应用程序服务器。

出于安全原因,现在我必须分别部署前端 (Tomcat) 和后端 (Weblogic)。

我有什么:

正面:

  1. JSF 页面
  2. ManagedBeans

返回:

  1. 具有业务逻辑 (EJB) 的会话bean
  2. 实体类。

这两层之间的通信接口将是RMI 调用。

当我有一个带有这两个模块的 EAR 项目时,第一个解决方案正在运行,因为我的后端模块 pom.xml 打包是jar

<packaging>jar</packaging>

因此,我可以将这个构建的 jar 包含到我前端的类路径中,并且我的前端看到了所需的类。

现在我尝试从我的后端创建 ear,因为使用 jar 打包我无法将所需的库包含到我的后端,并且它们在运行时丢失了。所以我在我的pom.xml 中将后端打包更改为ear 好的,但是如果我从后端构建了EAR,那么我无法将它包含到我的前端类路径中,并且我的前端无法从后端看到所需的类。

对不起,我对此完全感到困惑。你能给我任何关于分离这两个模块的正确方法的建议或教程吗?例如,如何使用maven 同时构建EARJAR

非常感谢!

【问题讨论】:

    标签: maven jakarta-ee


    【解决方案1】:

    在您的项目中,您应该有一个带有 EJB 的模块和相应的打包类型“ejb”。通常还会生成“ejb-client”人工制品。这可以用作前端的依赖项。 前端应该打包成“war”,后端打包成“ear”。

    看看各自的 maven 插件(EAR、WAR、EJB):

    【讨论】:

    • 谢谢,但是如果我在 EJB 插件中拥有所有实体类和 ejb 类,我的 EAR 项目中应该包含什么?我可以将依赖项构建到我的 EJB 项目中吗?输出会是什么?我必须将 EAR 部署到 weblogic 并将 WAR 部署到 tomcat。请帮我。谢谢!
    • 请原谅我迟到的回答。
    • 在最简单的情况下,您的 EAR 只包含一个 EJB。是的,您可以使用依赖项。它们通常封装在 EAR 中。 EAR 封装了您的代码及其所有依赖项。通常 EAR 是相互分离的,因此您可以在不同的 EAR 中使用不同版本的库。
    【解决方案2】:

    试试这个它可能会帮助你的问题.. http://maven.apache.org/plugins/maven-ear-plugin/examples/skinny-wars.html

    在典型的 J2EE 环境中,WAR 封装在 EAR 中以进行部署。 WAR 可以在 WEB-INF/lib 中包含其所有依赖的 JAR,但是如果有多个 WAR,由于存在重复的 JAR,EAR 会很快变得非常大。相反,J2EE 规范允许 WAR 通过 MANIFEST.MF 中的 Class-Path 设置引用打包在 EAR 中的外部 JAR。

    【讨论】:

      【解决方案3】:

      如果您的前端在 JSF 中,请创建一个单独的 maven 项目,该项目将包含所有 UI 相关代码(css/js/*.jsf 和 Backing Bean),例如 DemoProject-UI 将是您的前端项目名称。编写您的所有 UI这个项目中的相关内容。

      现在您的 JSF 页面支持 bean 需要与服务层通信。为此,创建一个单独的 maven 项目,例如 DemoProject-Service。您可以在此项目中编写一个 Web 服务,DemoProject-UI 将通过代理访问该服务。 DemoProject-Service 将有一个将被调用的 Web 服务,它将具有使用 Spring 服务/DAO 等的后端逻辑。

      根据您的问题,它询问您是否希望在单独的服务器上运行,例如 DemoProject-UI 将运行(Tomcat)并且 DemoProject-Model 将运行 上(Weblogic)。现在您有 2 个单独的项目在 2 个不同的服务器上运行。

      如果您想将两者都保留在同一台服务器上,则将第二个项目的 maven 依赖项添加到第一个项目中,无需使用 Web 服务。

      【讨论】:

        【解决方案4】:
        1. 最好的方法是你可以创建模块并创建一个final 项目作为父 pom,您可以从主项目进行部署。 所以最后一个主项目将在构建子之后构建 模块。

          <modelVersion>4.0.0</modelVersion>
          <parent>
              <groupId>com</groupId>
              <artifactId>practice</artifactId>
              <version>0.0.1</version>
          </parent>
          <groupId>com.xplanr</groupId>
          <artifactId>SheelBatchClient</artifactId>
          <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>org.glassfish.jersey.core</groupId>
                  <artifactId>jersey-client</artifactId>
                  <version>2.22.2</version>
              </dependency>
              <dependency>
                  <groupId>com.fasterxml.jackson.core</groupId>
                  <artifactId>jackson-core</artifactId>
                  <version>2.6.5</version>
              </dependency>
              <dependency>
                  <groupId>com.fasterxml.jackson.core</groupId>
                  <artifactId>jackson-databind</artifactId>
                  <version>2.6.5</version>
              </dependency>
              <dependency>
                  <groupId>org.codehaus.jettison</groupId>
                  <artifactId>jettison</artifactId>
                  <version>1.3.8</version>
              </dependency>
          </dependencies>
          <description>Xplanr Batch Client</description>
          <name>xplanrBatchClient</name>
          
          <scm>
              <connection>scm:git:git//github.com/XplanrAnalyticsInc/XplanrMain.git</connection>
          </scm>
          
          <build>
              <plugins>
                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-compiler-plugin</artifactId>
                      <version>2.2</version>
                      <configuration>
                          <source>1.8</source>
                          <target>1.8</target>
                      </configuration>
                  </plugin>
          
                  <plugin>
                      <artifactId>maven-dependency-plugin</artifactId>
                      <executions>
                          <execution>
                              <phase>prepare-package</phase>
                              <goals>
                                  <goal>copy-dependencies</goal>
                              </goals>
                              <configuration>
                                  <outputDirectory>${project.build.directory}/utility/XplanrBatchClient/lib</outputDirectory>
                              </configuration>
                          </execution>
                      </executions>
                  </plugin>
          
                  <plugin>
                      <artifactId>maven-resources-plugin</artifactId>
                      <version>3.0.2</version>
                      <executions>
                          <execution>
                              <id>copy-resources</id>
                              <!-- here the phase you need -->
                              <phase>validate</phase>
                              <goals>
                                  <goal>copy-resources</goal>
                              </goals>
                              <configuration>
                                  <outputDirectory>${basedir}/target/utility/XplanrBatchClient</outputDirectory>
                                  <resources>
                                      <resource>
                                          <directory>src/main/utility/XplanrBatchClient</directory>
                                          <filtering>true</filtering>
                                      </resource>
                                  </resources>
                                  <configuration>
                                      <archive>
                                          <manifest>
                                              <addClasspath>true</addClasspath>
                                          </manifest>
                                      </archive>
                                  </configuration>
                              </configuration>
                          </execution>
                      </executions>
                  </plugin>
          
                  <plugin>
                      <groupId>org.codehaus.mojo</groupId>
                      <artifactId>buildnumber-maven-plugin</artifactId>
                      <version>1.4</version>
                      <executions>
                          <execution>
                              <phase>validate</phase>
                              <goals>
                                  <goal>create</goal>
                              </goals>
                          </execution>
                      </executions>
                      <configuration>
                          <format>{0,number}</format>
                          <items>
                              <item>buildNumber</item>
                          </items>
                          <doCheck>false</doCheck>
                          <doUpdate>false</doUpdate>
                          <revisionOnScmFailure>unknownbuild</revisionOnScmFailure>
                          <!-- <shortRevisionLength>5</shortRevisionLength> -->
                      </configuration>
                  </plugin>
          
                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-jar-plugin</artifactId>
                      <version>2.3.1</version>
                      <executions>
                          <execution>
                              <phase>package</phase>
                              <goals>
                                  <goal>jar</goal>
                              </goals>
                          </execution>
                      </executions>
                      <configuration>
                          <jarName>xplanrBatchClient</jarName>
                          <version>1.0.0</version>
                          <outputDirectory>${project.build.directory}/utility/XplanrBatchClient/lib</outputDirectory>
                          <archive>
                              <manifestEntries>
                                  <implementation-version>${project.version}</implementation-version>
                                  <implementation-build>${buildNumber}</implementation-build>
                              </manifestEntries>
                          </archive>
                      </configuration>
                  </plugin>
          
              </plugins>
          </build>
          <version>1.0.0</version>
          

        2.然后你必须为主项目创建一个pom 在那里你必须提到作为父母,你可以在下面看到

        <parent>
            <groupId>com</groupId>
            <artifactId>practice</artifactId>
            <version>0.0.1</version>
        </parent>
        <groupId>com.practice</groupId>
        <artifactId>framework</artifactId>
        <packaging>war</packaging>
        <version>1.0.0</version>
        <name>DAG Maven Webapp</name>
        <url>http://www.practiceanalytics.com/</url>
        <repositories>
            <repository>
                <id>practice</id>
                <name>framework</name>
                <url>file://libs</url>
            </repository>
            <repository>
              <id>cloudera</id>
              <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
              <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </repository>
        
        </repositories>
        <dependencies>
        
        
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.2</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
        
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>default-jar</id>
                            <goals>
                                <goal>jar</goal>
                            </goals>
                            <configuration>
                                <classifier>client</classifier>
                                <finalName>udf-framework</finalName>
                                <includes>
                                    <include>**/udf/*</include>
                                </includes>
                                <outputDirectory>${project.build.directory}/udf/lib</outputDirectory>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
        
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.6</version>
                    <configuration>
                        <!-- <archiveClasses>true</archiveClasses>
                        <webresources>
                            <resource>
                                <directory>src/main/resources</directory>
                                <targetpath>WEB-INF/classes</targetpath>
                                <filtering>true</filtering>
                            </resource>
                        </webresources>
                        <warName>${project.war.name}</warName>
                        <warSourceExcludes>**/*.class</warSourceExcludes> -->
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
            </plugins>
            <finalName>framework</finalName>
        </build>
        <description>Web Project for Practice Code</description>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-08-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-03-01
          • 2019-03-01
          相关资源
          最近更新 更多