简体   繁体   English

Gradle:使用Jetty运行多个webapps

[英]Gradle: Run multiple webapps with Jetty

I want to run two webapps created by the same Gradle project into a Jetty server. 我想将同一个Gradle项目创建的两个webapp运行到Jetty服务器中。 Let's call these two webapps "ninja" and "warrior". 我们称这两个webapps为“ninja”和“warrior”。

Both webapps are very similar, they only differ in the application-context file (referenced in the web.xml file) and resources. 两个webapp都非常相似,它们仅在应用程序上下文文件(在web.xml文件中引用)和资源上有所不同。

In order to deploy them, these two options are accepted: 为了部署它们,接受这两个选项:

  • Use different ports: 使用不同的端口:
http://www.example.com:8080/app (ninja webapp)
http://www.example.com:8081/app (warrior webapp)
  • Use different paths: 使用不同的路径:
http://www.example.com:8080/ninja_app
http://www.example.com:8080/warrior_app

Having one or two instances of Jetty should be ok for this project. 有一个或两个Jetty实例应该可以用于这个项目。

This is my project layout: 这是我的项目布局:

/src/main/java
/src/main/resources
/src/main/webapp (ninja webapp)
/src/main/webapp-warrior

First question: How to create two war files with Gradle? 第一个问题:如何使用Gradle创建两个war文件?

Second question: How to deploy the two war files in the Jetty Server with Gradle? 第二个问题:如何使用Gradle在Jetty服务器中部署两个war文件?

Please have a look at Gretty gradle plugin: https://github.com/akhikhl/gretty 请看一下Gretty gradle插件: https//github.com/akhikhl/gretty

It supports multiple web-apps out of the box. 它支持多个开箱即用的Web应用程序。 It helps you to: 它可以帮助您:

  1. run web-app projects "inplace", ie from compiled classes 运行web-app项目“inplace”,即从编译的类中运行
  2. run web-app projects as WAR files 将web-app项目作为WAR文件运行
  3. run non-project WAR-files from the file system 从文件系统运行非项目WAR文件
  4. run non-project WAR-files from maven repositories (of course, WAR is downloaded first) 从maven存储库运行非项目WAR文件(当然,首先下载WAR)

Additionally you can: 此外,您可以:

  • Debug multiple web-apps on the same Jetty server under any Java IDE debugger. 在任何Java IDE调试器下,在同一Jetty服务器上调试多个Web应用程序。
  • Perform integration tests with multiple web-apps on the same Jetty server. 在同一Jetty服务器上与多个Web应用程序执行集成测试。
  • Perform code coverage with Jacoco - both client-side and server-side. 使用Jacoco执行代码覆盖 - 包括客户端和服务器端。

There is a lot of documentation helping with everything: http://akhikhl.github.io/gretty-doc/ 有很多文档可以帮助解决所有问题: http//akhikhl.github.io/gretty-doc/

Disclosure: I am author of Gretty plugin. 披露:我是Gretty插件的作者。

Happy coding :) 快乐编码:)

If you don't want to create two different projects, you may want to create two different gradle profiles, using the apply from: feature from Gradle. 如果您不想创建两个不同的项目,则可能需要使用Gradle中的apply from:功能创建两个不同的gradle配置文件。

For each webapp instance, ninja and warrior, you must create a script file with all the information specific for the profile. 对于每个webapp实例,ninja和warrior,您必须创建一个脚本文件,其中包含特定于该配置文件的所有信息。

In these new gradle build files, ninja-profile.gradle and warrior-profile.gradle, you can set the specific configurations that differ from ninja to warrior, which in this case could be: 在这些新的gradle构建文件ninja-profile.gradle和warrior-profile.gradle中,您可以设置不同于ninja和warrior的特定配置,在这种情况下可以是:

  1. Resources folder: you can create a separated resources folder for each of the instances 资源文件夹:您可以为每个实例创建一个单独的资源文件夹
  2. Jetty configuration: if you want to run two different instances of the 2 webapps, each of them in a separated jetty instance. Jetty配置:如果要运行两个不同的2个webapps实例,每个实例都在一个独立的jetty实例中。

In your "main" build file you define everything that is common for all profile and build needs, plus you add the following line: 在“main”构建文件中,您可以定义所有配置文件和构建需求的常用内容,并添加以下行:

apply from: "${profile}-profile.gradle"

When you run Gradle you can pass the name of the profile using the -P option: 运行Gradle时,可以使用-P选项传递配置文件的名称:

$ gradle -Pprofile=ninja tasks 

or 要么

$ gradle -Pprofile=warrior tasks

We finally implemented a solution where sourceSets were created for both projects, so the directory layout is: 我们最终实现了一个解决方案,其中为两个项目创建了sourceSets,因此目录布局是:

  • /src/main/java : Contains common classes for both projects /src/main/java :包含两个项目的公共类
  • /src/main/webapp : Contains common webapp for both projects. /src/main/webapp :包含两个项目的通用webapp。
  • /src/ninjaMain/resources : Contains the specific resources for the ninja project /src/ninjaMain/resources :包含忍者项目的特定资源
  • /src/warriorMain/resources : Contains the specific resources for the warrior project /src/warriorMain/resources :包含warrior项目的特定资源

Then, the build.gradle file was configured to create two war files. 然后,build.gradle文件配置为创建两个war文件。 After that, both war files were deployed in the same webapp container using cargo and jetty: 之后,两个war文件都使用货物和码头部署在同一个webapp容器中:

apply 'java'
apply 'war'
apply 'cargo'


task createNinjaWar(type: War, dependsOn: classes) {
    baseName = 'ninja'      
    from file('src/main/webapp')
    destinationDir = file("$buildDir/dist")     
    webInf {
        from ('src/ninjaMain/resources') { into 'classes' }
    }
}

task createWarriorWar(type: War, dependsOn: classes) {  
    baseName = 'warrior'    
    from file('src/main/webapp')
    destinationDir = file("$buildDir/dist")                 
    webInf {
        from ('src/warriorMain/resources') { into 'classes' }       
    }
}    

// Deploy
cargo {
    containerId = 'jetty9x'
    port = 8080
    deployable {
        context = 'ninja'           
        file = createNinjaWar.archivePath
    }
    deployable {
        context = 'warrior'
        file = createWarriorWar.archivePath
    }
}

The access URL for the two web applications are now: 现在,这两个Web应用程序的访问URL是:

  • http://www.example.com:8080/ninja
  • http://www.example.com:8080/warrior

For better readbility, code organisation and simpler maintenance these two projects should be separated. 为了更好的可读性,代码组织和更简单的维护,这两个项目应该分开。 Then jetty plugin can be applied and configured separately for each project and all env can be start in parent build.gradle file. 然后可以为每个项目单独应用和配置jetty插件,并且所有env都可以在父build.gradle文件中启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM