English 中文(简体)
毕业任务——向 Java申请提出论据
原标题:Gradle task - pass arguments to Java application

我有一份 Java申请,涉及一项习俗梯度任务,申请在被援引时需要一些论点。 它们是:

programName ( string | -f filename | -d key | -h)
Options:
    string         Message to be used.
    -d key         Use default messages, key must be s[hort], m[edium] or l[ong].
    -f filename    Use specified file as input.
    -h             Help dialog.

逐步完成的任务如下:

task run (type: JavaExec){
    description = "Secure algorythm testing"
    main =  main.Test 
    classpath = sourceSets.main.runtimeClasspath
}

I ve 尝试运行gradle-h,但并不奏效。

最佳回答

自格拉斯4.9以来,指挥线论点可以与——甚至连通过。 例如,如果你希望以指挥线理由启动申请(<条码>foo-bar),你可以使用。

梯度运行——禁忌

https://docs.gradle.org/ 当前/userguide/application_plugin.html” rel=“noreferer”>。

• 如何升级

问题回答

如果你想在任何时候都使用同样的一套论点,那么你们都需要:

run {
    args = ["--myarg1", "--myarg2"]
}

我的回答类似于@xlm:

task run (type: JavaExec, dependsOn: classes){
    if(project.hasProperty( myargs )){
        args(myargs.split( , ))
    }
    description = "Secure algorithm testing"
    main = "main.Test"
    classpath = sourceSets.main.runtimeClasspath
}

例如:

gradle run -Pmyargs=-d,s

利用科特林 DSL:

plugins {
    java
    application
}

application {
    mainClass = "io.fouad.AppLauncher"
    applicationDefaultJvmArgs = listOf("-Dsome.system.properties=123")
}

tasks.withType(JavaExec::class) {
    args = listOf("abc", "def")
}

我在试图将春布特人军的参数纳入我从指挥线提出的申请时就发现了这一点。 (一百万分之7.6)

首先,在您的build.gradle档案中:

if (project.hasProperty("args")) {
    ext.cmdargs = project.getProperty("args")
} else { 
    ext.cmdargs = ""
}

随后,在build.gradle文档中:

task runDemo(dependsOn:  jar ) {
    doLast {
        javaexec {
            classpath = sourceSets.main.runtimeClasspath
            jvmArgs = [ "-Xmx8G" ]
            mainClass = "my.demo.app.App"
            args cmdargs.split()
        }

    }
}

接着,为了在巴什指挥线上开展“白血”活动,你做了以下工作:

$ ./gradlew runDemo -Pargs= arg1 -arg2 --arg3 
--spring.datasource.username=myusername
--spring.datasource.password=mypassword
--spring.datasource.driver-class-name=some.sql.Driver 

一些说明:

  1. -P creates a property, args in this case, that is passed to gradle.
  2. The args property value is enclosed in single quotes.
  3. Because I m using bash and my args property value spanned multiple lines, I didn t have to do anything special because of how bash interprets text contained within single quotes. Specifically, you do not need a backslash on the end of each line to continue on the next line within the text bounded by single quotes.
  4. The build.gradle file reads the args property and puts the value in ext.cmdargs. Then, the runDemo task calls cmdargs.split() and puts the resulting array into the args variable that is passed into you application.




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签