Run Gradle build offline in a docker container - gradle

I am trying to run gradle build test on my Kotlin app in a docker container which does not have access to the internet hence I copy Gradle in Dockerfile
ADD https://services.gradle.org/distributions/gradle-${GRADLE_VERSION}-bin.zip /usr/bin/gradle.zip
but the problem is that my app has got the following plugin in the app.
plugins {
kotlin("jvm") version "1.3.40"
}
so how would I go about to add these plugins to my container before running gradle offline in container?

You should put all your GRADLE_USER_HOME into the container as well. It contains Gradle wrapper distributions, plugins and dependencies. By default, GRADLE_USER_HOME is ~/.gradle directory.
I suppose, what you can do is run your full build once in a fresh container with Internets access and then just copy /root/.gradle (or whatever your user is configured to be inside the container) from it. Or just use that existing container as a base for your build image.

Related

How to build Quarkus container based on profile with Gradle?

8.1
I'm able to build my container image with Jib running: ./gradlew clean build -Dquarkus.container-image.build=true
And then I can run it with docker run
The container built is running using the production profile.
I have a separate dev/staging container environment, where I can actually deploy and obviously the configs for passwords and domains are different.
Is there a way to specify the profile during the container build, so for example when it runs it uses the dev profile configurations?
This did the trick:
./gradlew clean build -Dquarkus.container-image.build=true -Dquarkus.profile=dev

Build and run gradle project on another machine

I have created a project using gradle and have created a gradle wrapper. How can I make sure that the project builds and runs on machine who don't have gradle installed?
If you created the project using gradle (i.e. gradle init), then gradle should have placed a gradlew, or gradlew.bat file in the root directory of your project. Use those files to run gradle on another machine.
gradlew can be used on *nix machines, while gradlew.bat is used on windows.
In any case, gradle also creates a gradle/wrapper which contains the gradle jar, and this can be run on any system which has java installed.
See: Difference between using gradlew and gradle

How to manage custom common libraries built in maven for my Application in kubernetes

I need to understand how to deal with common libraries that i have created which my Application depends upon. When i create jar for this app using maven it creates a package. But how do we maintain or configure other common libraries which are listed in pom.xml of this application?
should we have maven also as an image in the docker file?
Please explain in detail.
My current progress is explained below: I have an Application A which has other dependencies like B and C libraries which i have specified in pom.xml. When i run application A in my local system it uses local repository that i have configured in user settings for maven. SO it works fine.
So how do we maintain this in kubenetes.
The images that you use to build the containers that run on Kubernetes should contain everything that's needed to run the application.
When you create the JAR file for your application, this JAR should contain the dependencies. There are different ways to achieve this, using both Maven or Gradle. This is an example using Maven and its Apache Maven Assembly Plugin. This is another good user guide on how to achieve it.
Then, you need to create a container image that can run that JAR file, something like
FROM openjdk:8-jdk-alpine
EXPOSE 8080
WORKDIR /opt/app
CMD ["java", "-jar", "app.jar"]
COPY build/libs/app.jar /opt/app
Once this container image is published on a registry, whenever Kubernetes needs to schedule and create a container, it will just use that image: there is no need to re-compile the application and its dependencies.

maven plugin for docker (e.g. to run with needed db)

Making an image for maven project should be straightforward, as maven know how to build (and can know how to run)
How to build docker image and run it with maven?
Let's say the app also needs MongoDB, that I can run as docker run -p 27017:27017 mongo. Is it possible also to specify with some maven plugin?
The maven plugin created by fabric8 allows you to do this:
the plugin and its documentation is available on github: https://github.com/fabric8io/docker-maven-plugin
The samples include for example https://github.com/fabric8io/docker-maven-plugin/blob/master/samples/data-jolokia-demo/pom.xml (which seems to be similar to what you plan).
An alternative could be using Docker-compose and some scripts outside maven, once the images are created.

Build Java Gradle project within a Docker container?

We have a Java project built with Gradle, and it requires Java 8 etc. Developers have different Java versions on their machines (different projects, IDEA has always been picky about Open JDK, but we use it to run our apps) and we would like to easily be able to build with the correct Java version.
The idea is to use Docker for the task, i.e. start a docker container with the correct Java version and use it for the build (compile, test, assemble etc.).
Is this a good idea? How would I go about it? For example, do I tell Gradle to start the container, and tell it to use its javac? Or do I start the container, mount a volume with the project code, and build using Gradle in that container? This would have the disadvantage that people need to start a container first, but I guess it could be scripted.
Does anybody do something like this?
If the goal is to standardise the build, then you should use a build automatisation software:
Jenkins
TeamCity
Bamboo
...
Also, if your gradle project must be run with a specific version, you may enforce it: How do I tell Gradle to use specific JDK version?
I wouldn't use docker for that.
I used a gradle container to build my springboot java project. You can do something similar.
Dockerfile
FROM gradle:jdk8
COPY . /home/gradle/project
WORKDIR /home/gradle/project
ENTRYPOINT ["gradle"]
CMD ["bootRepackage"]
You can also find information about different gradle containers at https://hub.docker.com/_/gradle/ which shows how you can mount your host directory as volume to the container and build.

Resources