使用Gradle的多个启动脚本 [英] Multiple start scripts using Gradle

查看:428
本文介绍了使用Gradle的多个启动脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在运行的Maven构建版本(如下所示),它准备了几个可执行文件作为两个单独的进程启动。



虽然这很好,但怎么可能呢使用Gradle完成?我发现Gradle提供了一个名为 application 的插件,但是我很难找到一个很好的例子来告诉它,输入时: gradle stage ,它应该创建2个可执行文件。



现在,当我调用 stage 时,在我的gradle脚本中定义的root主类上提供了一个可执行文件:

  apply plugin:'java'
apply插件:'application'

mainClassName ='SpringLauncher'
applicationName ='foo'
compileJava.options.encoding ='UTF-8'
targetCompatibility ='1.7 '
sourceCompatibility ='1.7'

任务阶段(dependsOn:['clean','installApp','hello'])

以及Maven构建:

 < build> 
< plugins>
< plugin>
< groupId> org.codehaus.mojo< / groupId>
< artifactId> appassembler-maven-plugin< / artifactId>
< version> 1.1.1< / version>
<配置>
< assembleDirectory>目标< / assembleDirectory>
<程式>
< program>
< mainClass> foo.bar.scheduler.SchedulerMain< / mainClass>
<名称>排程器< /名称>
< / program>
< program>
< mainClass> SpringLauncher< / mainClass>
<名称>网页< /名称>
< / program>
< / programs>
< / configuration>
<执行次数>
<执行>
阶段包< /阶段><目标><目标>汇编< /目标>< /目标>
< /执行>
< /执行次数>
< / plugin>
< / plugins>


解决方案

不幸的是,gradle应用程序插件没有为多个可执行脚本提供一流的支持。

幸运的是,由于gradle脚本很流行,你可以很容易地改变应用程序插件的功能。

Application插件的文档显示 startScripts 任务的类型为 CreateStartScripts ,因此请尝试创建自己的同一类型的第二个任务

 任务schedulerScripts(type:CreateStartScripts){
mainClassName =foo.bar.scheduler.SchedulerMain
applicationName =scheduler
outputDir = new File(project.buildDir,'scripts')
classpath = jar.outputs.files + project.configurations.runtime
}

然后在你的发行版中包含该任务的输出

  applicationDistribution.into(bin){
from(schedulerScripts)
fileMode = 0755
}


I have a working Maven build (shown below) that prepares a couple of executables to launch as two separate processes.

Although this works fine, how can this be done using Gradle? I see that Gradle provides a plugin called application, but I have a hard time finding a nice example on how to tell it that when typing: gradle stage, it should create 2 executables.

Right now when I call stage it only provides an executable on the "root" mainclass defined in my gradle script:

apply plugin: 'java'
apply plugin: 'application'

mainClassName = 'SpringLauncher'
applicationName = 'foo'
compileJava.options.encoding = 'UTF-8'
targetCompatibility = '1.7'
sourceCompatibility = '1.7'

task stage(dependsOn: ['clean', 'installApp', 'hello'])

And the Maven build:

<build>
<plugins>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
      <artifactId>appassembler-maven-plugin</artifactId>
      <version>1.1.1</version>
      <configuration> 
        <assembleDirectory>target</assembleDirectory> 
        <programs>
            <program>
                <mainClass>foo.bar.scheduler.SchedulerMain</mainClass>
                <name>scheduler</name>
            </program>
            <program>
                <mainClass>SpringLauncher</mainClass>
                <name>web</name>
            </program>
        </programs>
      </configuration>
      <executions>
          <execution>
              <phase>package</phase><goals><goal>assemble</goal></goals>
          </execution>            
      </executions>
  </plugin>
</plugins>

解决方案

Unfortunately the gradle application plugin does not provide first class support for multiple executable scripts.

Luckily though, because gradle scripts are groovy, you can change what the application plugin does reasonably easily.

The documentation for the Application plugin show that the startScripts task is of type CreateStartScripts, so try creating yourself a second task of the same type

task schedulerScripts(type: CreateStartScripts) {
    mainClassName = "foo.bar.scheduler.SchedulerMain"
    applicationName = "scheduler" 
    outputDir = new File(project.buildDir, 'scripts')
    classpath = jar.outputs.files + project.configurations.runtime
}

then include the output of that task in your distribution

applicationDistribution.into("bin") {
            from(schedulerScripts)
            fileMode = 0755
}

这篇关于使用Gradle的多个启动脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