java - 如何将 Web 服务器嵌入到 CAD Web 应用程序版本中

标签 java jetty maven-3 cas embedded-jetty

我尝试运行 CAS webapp,例如可执行的 WAR,最初我发现了两种可能的解决方案。但有更好的解决方案吗?

1) 创建一个主类来启动 Jetty 嵌入式服务器并运行 CAS war 文件;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.tomcat.InstanceManager;
import org.apache.tomcat.SimpleInstanceManager;
import org.eclipse.jetty.annotations.ServletContainerInitializersStarter;
import org.eclipse.jetty.apache.jsp.JettyJasperInitializer;
import org.eclipse.jetty.plus.annotation.ContainerInitializer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
import org.h2.jdbcx.JdbcDataSource;

public class CasServerEmbedded {

    public static void main(String[] args) throws Exception {

        // Create the server
        Server server = new Server(8080);

        // Enable parsing of jndi-related parts of web.xml and jetty-env.xml
        org.eclipse.jetty.webapp.Configuration.ClassList classlist = org.eclipse.jetty.webapp.Configuration.ClassList
                .setServerDefault(server);
        classlist.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration",
                "org.eclipse.jetty.plus.webapp.EnvConfiguration", "org.eclipse.jetty.plus.webapp.PlusConfiguration");

        // Create a WebApp
        final WebAppContext webAppContext = new WebAppContext();
        webAppContext.setAttribute("javax.servlet.context.tempdir", getScratchDir());
        webAppContext.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
                ".*/[^/]*servlet-api-[^/]*\\.jar$|.*/javax.servlet.jsp.jstl-.*\\.jar$|.*/.*taglibs.*\\.jar$");
        webAppContext.setAttribute("org.eclipse.jetty.containerInitializers", jspInitializers());
        webAppContext.setAttribute(InstanceManager.class.getName(), new SimpleInstanceManager());
        webAppContext.addBean(new ServletContainerInitializersStarter(webAppContext), true);
        webAppContext.setWar("C:\\temp\\cas-server-webapp-4.2.1.war");
        server.setHandler(webAppContext);

        final JdbcDataSource jdbcDataSource = new JdbcDataSource();
        jdbcDataSource.setURL("jdbc:h2:c:\\temp\\teste;mv_store=false");
        jdbcDataSource.setUser("sa");
        jdbcDataSource.setPassword("sa");
        jdbcDataSource.getConnection();

        new org.eclipse.jetty.plus.jndi.Resource(webAppContext, "jdbc/mydatasource", jdbcDataSource);

        server.start();
        server.join();
    }

    /**
     * Establish Scratch directory for the servlet context (used by JSP
     * compilation)
     */
    private static File getScratchDir() throws IOException {
        File tempDir = new File(System.getProperty("java.io.tmpdir"));
        File scratchDir = new File(tempDir.toString(), "embedded-jetty-jsp");

        if (!scratchDir.exists()) {
            if (!scratchDir.mkdirs()) {
                throw new IOException("Unable to create scratch directory: " + scratchDir);
            }
        }
        return scratchDir;
    }

    /**
     * Ensure the jsp engine is initialized correctly
     */
    private static List<ContainerInitializer> jspInitializers() {
        JettyJasperInitializer sci = new JettyJasperInitializer();
        ContainerInitializer initializer = new ContainerInitializer(sci, null);
        List<ContainerInitializer> initializers = new ArrayList<ContainerInitializer>();
        initializers.add(initializer);
        return initializers;
    }
}

2) 使用 Maven,我可以执行目标 jetty:run-war,它会在 Jetty 嵌入式服务器内启动 WAR 覆盖层。

<?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.jasig.cas</groupId>
    <artifactId>cas-overlay</artifactId>
    <packaging>war</packaging>
    <version>1.0</version>
    <properties>
        <cas.version>4.2.1</cas.version>
        <maven-jetty-plugin.version>9.3.6.v20151106</maven-jetty-plugin.version>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.jasig.cas</groupId>
            <artifactId>cas-server-webapp</artifactId>
            <version>${cas.version}</version>
            <type>war</type>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <warName>cas</warName>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <overlays>
                        <overlay>
                            <groupId>org.jasig.cas</groupId>
                            <artifactId>cas-server-webapp</artifactId>
                        </overlay>
                    </overlays>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>${maven-jetty-plugin.version}</version>
                <configuration>
                    <jettyXml>${basedir}/etc/jetty/jetty.xml</jettyXml>
                    <systemProperties>
                        <systemProperty>
                            <name>org.eclipse.jetty.annotations.maxWait</name>
                            <value>240</value>
                        </systemProperty>
                    </systemProperties>
                    <webApp>
                        <contextPath>/cas</contextPath>
                        <overrideDescriptor>${basedir}/etc/jetty/web.xml</overrideDescriptor>
                    </webApp>
                    <webAppConfig>
                        <allowDuplicateFragmentNames>true</allowDuplicateFragmentNames>
                    </webAppConfig>
                    <jvmArgs>-Dlog4j.configurationFile=/etc/cas/log4j2.xml -Xdebug
                        -Xrunjdwp:transport=dt_socket,address=5000,server=y,suspend=n</jvmArgs>
                </configuration>
            </plugin>
        </plugins>
        <finalName>cas</finalName>
    </build>
</project>
正如你所看到的,这两种解决方案都将 CAS 启动为独立的 Web 服务器,但我想要实现的是 Jenkins 所做的事情,一个可以通过简单的命令行启动的 WAR 文件:java -jar CAS.war

有什么方法可以将 Maven 构建导出到通过嵌入的 Jetty 运行的 WAR 文件中吗?

谢谢,

最佳答案

此行为是由 CAS v5 和 Spring Boot 自动提供的。不要重新发明轮子。

关于java - 如何将 Web 服务器嵌入到 CAD Web 应用程序版本中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37230943/

相关文章:

java - Spring AMQP : Unable to set MessageProperties using RabbitMessagingTemplate

jetty - 如何在 Ubuntu 16.04 中为 CKAN 设置 jetty.home

java - Maven 项目管理

maven - 将 Maven 输出发送到文件

java - 运行所有 Maven 测试时无法控制 log4j

java - 如何在 LinearLayout 中隐藏部分可见的 View ?

java - golang 中 key 长度为 15360 的 RSA key 对生成速度太慢。需要做些什么来解决这个问题?

java - 如何根据 Camel Exchange 的起源地进行不同的处理?

java - 如何增加 Java 6 的最大堆大小?

java - Jetty 服务器的可扩展负载均衡器