Gradle Application Plugin - APP__HOME in applicationDefaultJvmArgs

Gradle Application Plugin - APP_HOME in applicationDefaultJvmArgs

In Gardle,application plugin, können Sie die Systemeigenschaften überapplicationDefaultJvmArgs übergeben:

gradle.build

    apply plugin:'application'
    mainClassName = "com.example.analyzer.engine.hydra.entryPointForJar"
    applicationName = 'analyzer'

    distZip {
        archiveName 'analyzer-' + version + '.zip'
    }

    applicationDefaultJvmArgs = ["-Dlogback.configurationFile=logback.xml"]

Das Problem ist, wie man das APP_HOME fürlogback.xml bekommt?

gradle.build

    applicationDefaultJvmArgs = ["-Dlogback.configurationFile=APP_HOME/logback.xml"]

Sie können APP_HOME hart codieren, dies funktioniert jedoch nur für eine Plattform (Windows oder * nix).

1. Lösung

Um dies zu beheben, erstellen Sie eine benutzerdefinierte Variable "MY_APP_HOME" und ersetzen Sie sie durchdoLast

gradle.build

    applicationDefaultJvmArgs = ["-Dlogback.configurationFile=MY_APP_HOME/logback.xml"]

    startScripts {
        doLast {
            unixScript.text = unixScript.text.replace('MY_APP_HOME', '\$APP_HOME')
            windowsScript.text = windowsScript.text.replace('MY_APP_HOME', '%~dp0..')
        }
    }

Note
Diese Lösung funktioniert sowohl auf Windows- als auch auf * nix-Plattformen. Getestet mit Gradle 2.0

Baue es.

gradle distZip

Ausgabe

${project}uild\distributions\${project-name}innalyzer

#!/usr/bin/env bash

##############################################################################
##
##  analyzer start up script for UN*X
##
##############################################################################

DEFAULT_JVM_OPTS='"-Dlogback.configurationFile=$APP_HOME/logback.xml"

${project}uild\distributions\${project-name}innalyzer.bat

@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem  analyzer startup script for Windows
@rem
@rem ##########################################################################

set DEFAULT_JVM_OPTS="-Dlogback.configurationFile=%~dp0../logback.xml"

2. Lösung - Benutzerdefiniertes Startskript

Dies ist für das benutzerdefinierte Startskript:

gradle.build

  task abcStartScripts(type: CreateStartScripts) {
    mainClassName = "com.example.analyzer.engine.hydra.entryPointForJar"
    classpath = startScripts.classpath
    outputDir = startScripts.outputDir
    applicationName = 'analyzer'
    defaultJvmOpts = ["-Dlogback.configurationFile=MY_APP_HOME/logback.xml"]

    abcStartScripts {
        doLast {
            unixScript.text = unixScript.text.replace('MY_APP_HOME', '\$APP_HOME')
            windowsScript.text = windowsScript.text.replace('MY_APP_HOME', '%~dp0..')
        }
    }

  }

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