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

export Java EE Web Application as JAR

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

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

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

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

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

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

如果你想使用 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 development