【问题标题】:TomEE embedded/bundle with single applicationTomEE 嵌入/捆绑单个应用程序
【发布时间】:2015-12-08 15:44:54
【问题描述】:

我目前正在探索构建一个绑定到 TomEE 服务器的 web 应用程序,即它要么作为 TomEE 中的唯一(附加)应用程序运行,要么嵌入了 TomEE。 webapp 应该支持 servlet 和 JSP(基本上我们希望在这里使用 Struts2)。

我尝试了几个选项,但都有几个问题:

选项 1:使用 Maven-Shade-Plugin 捆绑我们的应用程序和 tomee-embedded,如下所述:http://www.tomitribe.com/blog/2015/03/50-shades-of-tomee/

问题:

1) 部署捆绑的应用程序仅适用于通过 --as-war 选项的 7.x 版本,但 7.x 尚未准备好生产,我找不到任何计划的发布日期

2) 部署 JAXRS webservices 工作正常,但 servlet 没有,例如web.xml 没有被拾取,存在类加载问题等。

选项 2:使用 TomEE-Maven-Plugin 构建一个包含 tomee 以及我们的 web 应用程序的 zip。

问题:

1) 这仅适用于插件版本 7.x(尚未准备好生产),但这不应影响生成的构建(基于 TomEE 1.7.2)

2) 由于 TomEE 可能会加载一些也由应用程序提供的库(在不同版本中),因此存在库冲突。

由于应用程序与服务器绑定,反之亦然,因此应该可以将所有库放入 TomEE 的 lib 文件夹,该文件夹通过将 Maven 插件指向下载依赖项的文件夹来工作。这可行,但没有解决冲突,即库只是复制到目标文件夹。

我现在可以手动配置插件以删除每个冲突的工件,但随着应用程序以及潜在冲突库数量的增加,这可能会变得非常麻烦。

选项 3:与选项 2 类似,但不使用 TomEE-Maven-Plugin。

我想应该可以使用预打包的 TomEE 和一些 Maven 插件来构建一个可分发的 zip 来解决问题。 1 但问题没有。 2 - 这是更大的 - 仍然存在:手动处理所有依赖项会很麻烦。


最后,问题是:我应该如何构建该应用程序?

我更喜欢选项 1,但我还没有找到任何关于如何使其工作的文档/示例/教程。

选项 2 和 3 也可以,但我需要一些方法让 Maven 解决依赖冲突,而不需要我检查每个依赖项,尤其是传递的依赖项。

