这个 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
- 快速会话 = 真
但是,在没有任何参数的情况下,每次运行时 apache-tomee 目录都设置为 "tomee-embedded_" + System.currentTimeMillis() + "-home"],并且应用程序名称和路径设置为 null,从而产生 404 错误本地主机:8080。为保持一致,请将目录设置为单个位置。
此外,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
这可能无法回答您的所有配置问题或为您的项目提供理想的框架,但它应该可以帮助您入门。