1 回答
TA贡献2003条经验 获得超2个赞
如果你想使用 vanilla Java EE,你可以使用嵌入式 Jetty 服务器或嵌入式 Tomcat 服务器:
以下是嵌入式 Tomcat 和 Maven 的示例:
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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>org.company</groupId>
<artifactId>app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
<servlet.version>3.1.0</servlet.version>
<jsf.version>2.2.19</jsf.version>
<tomcat.version>9.0.21</tomcat.version>
</properties>
<dependencies>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>${jsf.version}</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>${jsf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-el</artifactId>
<version>${tomcat.version}</version>
</dependency>
</dependencies>
<build>
<finalName>app</finalName>
<resources>
<resource>
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>12</source>
<target>12</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>app-${project.version}</finalName>
<archive>
<manifest>
<mainClass>org.company.app.Application</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
主类:
package org.company.app;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;
import java.io.File;
public class Application {
public static void main(final String[] args) throws Exception {
final String webappPath = new File("src/main/webapp").getAbsolutePath();
final Tomcat tomcat = new Tomcat();
final StandardContext ctx = (StandardContext) tomcat.addWebapp("/", webappPath);
System.out.println(ctx
);
// Declare an alternative location for your "WEB-INF/classes" dir
// Servlet 3.0 annotation will work
final String targetClassesPath = new File("target/classes").getAbsolutePath();
final WebResourceRoot resources = new StandardRoot(ctx);
resources.addPreResources(new DirResourceSet(//
resources, "/WEB-INF/classes", //
targetClassesPath, "/"));
ctx.setResources(resources);
tomcat.start();
tomcat.getServer().await();
}
}
剩下的和往常一样 java ee 开发
添加回答
举报