Define a task to ZIP archive a set of project files in Gradle

Overview

--Define a task to ZIP archive a set of project files in Gradle --Operation check environment: Gradle 6.6 + Java 14 (AdoptOpenJDK 14.0.2)

build.gradle

plugins {
  id 'java'
}

repositories {
  mavenCentral()
}

version = '1.2.3'

dependencies {
  //Specify the library to use
  implementation 'com.google.guava:guava:29.0-jre'
  testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'
  testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.2'
}

//Define a task to ZIP archive a set of project files
task myZip(type: Zip) {

  //Directory to output ZIP file
  destinationDirectory = file('..') //Keep it one level higher

  //File name to archive
  archiveFileName =
    'myapp-' +
    archiveVersion.get() + '-' +
    new Date().format('yyyyMMdd_HHmmss_SSS') + '.zip'

  //Project directory
  from '.' //Current directory

  //Gradle generated file storage(build directory)Or
  // .Don't include hidden directories such as gradle in your ZIP file
  excludes = ['build', '.*']
}

Operation check

It is assumed that not only the source code but also text files and image files are included in the project set.

$ find .
.
./foodir
./foodir/foo.jpg
./foodir/foo.png
./foo.txt
./build.gradle
./src
./src/test
./src/test/resources
./src/test/resources/mytest.properties
./src/test/java
./src/test/java/myapp
./src/test/java/myapp/AppTest.java
./src/main
./src/main/resources
./src/main/resources/myapp.properties
./src/main/java
./src/main/java/myapp
./src/main/java/myapp/App.java

Execute the defined task myZip.

$ gradle myZip

BUILD SUCCESSFUL in 651ms
1 actionable task: 1 executed

Check the contents of the ZIP file.

$ unzip -l ../myapp-1.2.3-20200816_091726_068.zip 
Archive:  ../myapp-1.2.3-20200816_091726_068.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  08-16-2020 07:45   foodir/
    14526  08-16-2020 09:16   foodir/foo.jpg
    18136  10-18-2019 18:50   foodir/foo.png
       84  08-16-2020 09:14   foo.txt
      998  08-16-2020 08:51   build.gradle
        0  08-16-2020 06:59   src/
        0  08-16-2020 06:59   src/test/
        0  08-16-2020 09:16   src/test/resources/
        9  08-16-2020 09:17   src/test/resources/mytest.properties
        0  08-16-2020 06:59   src/test/java/
        0  08-16-2020 06:59   src/test/java/myapp/
      357  08-16-2020 06:59   src/test/java/myapp/AppTest.java
        0  08-16-2020 06:59   src/main/
        0  08-16-2020 09:16   src/main/resources/
        9  08-16-2020 09:16   src/main/resources/myapp.properties
        0  08-16-2020 06:59   src/main/java/
        0  08-16-2020 06:59   src/main/java/myapp/
      286  08-16-2020 06:59   src/main/java/myapp/App.java
---------                     -------
    34405                     18 files

Reference material

Recommended Posts

Define a task to ZIP archive a set of project files in Gradle
Set the time of LocalDateTime to a specific time
How to run a djUnit task in Ant
Java tips-Create a Spring Boot project in Gradle
Set the source of the library set as a dependency in IntelliJ as a separate module of the project
Add a project in any folder with Gradle
View the Gradle task in the Spring Boot project
How to create a Spring Boot project in IntelliJ
How to set BuildConfig of library project (module) to Debug
I want to define a function in Rails Console
[Rails 6] How to set a background image in Rails [CSS]
I made a plugin to execute jextract with Gradle task
Set up Gradle multi-project in IntelliJ to build JAR file
I tried to make a client of RESAS-API in Java
How to set up a proxy with authentication in Feign
Let's define a Rake task.
Gradle
Gradle Memorandum
Gradle installation
View the Gradle task in the Spring Boot project
Handle business logic for a set of Entity in Java class
Fix the file name of war to the one set in Maven
The milliseconds to set in /lib/calendars.properties of Java jre is UTC
Cast an array of Strings to a List of Integers in Java
Add a time stamp to the JAR file name in Gradle
How to output a list of strings in JSF as comma-separated strings
How to set an image in the drawable Left / Right of a button using an icon font (Iconics)