簡體   English   中英

如何在 build.gradle 中獲取 System.getproperty value.java 文件

[英]How to get System.getproperty value .java file in build.gradle

我正在嘗試將參數從命令行傳遞給 java class 但無法獲取值

build.gradle包含

plugins {
    id 'java'
}

group 'test'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

test {
    useJUnitPlatform()
    testLogging {
        events "passed", "skipped", "failed"
    }
}
tasks.withType(JavaCompile) {
    options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
    options.deprecation = true
}
test {
    systemProperty "host", System.getProperty("host")
    systemProperty "port", System.getProperty("port")
}

dependencies {
    testImplementation(platform('org.junit:junit-bom:5.7.1'))
    testImplementation('org.junit.jupiter:junit-jupiter')
    implementation group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1'
    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.13.3'
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.13.3'
    compile fileTree(include: ['*.jar'], dir: 'lib')
    implementation group: 'org.json', name: 'json', version: '20210307'
}

src/main/test/test.java

public class test {
    public static void main(String[] args) {
        System.out.println("Recieved ip is:"+System.getProperty("host"));
        System.out.println("Recieved port is:"+System.getProperty("port"));
    }
}

收到 output 是null

我正在通過命令行傳遞 arguments:

./gradlew clean build test -Dhost=127.0.0.1 -Dport=4433

如何獲得這些值

預先感謝

將此添加到您的 build.gradle 文件中

task serverArgs(type: JavaExec) {
    group = "Execution"
    description = "Run the main class with ServerArgs"
    classpath = sourceSets.main.runtimeClasspath
    main = "you main class path" //test.test
    systemProperty "host", System.getProperty("host")
    systemProperty "port", System.getProperty("port")
}

並像這樣執行

./gradlew clean serverArgs build test -Dhost=127.0.0.1 -Dport=4433

output 是:

收到的 ip 是:127.0.0.1

收到的端口是:4433

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM