Home > Enterprise >  Gradle set archiveFile of a custom jar
Gradle set archiveFile of a custom jar

Time:11-09

Given a simple structure:

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

I am using the newest gradle version (7.2).

I try to build a jar with the content of folder.


Test 1

With following build.gradle file, defining a custom jar task:

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

When I run ./gradlew customJar I get this error:

> 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.

Test 2

When I update the task in the build.gradle file to:

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

I get this result:

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


Other posts indicate to set baseName or archiveBaseName. This does not help.

CodePudding user response:

archiveFile is a computed value which requires setting the destinationDirectory and baseName. This will work:

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'
}

  • Related