How to run a Gradle task with different parameters

断了今生、忘了曾经 提交于 2019-12-02 04:35:09

问题


I'd like to define a task in gradle (called gen) that runs the gradle task jar but with a fix value for baseName. I also want the original task jar to be available afterwards.

My Problem is, that I can't transfer the setting for manifest.

I tired e.g.

def mainFile = 'com.so.proj.App'
def filename = 'something'

jar {
    baseName filename
    manifest {
        attributes 'Main-Class': mainFile
    }
}

task gen (type: Jar) {
    description "Generates JAR without version number."
    doFirst {
        //archiveName = jar.baseName + "." + extension
        archiveName = filename + ".jar"
        manifest {
            attributes 'Main-Class': mainFile
        }
    }
}

I thought I'm redefining the Jar task by using other values for archiveName and manifest.

When running ./gradlew jar an executable JAR file is generated.

When running ./gradlew gen a jar file is generated. Unfortunately when trying to run the program using java -jar build/libs/something.jar I get the error message:

java -jar build/libs/something.jar

Error: Could not find or load main class com.so.proj.App

What am I doing wrong? I simply want to run the jar task with different parameters (without configuring the jar task itself, but running an alias). And what is the code doing that I wrote (I don't get an error when running the task. But what is it doing?)


回答1:


I don't think you need doFirst in your gen task. You need to add a with jar to include all regular jar contents, which results in:

task gen (type: Jar) {
    description "Generates JAR without version number."
    archiveName = filename + ".jar"
    manifest {attributes 'Main-Class': mainFile}
    with jar
}


来源:https://stackoverflow.com/questions/35878939/how-to-run-a-gradle-task-with-different-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!