Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Maven 使用Gradle的多个启动脚本_Maven_Gradle - Fatal编程技术网

Maven 使用Gradle的多个启动脚本

Maven 使用Gradle的多个启动脚本,maven,gradle,Maven,Gradle,我有一个工作的Maven构建(如下所示),它准备了两个可执行文件作为两个单独的进程启动 虽然这很好,但如何使用Gradle实现呢?我看到Gradle提供了一个名为application的插件,但我很难找到一个很好的例子来说明当键入:GradleStage时,它应该创建两个可执行文件 现在,当我调用stage时,它只在gradle脚本中定义的“root”main类上提供一个可执行文件: apply plugin: 'java' apply plugin: 'application' mainCl

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

虽然这很好,但如何使用Gradle实现呢?我看到Gradle提供了一个名为
application
的插件,但我很难找到一个很好的例子来说明当键入:
GradleStage
时,它应该创建两个可执行文件

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

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'])
以及Maven构建:

<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>

org.codehaus.mojo
appassembler maven插件
1.1.1
目标
foo.bar.scheduler.SchedulerMain
调度程序
弹簧发射器
网状物
包装组件

使用
JavaExec可能更好

task scheduler(type: JavaExec) {
   main = "foo.bar.scheduler.SchedulerMain"
   classpath = sourceSets.main.runtimeClasspath
}

task web(type: JavaExec) {
   main = "SpringLauncher"
   classpath = sourceSets.main.runtimeClasspath
}

然后,您可以运行
gradleschedulerweb

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

幸运的是,因为gradle脚本是groovy,所以您可以很容易地更改应用程序插件的功能

这表明
startScripts
任务属于同一类型,因此请尝试为自己创建第二个相同类型的任务

task 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
}

我发现的simples方法是添加一个新的
CreateStartScripts
任务,并使其成为
schedulerScripts
的依赖项:

task schedulerScripts(type: CreateStartScripts) {
    mainClassName = 'foo.bar.scheduler.SchedulerMain'
    applicationName = 'scheduler'
    classpath = startScripts.classpath
    outputDir = startScripts.outputDir
}
startScripts.dependsOn schedulerScripts
科特林DSL 可重用函数 等效于使用Gradle Kotlin DSL,并将创建多个脚本的通用逻辑抽象为可重用函数:

fun createAdditionalScript(名称:字符串,配置StartScripts:CreateStartScripts.(->单位)=
任务。注册(“startScripts$name”){
配置StartScripts()
applicationName=name
outputDir=文件(project.buildDir,“脚本”)
classpath=tasks.getByName(“jar”).outputs.files+configurations.runtimeClasspath.get()
}.还有{
application.applicationDistribution.into(“bin”){
来自(it)
fileMode=0b000\u 111\u 101\u 101
duplicatesStrategy=duplicatesStrategy.EXCLUDE
}
}
现在你可以这样使用它:

createAdditionalScript("foo") {
  mainClassName = "path.to.FooKt"
}

createAdditionalScript("bar") {
  mainClassName = "path.to.BarKt"
}
通过
buildSrc可重用性
函数
createAdditionalScript
最好放在
buildSrc
或plugin中,以保持主脚本干净,为此,它看起来如下:

createAdditionalScript("foo") {
  mainClassName = "path.to.FooKt"
}

createAdditionalScript("bar") {
  mainClassName = "path.to.BarKt"
}
import org.gradle.api.Project
导入org.gradle.api.file.DuplicatesStrategy
导入org.gradle.api.plugins.JavaApplication
导入org.gradle.api.tasks.application.CreateStartScripts
导入org.gradle.kotlin.dsl.configure
导入org.gradle.kotlin.dsl.register
导入java.io.xml文件
有趣的项目。附加脚本(名称:字符串,配置StartScripts:CreateStartScripts.(->单位)=
任务。注册(“startScripts$name”){
配置StartScripts()
applicationName=name
outputDir=文件(buildDir,“脚本”)
classpath=tasks.getByName(“jar”).outputs.files+configurations.getByName(“runtimeClasspath”)
}.还有{
配置{
applicationDistribution.into(“bin”){
来自(it)
fileMode=0b000\u 111\u 101\u 101
duplicatesStrategy=duplicatesStrategy.EXCLUDE
}
}
}

注意
fileMode
使用二进制权限表示来设置用户、组和其他用户的r、w和x位。Kotlin DSL不像Groovy那样解释octal那样解释
0755
之类的值。

向复制任务添加
DuplicateStrategy='exclude'
将消除
bin/
目录中的重复脚本。CreateStartScripts的死链接。现在就在这里: