将 Java EE Web 应用程序导出为 JAR

这可能是一个奇怪的问题,但我对此很好奇:

我想创建一个 Java EE Web 项目,我可以将其打包到 JAR 文件中(<packaging>jar</packaging>而不是<packaging>war</packaging>)。

换句话说,我想在 JAR(由 maven 构建)中包含 Java EE Web 服务器。

在 WebServer 内部,我想使用 Servlet,就像我可以在将其打包到 WAR 文件时使用它们一样,但不需要执行 JAR 的设备安装可以部署我的 JAR 的 Web 服务器。

我想要一个类似可执行 JAR 的东西,它包含服务器并运行它而无需安装其他东西。

是否有在 JAR 文件中工作的(理想情况下是轻量级的)服务器或任何其他创建这样的 JAR 文件的可能性?


德玛西亚99
浏览 109回答 1
1回答

湖上湖

如果你想使用 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"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">&nbsp; &nbsp; <modelVersion>4.0.0</modelVersion>&nbsp; &nbsp; <groupId>org.company</groupId>&nbsp; &nbsp; <artifactId>app</artifactId>&nbsp; &nbsp; <version>0.0.1-SNAPSHOT</version>&nbsp; &nbsp; <packaging>jar</packaging>&nbsp; &nbsp; <properties>&nbsp; &nbsp; &nbsp; &nbsp; <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>&nbsp; &nbsp; &nbsp; &nbsp; <maven.compiler.source>12</maven.compiler.source>&nbsp; &nbsp; &nbsp; &nbsp; <maven.compiler.target>12</maven.compiler.target>&nbsp; &nbsp; &nbsp; &nbsp; <servlet.version>3.1.0</servlet.version>&nbsp; &nbsp; &nbsp; &nbsp; <jsf.version>2.2.19</jsf.version>&nbsp; &nbsp; &nbsp; &nbsp; <tomcat.version>9.0.21</tomcat.version>&nbsp; &nbsp; </properties>&nbsp; &nbsp; <dependencies>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>com.sun.faces</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>jsf-api</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>${jsf.version}</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>com.sun.faces</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>jsf-impl</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>${jsf.version}</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.apache.tomcat.embed</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>tomcat-embed-core</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>${tomcat.version}</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp;<dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<groupId>org.apache.tomcat</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<artifactId>tomcat-jasper</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<version>${tomcat.version}</version>&nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<groupId>org.apache.tomcat.embed</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<artifactId>tomcat-embed-el</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<version>${tomcat.version}</version>&nbsp; &nbsp; &nbsp; </dependency>&nbsp; </dependencies>&nbsp; &nbsp; <build>&nbsp; &nbsp; &nbsp; &nbsp; <finalName>app</finalName>&nbsp; &nbsp; &nbsp; &nbsp; <resources>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <resource>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <directory>src/main/webapp</directory>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <targetPath>META-INF/resources</targetPath>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </resource>&nbsp; &nbsp; &nbsp; &nbsp; </resources>&nbsp; &nbsp; &nbsp; &nbsp; <plugins>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.apache.maven.plugins</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>maven-compiler-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>3.8.1</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <source>12</source>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <target>12</target>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.apache.maven.plugins</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>maven-assembly-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <descriptorRefs>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <descriptorRef>jar-with-dependencies</descriptorRef>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </descriptorRefs>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <finalName>app-${project.version}</finalName>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <archive>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <manifest>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <mainClass>org.company.app.Application</mainClass>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </manifest>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </archive>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <executions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <phase>package</phase>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goal>single</goal>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </executions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; &nbsp; &nbsp; </plugins>&nbsp; &nbsp; </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 {&nbsp; &nbsp; public static void main(final String[] args) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; final String webappPath = new File("src/main/webapp").getAbsolutePath();&nbsp; &nbsp; &nbsp; &nbsp; final Tomcat tomcat = new Tomcat();&nbsp; &nbsp; &nbsp; &nbsp; final StandardContext ctx = (StandardContext) tomcat.addWebapp("/", webappPath);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(ctx&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; // Declare an alternative location for your "WEB-INF/classes" dir&nbsp; &nbsp; &nbsp; &nbsp; // Servlet 3.0 annotation will work&nbsp; &nbsp; &nbsp; &nbsp; final String targetClassesPath = new File("target/classes").getAbsolutePath();&nbsp; &nbsp; &nbsp; &nbsp; final WebResourceRoot resources = new StandardRoot(ctx);&nbsp; &nbsp; &nbsp; &nbsp; resources.addPreResources(new DirResourceSet(//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resources, "/WEB-INF/classes", //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; targetClassesPath, "/"));&nbsp; &nbsp; &nbsp; &nbsp; ctx.setResources(resources);&nbsp; &nbsp; &nbsp; &nbsp; tomcat.start();&nbsp; &nbsp; &nbsp; &nbsp; tomcat.getServer().await();&nbsp; &nbsp; }}剩下的和往常一样 java ee 开发
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java