相关疑难解决方法(0)

Gradle任务 - 将参数传递给Java应用程序

我有一个使用自定义gradle任务运行的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.
Run Code Online (Sandbox Code Playgroud)

Gradle任务看起来像:

task run (type: JavaExec){
    description = "Secure algorythm testing"
    main = 'main.Test'
    classpath = sourceSets.main.runtimeClasspath
}
Run Code Online (Sandbox Code Playgroud)

我试过跑gradle run -h,但是不起作用.

java arguments gradle

105
推荐指数
6
解决办法
8万
查看次数

如何将参数或参数传递给gradle任务

我有一个gradle构建脚本,我试图包含Eric Wendelin的css插件 - http://eriwen.github.io/gradle-css-plugin/

它很容易实现,因为我只想缩小(而不是组合和gzipping),我已经得到了构建脚本的相关部分,如下所示:

minifyCss {
    source = "src/main/webapp/css/brandA/styles.css"
    dest = "${buildDir}/brandA/styles.css"
    yuicompressor {
        lineBreakPos = -1
    }
}

war {
    baseName = 'ex-ren'
}

war.doFirst {
    tasks.myTask.minifyCss.execute()
}
Run Code Online (Sandbox Code Playgroud)

这很完美 - 当我运行gradle war任务时,它调用minifyCss任务,获取源css文件,并在buildDir中创建一个缩小版本

但是,我有一些css文件需要缩小,但不能合并到一个文件中(因此我没有使用combineCss任务)

我希望能够做的是使用某种类型的minifyCss任务引用变量的源和目标属性(假设这是正确的术语吗?) - 传递给签名中的任务的变量,或全局变量,或者东西......

我觉得这样的东西(这不起作用):

minifyCss(sourceFile, destFile) {
    source = sourceFile
    dest = destFile
    yuicompressor {
        lineBreakPos = -1
    }
}

war {
    baseName = 'ex-ren'
}

war.doFirst {
    tasks.myTask.minifyCss.execute("src/main/webapp/css/brandA/styles.css", "${buildDir}/brandA/styles.css")
    tasks.myTask.minifyCss.execute("src/main/webapp/css/brandB/styles.css", "${buildDir}/brandB/styles.css")
    tasks.myTask.minifyCss.execute("src/main/webapp/css/brandC/styles.css", "${buildDir}/brandC/styles.css")
}
Run Code Online (Sandbox Code Playgroud)

这也不起作用:

def sourceFile = null
def destFile = null

minifyCss …
Run Code Online (Sandbox Code Playgroud)

variables parameters groovy gradle

42
推荐指数
6
解决办法
9万
查看次数

运行gradle任务时如何在命令行中传递多个参数?

我有一个由gradle任务运行的java和groovy类.我设法让它工作,但我不喜欢我必须在命令行中传递参数的方式.以下是我目前通过命令行执行的操作:gradle runTask -Pmode"['doStuff','username','password']"
我的build.gradle代码采用这些参数,如下所示:

if (project.hasProperty("mode")) {
args Eval.me(mode)}
Run Code Online (Sandbox Code Playgroud)

然后我在我的java代码中使用我的参数/参数,如下所示:

String action = args[0]; //"doStuff"
String name = args[1]; .. //"username"
Run Code Online (Sandbox Code Playgroud)

我想知道有没有办法以更好的方式传递参数,例如:

gradle runTask -Pmode=doStuff -Puser=username -Ppass=password 
Run Code Online (Sandbox Code Playgroud)

以及如何在我的java类中使用它们.

java gradle build.gradle

14
推荐指数
1
解决办法
2万
查看次数

如何使用Gradle将参数传递给main方法?

我必须将两个参数传递给我的main方法.我的构建脚本是

// Apply the java plugin to add support for Java
apply plugin: 'java'

// In this section you declare where to find the dependencies of your project
repositories {
    // Use 'maven central' for resolving your dependencies.
    mavenCentral()
}

// In this section you declare the dependencies for your production and test code
dependencies {
    compile 'com.example:example-core:1.7.6'
}

task main(type: JavaExec, dependsOn: classes) {
    description = 'This task will start the main class of the example project'
    group = …
Run Code Online (Sandbox Code Playgroud)

java program-entry-point gradle commandargument

10
推荐指数
2
解决办法
1万
查看次数

如何将命令行参数传递给 Gradle Kotlin DSL

这是来自 Groovy 的一个示例,它准确地代表了我想要实现的目标:

Command line

./gradlew jib -PmyArg=hello
Run Code Online (Sandbox Code Playgroud)

build.gradle.kts

task myTask {
    doFirst {
       println myArg
       ... do what you want
    }
}
Run Code Online (Sandbox Code Playgroud)

此示例的来源在这里 - 选项 3

如何myArg在 Kotlin DSL 中读取传递和读取值?

groovy gradle kotlin

10
推荐指数
2
解决办法
3879
查看次数

将属性传递给自定义gradle任务

如何将属性传递给gradle自定义任务?在蚂蚁它看起来像这样:

public class MyCustomTask extends Task {

    private String name;
    private String version;

    @Override
    public void execute() throws BuildException {
        // do the job
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setVersion(String version) {
        this.version = version;
    }

}
Run Code Online (Sandbox Code Playgroud)

如何在gradle中完成?

class MyCustomTask extends DefaultTask {

    private String name
    private String version

    @TaskAction
    def build() {
        // do the job
    }

}
Run Code Online (Sandbox Code Playgroud)

属性名称和版本是必需的,用户需要将它们传递给任务.

groovy gradle

9
推荐指数
2
解决办法
1万
查看次数

9
推荐指数
2
解决办法
5521
查看次数

在Android gradle构建中,在构建期间更改strings.xml的内容

我希望能够在gradle构建期间从我们的CMS(或某些数据库)加载res/values/strings.xml中的用户可见字符串的值.

例如

<string name="button_label">OK, do it</string>
Run Code Online (Sandbox Code Playgroud)

可以改为

<string name="button_label">OK, do it now!</string>
Run Code Online (Sandbox Code Playgroud)

... 管他呢.

这个想法是新的价值将从我们的CMS读取AT BUILD TIME,然后将被纳入APK文件.(动机是自动更改应用程序字符串,以便从现有CMS中读取应用程序中的任何文本).

实现这一目标的最佳方法是什么?是否有可能在构建期间生成/修改资源文件(例如strings.xml),就在它们被Android构建系统使用之前?


添加更多要求:

android gradle android-resources

9
推荐指数
1
解决办法
2647
查看次数

IntelliJ IDEA 命令行版本运行配置

我有一个有效的 IntelliJ IDEA 运行配置。它使用 Spring Boot。

我想从 MacOS 命令行执行相同的运行。如何让 IntelliJ IDEA 显示我需要执行运行配置的命令(或多个命令)。

这是gradle build.gradle文件:

plugins {
    id 'org.springframework.boot' version '2.6.4'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'org.mountsinai'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = "15"

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
    runtimeOnly 'com.microsoft.sqlserver:mssql-jdbc'
    implementation group: 'org.springframework', name: 'spring-aspects', version: '5.3.15'
    implementation group: 'nz.net.ultraq.thymeleaf', name: 'thymeleaf-layout-dialect', version: '3.0.0'
    implementation group: 'com.jayway.jsonpath', name: 'json-path', version: '2.7.0'
    implementation group: 'com.github.pcj', …
Run Code Online (Sandbox Code Playgroud)

java macos intellij-idea gradle

3
推荐指数
1
解决办法
1582
查看次数