Make a Gradle project work without an internet connection - gradle

Given a Gradle project, that has:
Runtime/Compile-time dependencies.
Source/Javadoc Jars of dependencies.
Gradle plugins.
All of which are retrieved from the internet.
How can I download all of these online dependencies to a local folder, and then use them locally in a Gradle project?
The goal is to end up with a project that doesn't rely on an internet connection at all.

You can do a one-time download of your required dependencies to a local folder, and add a local "maven repo", besides the dependencies, it should also work for sources.
repositories {
maven {
url 'file://C:/LocalRepo'
}
}

Related

Gradle still downloads dependencies already available in local repo

I have set gradle up to use a local maven repository (gmaven_stable file) using the "offline.gradle" file method in the ".gradle\init.d" directory.
I constantly update the local maven repo with already downloaded files in the gradle dependency cache (.gradle\caches\modules-2\files-2.1).
However, I have realized that gradle still downloads some dependencies during "gradle sync" and "compile time" most of which I am already having in my local maven repo. Dependencies like "junit" , "google play services" and many others are always downloaded whenever I hit "Build" or "Run" button in Android Studio.
I would much appreciate if I would be guided to stop this behaviour in gradle. I am spending a lot of time gathering dependencies to build my local maven repo because of low and costly internet access in my area. It is quite annoying whenever that scarce internet I have access to is used by "Gradle" to re-download those dependencies I have already cached in my local maven repo.
Gradle has a built-in option that will avoid network access:
The --offline command line switch tells Gradle to always use dependency modules from the cache, regardless if they are due to be checked again. When running with offline, Gradle will never attempt to access the network to perform dependency resolution. If required modules are not present in the dependency cache, build execution will fail.
https://docs.gradle.org/current/userguide/dynamic_versions.html#sec:offline-mode
I constantly update the local maven repo with already downloaded files in the gradle dependency cache (.gradle\caches\modules-2\files-2.1).
You should not be manually messing with Gradle's internal cache folder. Instead, you place your JARs in a folder else where you can access them such as your home directory. Then, you can configure a flat directory repository:
repositories {
flatDir {
dirs("mylibs")
}
}
https://docs.gradle.org/current/userguide/declaring_repositories.html#sub:flat_dir_resolver
You will also need to download and define various Gradle plugins that are used in your project as well and configure them the same way as the application repositories:
// settings.gradle.kts
pluginManagement {
repositories {
flatDir {
dirs("mylibs")
}
}
}

Using artifactory in gradle

I want to use an artifactory in gradle. To be more specific, i want to use 4 customized jars that are not in the maven repository. So i'd like them to be at the artifactory server and will be downloaded when needed.
Do i need to install something other than "Gradle eclipse integration" ?
Can someone give me an example on how to do that in the gradle.build?
First, you need to deploy those jars to Artifactory.
Probably, using the UI will be the easiest way to go.
Next, you need to declare Artifactory as your repository. You can do it by using the standard repositories clause (as #lance-java suggested), or by using Artifactory Gradle plugin. Probably the easiest will be generating the build script snippet from Artifactory itself.
Last will be adding the dependencies to your script. You can navigate to the jars you uploaded in the tree browser, and copy the snippets of dependency declarations from there.
Both steps are documented in the User Guide as well.
I am with JFrog, the company behind Bintray and [artifactory], see my profile for details and links.
repositories {
maven {
url "http://repo.mycompany.com/maven2"
}
}
dependencies {
compile "com.foo.mycompany:dep1:1.0"
compile "com.foo.mycompany:dep2:1.0"
compile "com.foo.mycompany:dep3:1.0"
compile "com.foo.mycompany:dep4:1.0"
}

Fetch all dependencies, put them in a new local Maven repository using Gradle

I have a Gradle project with several subprojects and many, many dependencies. I would like to have a simple way to tell Gradle to download all dependencies (including those under buildscript!) and put them in a local Maven/Ivy repository for later use. The Gradle script should then be able to pull all dependencies from the local repository.
Background: I need to build the application on a server which has absolutely no access to any public Maven repositories, so all dependencies must already be present on the host. I've tried a flat directory, but I have not found it easy to resolve the transitive dependencies, and managing them by hand is not an option. Copying the Gradle cache did not work, either.
Can anyone suggest something? Thanks.
I found a solution, namely to install Apache Archive (http://archiva.apache.org/) locally and set it up as a proxy. Then I copied the entire installation to the target-server and disabled the remote repositories. The dependencies could then be fetched locally.

How to install a Maven/Gradle project along with all its dependencies to a local repository?

I have a Gradle project that depends on several open-source projects up on Maven Central. I'd like to install the project – along with all its direct and transitive dependencies – to my local maven repository, so that I could later zip it all up and put it on offline machines.
How do I do it with Gradle/Maven?
mvn dependency:get plugin will fetch the artifact with all dependencies to the local repository.
I had also developed a plugin to install remote artifacts to a local machine.
If you want to later ZIP up your project w/ dependencies and move them to a different machine, you could try Maven's appassembler plugin. It collects all dependencies and creates a launcher, all in the target folder, ready for deployment.
But note, this, by default, creates a flat directory structure with all dependencies, it doesn't preserve the Maven format. It also has the option to create a repository.

How does Gradle/Ivy read a Nexus repo acting as a Maven mirror?

How does Ivy read a Nexus repo acting as a Maven mirror?
I was thinking about using Gradle as my build system, and Gradle is built from Ant+Ivy (using Groovy). I have a Nexus repository on my local network that acts as a "mirror". In order for me to build my projects, I put a "mirror" entry in my .m2\settings.xml config file. I am able to build my Maven projects just fine but Gradle does not read the .m2 config and so my Gradle projects wont build.
I do not know how to configure Gradle to use the nexus repo as a mirror. Can anyone explain this or give me some hints? I suspect it has something to do with usage of a ivysettings.xml file maybe? This post implies that Gradle DOES in fact read the Maven config but I do not experience this.
I'm using Gradle with a Nexus repo and a Maven proxy and I did not have to modify any of those xml files. I just installed Nexus, created a user with a password through the admin UI, and added this config in my gradle init script (e.g. ${USER_HOME}/.gradle/init.gradle):
allprojects {
repositories {
// Third party dependencies are fetched from MavenCental through a Nexus proxy repository
maven {
credentials {
username 'some_username'
password 'some_password'
}
url 'http://dev.primalogik.com/nexus/content/groups/public/'
}
}
dependencies {
// Example of a compile time dependency
compile group: 'com.google.gwt', name: 'gwt-dev', version: '2.5.1'
...
}
}
The following answer describes how to configure a Maven repository manager in ivy:
Use public maven repository with ivy
I'm uncertain whether this actually helps with a Gradle build (I thought Gradle had stopped using ivy).
Update
Gradle documentation on configuring repositories:
http://www.gradle.org/docs/current/userguide/dependency_management.html#sub:maven_repo

Resources