Git Product home page Git Product logo

gradle-plus-docker's Introduction

This repo shows how to build Gradle projects with Docker, while taking advantage of things such as build caching and a relatively new Docker feature, Multi-stage builds.

The Dockerfile in this repo does the following, in this order:

  • Creates a build container from the Alpine Linux-based Gradle Docker image.
  • Sets up a build directory.
  • Changes the Gradle home directory so that docker build caches dependencies until the project's Gradle files change.
  • Copies in the project's Gradle scripts separately of the source code so that dependencies aren't redownloaded for every single source file change.
  • Downloads the project's dependencies.
  • Copies the source in and builds the project.

After the project is built:

  • A new container is created specifically to host it, based off OpenJDK's Alpine-based JRE 8 image.
  • A new user is created inside the container so that the project doesn't run as root.
    • Even though Docker does a pretty good job at isolating containers, it's probably a good idea to refrain from using root privileges unless necessary, just in case the worst happens and someone pulls out a nasty kernel exploit.
  • A small launcher script (run.sh) is copied in. This script simply adds all the project's dependencies to the classpath at runtime and runs the project.
  • The build output from container #1 is copied in, dependencies and all.
  • run.sh is set as the launch command.

The output container is now built and ready to go.

In order for this to work, a few modifications to build.gradle needed to be made.

  • A new task ("bundleWithDependencies") has been added to copy the project and dependency JARs to a given output directory.
  • A jar section has been added so that the project is built into a JAR file. The output file name in this section needs to match the file name of the JAR in run.sh.
    • The main class attribute in this section also needs to be set to the main class of the project
  • An empty task has been added. This exists solely to make Gradle download dependencies and exit afterwards, much like npm install or similar.

(Note: even though the project in this repo is written in Kotlin, the same concept works for Java projects.)

gradle-plus-docker's People

Contributors

0x41111111 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.