【问题标题】:Gradle set archiveFile of a custom jarGradle设置自定义jar的archiveFile
【发布时间】:2021-12-21 21:36:34
【问题描述】:

给定一个简单的结构:

.
├── build.gradle
└── folder
    ├── file.txt
    └── other.txt

我正在使用最新的 gradle 版本 (7.2)。

我尝试用文件夹的内容构建一个 jar。


测试 1

使用以下build.gradle文件,定义自定义jar任务:

task customJar(type: Jar) {
    manifest {
        attributes 'Implementation-Title': 'Gradle Jar File Example',  
                   'Main-Class': 'xxx.aaa.Bbb'
    }
    from 'folder'
}

当我运行 ./gradlew customJar 时,我收到此错误:

> Task :customJar FAILED

FAILURE: Build failed with an exception.

* What went wrong:
A problem was found with the configuration of task ':customJar' (type 'Jar').
  - Type 'org.gradle.api.tasks.bundling.Jar' property 'archiveFile' doesn't have a configured value.
    
    Reason: This property isn't marked as optional and no value has been configured.
    
    Possible solutions:
      1. Assign a value to 'archiveFile'.
      2. Mark property 'archiveFile' as optional.
    
    Please refer to https://docs.gradle.org/7.2/userguide/validation_problems.html#value_not_set for more details about this problem.

测试 2

当我将build.gradle 文件中的任务更新为:

task customJar(type: Jar) {
    manifest {
        attributes 'Implementation-Title': 'Gradle Jar File Example',  
                   'Main-Class': 'xxx.aaa.Bbb'
    }
    archiveFile = 'custom-jar'
    from 'folder'
}

我得到这个结果:

FAILURE: Build failed with an exception.

* Where:
Build file '/<path to my project>/build.gradle' line: 101

* What went wrong:
A problem occurred evaluating root project 'tmp'.
> Cannot set the value of read-only property 'archiveFile' for task ':customJar' of type org.gradle.api.tasks.bundling.Jar.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org


其他帖子表示设置baseNamearchiveBaseName。这没有帮助。

【问题讨论】:

    标签: java gradle jar


    【解决方案1】:

    archiveFile 是一个计算值,需要设置destinationDirectorybaseName。这将起作用:

    tasks.register("customJar", Jar) {
        manifest {
            attributes 'Implementation-Title': 'Gradle Jar File Example',  
                       'Main-Class': 'xxx.aaa.Bbb'
        }
        destinationDirectory = layout.buildDirectory.dir("libs")
        baseName = 'custom-jar'
        from 'folder'
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-02
      • 2012-07-13
      • 1970-01-01
      • 2021-09-10
      • 1970-01-01
      相关资源
      最近更新 更多