简体   繁体   English

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

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

I'm trying to pass an argument from command line to a java class but unable get the values我正在尝试将参数从命令行传递给 java class 但无法获取值

build.gradle contains 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"));
    }
}

Recieved output is null收到 output 是null

i am passing the arguments via command line:我正在通过命令行传递 arguments:

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

How to get the these values如何获得这些值

Thanks in advancce预先感谢

Add this in your build.gradle file将此添加到您的 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")
}

And execute like this并像这样执行

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

And the output is: output 是:

Recieved ip is: 127.0.0.1收到的 ip 是:127.0.0.1

Recieved port is: 4433收到的端口是:4433

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

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