【问题讨论】:

    标签: java maven apache-tomee tomee-7


    【解决方案1】:

    此 MySQL 数据库应用程序类似于您描述的使用 Maven 处理依赖项并在启动时加载嵌入的 tomee 的应用程序。

    public class Main {
        public static void main(String[] args) {
                String currentDir = null;
            try {
                currentDir = new File(".").getCanonicalPath();
                LOG.info("Current application directory: " + currentDir);
            } catch (IOException e) {
                e.printStackTrace();
            }
            String war = currentDir + "\\target\\app.war";
            String tomeeConf = currentDir + "\\src\\main\\tomee\\conf\\tomee.xml";
    
            // ARGUMENTS
            String[] setup = { "--path", war, "--tomeexml", tomeeConf };
            try {
                // PROGRAM:
                org.apache.tomee.embedded.Main.main(setup);
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    Tomee 嵌入式文档有点稀少,因为这仍然是一项正在开发的技术,而且很难找到有用的工作示例。配置具有挑战性,如果我没有感谢 Mark Struberg 为我自己的程序启动和运行所提供的帮助,那就太失职了。

    Tomee Embedded 需要一个位于以下位置的配置文件: src/main/tomee/tomee.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <tomee>
    <!-- Connection details specified in src/main/resource/persistence.xml -->
    <Resource id="APP-DATA" type="javax.sql.DataSource">
            IgnoreDefaultValues true 
            JdbcDriver com.mysql.jdbc.Driver
            JdbcUrl jdbc:mysql://localhost:3306/scc-data
            UserName admin
            Password pass
            JtaManaged true
        </Resource>
    </tomee>
    

    Tomee 提供以下关键默认值:

    • 默认值 [Tomee 配置类]
    • httpPort = 8080
    • 停止端口 = 8005
    • 主机=“本地主机”
    • https 端口 = 8443
    • quickSession = 真

    但是,没有任何参数,apache-tomee 目录设置为 "tomee-embedded_" + System.currentTimeMillis() + "-home"] 在每次运行时,应用程序名称和路径都设置为 null,在 localhost:8080 处产生 404 错误。为保持一致性,请将目录设置为单个位置。

    此外,Tomee 默认将 OpenJPA 作为 JPA 提供者。当我开始开发时,Tomee 7.0.2 使用了 OpenJPA 2.4.1,它有一个未解决的问题 [OPENJPA-2635 3/20/16] 提示切换到 Hibernate。 (这个错误很可能已经在版本 7.0.3 中得到修复。)Tomee 的加载过程显然在读取 persistence.xml 或 tomee.conf 之前将 OpenJPA 实现为提供程序。为了防止过早加载 OpenJPA,添加到 persistence.xml:

    http://stackoverflow.com/questions/40818396/

    还应该将 Hibernate 添加到 apache-tomee 库中,在我的例子中是 hibernate-core.jar,它位于 F:\theapp\apache-tomee\lib,其范围设置为在 pom 中提供。

    http://stackoverflow.com/questions/10852035/how-to-use-tomee-with-hibernate

    请注意,Tomee 7.0.3 实现了 Tomcat 8.5.11 和 JavaEE 7,并利用: Java Servlet 3.1、JSF 2.2(myfaces 2.2.11)、JSTL 1.2、JSP 2.3、
    EL 3.0 和 JPA 2.1。

    父 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">
      <modelVersion>4.0.0</modelVersion>
      <groupId>rfpeake</groupId>
      <artifactId>theapp-parent</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>pom</packaging>
      <name>theapp</name>
      <description>Database project</description>
    
        <modules>
            <module>../theapp-app</module>
        </modules>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <tomee.version>7.0.3</tomee.version>
            <tomee.classifier>webprofile</tomee.classifier>
            <mysql-connector-version>5.1.40</mysql-connector-version>
            <hibernate.version>5.2.9.Final</hibernate.version>
            <primefaces.version>6.1</primefaces.version>                    
        </properties>
    
        <dependencies>
    
            <!-- TOMEE EMBEDDED -->
            <dependency>
                <groupId>org.apache.tomee</groupId>
                <artifactId>tomee-embedded</artifactId>
                <version>${tomee.version}</version>            
            </dependency>       
    
            <!-- Declaration for JSF 2.2 with Tomee 7 -->
            <dependency>
                <groupId>javax</groupId>
                <artifactId>javaee-web-api</artifactId>
                <version>7.0</version>
                <scope>provided</scope>
            </dependency>
    
            <!-- JPA spec [required] -->
            <dependency>
                <groupId>org.apache.geronimo.specs</groupId>
                <artifactId>geronimo-jpa_2.0_spec</artifactId>
                <version>1.1</version>
                <scope>provided</scope>
            </dependency>
    
            <!-- Hibernate -->
            <dependency>
             <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>${hibernate.version}</version>
                <scope>provided</scope>
            </dependency>
    
            <!--  Hibernate EHCache -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
    
            <!-- Primefaces -->
        <dependency>
                    <groupId>org.primefaces</groupId>
                    <artifactId>primefaces</artifactId>
                    <version>${primefaces.version}</version>
    
            <!-- JUnit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
    </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">
        <modelVersion>4.0.0</modelVersion>
    
        <parent>
            <groupId>rfpeake</groupId>
            <artifactId>theapp-parent</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <relativePath>../theapp-parent/pom.xml</relativePath>
        </parent>
    
        <artifactId>theapp-app</artifactId>
        <packaging>war</packaging>
        <name>Database Application</name>
    
        <build>
            <finalName>theapp</finalName>
    
            <!-- pluginManagement tag required in Eclipse to avoid error:
                Plugin execution not covered by lifecycle configuration. -->
            <pluginManagement>
                <plugins>               
                    <plugin>
                        <groupId>org.apache.tomee.maven</groupId>
                        <artifactId>tomee-embedded-maven-plugin</artifactId>
                        <version>${tomee.version}</version>
                        <configuration>
                            <tomeeVersion>${tomee.version}</tomeeVersion>
                            <tomeeClassifier>
                                ${tomee.classifier}</tomeeClassifier>
                            <synchronization>
                                <extensions>
                                    <!-- To update each time app built
                                        with mvn compile -->
                                    <extension>.class</extension>
                                    <extension>.properties</extension>
    
                                    <extension>.css</extension>
                                </extensions>
                                <updateInterval>2</updateInterval>
                            </synchronization>
                            <!-- For some reason, must to be false
                                or reloading does not work! -->
    
                            <reloadOnUpdate>false</reloadOnUpdate>
    
                            <warFile>
                                ${project.build.directory}/${project.build.finalName}
                            </warFile>
                            <!-- path tused by tomEE in the tomee:deploy and 
                                tomee:undeploy goals -->
                            <path>
                                ${project.build.directory}/apache-                              tomee/webapps${project.build.finalName}
                            </path>
                            <args>
                                -Djava.awt.headless=true -Dfile.encoding=UTF-8 -server
                                -Xms128m -Xmx4096m -XX:PermSize=196m -XX:MaxPermSize=128m
                                -XX:+DisableExplicitGC</args>
                            <libs>
                                <lib>
                                    mysql:mysql-connector-java:${mysql-connector-version}
                                </lib>
                            </libs>
                        </configuration>
                        <dependencies>
                            <dependency>
                                <groupId>org.apache.tomee</groupId>
                                <artifactId>apache-tomee</artifactId>
                                <version>${tomee.version}</version>
                                <classifier>${tomee.classifier}</classifier>
                                <type>zip</type>
                                <!-- set this to runtime for it to work -->
                                <scope>runtime</scope>
                            </dependency>
                        </dependencies>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>2.3.2</version>
                        <configuration>
                            <source>1.7</source>
                            <target>1.7</target>
                            <showDeprecation>true</showDeprecation>
                        </configuration>
                    </plugin>
                </plugins>
            </pluginManagement>
        </build>
    </project>
    

    关于插件管理标签,见http://stackoverflow.com/questions/6352208/how-to-solve-plugin-execution-not-covered-by-lifecycle-configuration-for-sprin

    这可能无法回答您的所有配置问题或为您的项目提供理想的框架,但它应该可以帮助您入门。

    【讨论】:

    • 谢谢,这绝对是一个好的开始。
    • 嗨@Robert,关于这个“Hibernate 也应该添加到 apache-tomee 库中”,是否足以将 hibernate 作为依赖项添加到我的 gradle 构建文件中TomEE 库文件夹?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-27
    • 2021-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    相关资源
    最近更新 更多