Offline maven build

Hi guys, I’m trying to make an application with gradle and grails. My problem is that I would like to make the project available offline, but if I comment out the maven { url “https://repo.grails.org/grails/core” } line I get a build error, but if the line is left uncommented it works. I have a local maven repository and I believe everything is cached there already, so how could I make it work wothout the online repository?

Thanks.

Do you have mavenLocal() configured?

repositories {
   mavenLocal()
}

Yes I, have that line, but should I do something else as well?
and I have C:/Programs/maven-repository in my settings.xml

Perhaps you need

maven {
   url uri('C:/Programs/maven-repository')
}

There’s also the --offline flag at the command line to use the gradle cache
There’s also this gist which can store project dependencies on the local file system

Thanks, I have tried out your fix but I got a lot of error messages, which I have included
The error messages:
FAILURE: Build failed with an exception.

> * What went wrong:
> A problem occurred configuring root project 'offline'.
> > Could not resolve all dependencies for configuration ':runtime'.
>    > Could not resolve org.springframework.boot:spring-boot-starter-logging:.
>      Required by:
>          offline:offline:0.1
>       > Cannot change dependencies of configuration 'detachedConfiguration1' after it has been resolved.
>    > Could not resolve org.springframework.boot:spring-boot-autoconfigure:.
>      Required by:
>          offline:offline:0.1
>       > Cannot change dependencies of configuration 'detachedConfiguration1' after it has been resolved.
>    > Could not resolve org.grails:grails-core:.
>      Required by:
>          offline:offline:0.1
>       > Cannot change dependencies of configuration 'detachedConfiguration1' after it has been resolved.
>    > Could not resolve org.springframework.boot:spring-boot-starter-actuator:.
>      Required by:
>          offline:offline:0.1
>       > Cannot change dependencies of configuration 'detachedConfiguration1' after it has been resolved.
>    > Could not resolve org.springframework.boot:spring-boot-starter-tomcat:.
>      Required by:
>          offline:offline:0.1
>       > Cannot change dependencies of configuration 'detachedConfiguration1' after it has been resolved.
>    > Could not resolve org.grails:grails-dependencies:.
>      Required by:
>          offline:offline:0.1
>       > Cannot change dependencies of configuration 'detachedConfiguration1' after it has been resolved.
>    > Could not resolve org.grails:grails-web-boot:.
>      Required by:
>          offline:offline:0.1
>       > Cannot change dependencies of configuration 'detachedConfiguration1' after it has been resolved.
>    > Could not resolve org.grails.plugins:cache:.
>      Required by:
>          offline:offline:0.1
>       > Cannot change dependencies of configuration 'detachedConfiguration1' after it has been resolved.
>    > Could not resolve org.grails.plugins:scaffolding:.
>      Required by:
>          offline:offline:0.1
>       > Cannot change dependencies of configuration 'detachedConfiguration1' after it has been resolved.
>    > Could not resolve org.grails.plugins:hibernate4:.
>      Required by:
>          offline:offline:0.1
>       > Cannot change dependencies of configuration 'detachedConfiguration1' after it has been resolved.
>    > Could not resolve org.hibernate:hibernate-ehcache:.
>      Required by:
>          offline:offline:0.1
>       > Cannot change dependencies of configuration 'detachedConfiguration1' after it has been resolved.
>    > Could not resolve org.grails.plugins:asset-pipeline:.
>      Required by:
>          offline:offline:0.1
>       > Cannot change dependencies of configuration 'detachedConfiguration1' after it has been resolved.
>    > Could not resolve com.h2database:h2:.
>      Required by:
>          offline:offline:0.1
>       > Cannot change dependencies of configuration 'detachedConfiguration1' after it has been resolved.

So my problem is, that if I use
maven {
url “https://repo.grails.org/grails/core
//url uri(‘C:/Programs/maven-repository/’)
}
the URL it works, but if I comment it out and try to use the local version I get a lot of errors, but the grails website works, even offline because I have it cached on the system.

Have you tried

  1. Running a normal build (to prep gradle’s cache)
  2. Running a second build passing --offline at command line?

Thank I have tried it out it really works if I use the --offline parameter, but the problem is that it is using the cache, but the project I am working on requires that I can put the libraries under configuration management, so I have to make sure that, the project doesn’t use the cache, only the available maven-repository (in my case under C:\Programs).
So is there a way I can make the project to run only from the local repository without the cache?

You could use this gist to backup your dependencies and then reference them via

maven {
   url uri('c:/path/to/offline/dependencies')
}

Yes, I have tried the gist, but it doesn’t copy everything because when I try to run it, I get errors saying that I don’t have a specific files at the path. So could it be that the gist doesn’t backup all the dependecies?

Please post the errors and relevant snippet from build.gradle, perhaps @bmuschko can help?

I have uploaded the build.gradle gist
Maybe the problem is that I didn’t include all the necessary dependencies into the gradle script.

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'offline'.
> Could not resolve all dependencies for configuration ':classpath'.
   > Could not find org.grails:grails-gradle-plugin:3.1.4.
     Searched in the following locations:
         file:/C:/workspace/offline/build/offline-repo/org/grails/grails-gradle-plugin/3.1.4/grails-gradle-plugin-3.1.4.pom
         file:/C:/workspace/offline/build/offline-repo/org/grails/grails-gradle-plugin/3.1.4/grails-gradle-plugin-3.1.4.jar
     Required by:
         :offline:unspecified
   > Could not find com.bertramlabs.plugins:asset-pipeline-gradle:2.7.4.
     Searched in the following locations:
         file:/C:/workspace/offline/build/offline-repo/com/bertramlabs/plugins/asset-pipeline-gradle/2.7.4/asset-pipeline-gradle-2.7.4.pom
         file:/C:/workspace/offline/build/offline-repo/com/bertramlabs/plugins/asset-pipeline-gradle/2.7.4/asset-pipeline-gradle-2.7.4.jar
     Required by:
         :offline:unspecified
   > Could not find org.grails.plugins:hibernate4:5.0.3.
     Searched in the following locations:
         file:/C:/workspace/offline/build/offline-repo/org/grails/plugins/hibernate4/5.0.3/hibernate4-5.0.3.pom
         file:/C:/workspace/offline/build/offline-repo/org/grails/plugins/hibernate4/5.0.3/hibernate4-5.0.3.jar
     Required by:
         :offline:unspecified

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Ah… looks like you need to take a copy of the buildscript classpath configuration too.
Note that the OfflineMavenRepository accepts a configurationName parameter and looks it up via project.configurations.getByName(…). You’ll need to tweak it to accept a Configuration object instead of a string. Then you could do:

task exportRuntime(type: OfflineMavenRepository) {
   configuration = configurations.runtime
   repoDir = file('/path/to/repo')
}
task exportBulidscriptClasspath(type: OfflineMavenRepository) {
   configuration = buildscript.configurations.classpath
   repoDir = file('/path/to/repo')
}

Or perhaps

task exportBoth(type: OfflineMavenRepository) {
   configurations = [buildscript.configurations.classpath, configurations.runtime]
   repoDir = file('/path/to/repo')
}

Note: You’ll also need to declare the file path in your buildscript {} block when working offline

buildscript {
    repositories {
        maven { 
           url uri('c:/path/to/offline/dependencies')
        }
    }
}

Thanks, with the two export tasks it finally downloaded the core libraries, but it still can’t find the libs.
I have this in my buildscript: where offline-repo is the directory where the previous tasks downloaded the libraries. Should I give a different path to it?

buildscript {
ext {
grailsVersion = project.grailsVersion
}
repositories {
maven {
url uri(‘C:/workspace/offline/build/offline-repo’)
}

Are using the new or old syntax to declare org.grails:grails-gradle-plugin:3.1.4?

If you are using the “new” plugin syntax, you will have to add the following to settings.gradle

pluginRepositories {
  maven {
    url uri('C:/workspace/offline/build/offline-repo')
  }
}

If you are using the “old” plugins syntax you will need to add the following to build.gradle

buildscript {
   repositories {
      maven {
         url uri('C:/workspace/offline/build/offline-repo')
      }
   }
}

In both cases, you’ll also need a repository declaration for the normal dependencies

repositories {
   maven {
      url uri('C:/workspace/offline/build/offline-repo')
   }
}

I am using classpath “org.grails:grails-gradle-plugin:$grailsVersion” and I don’t have a settings.gradle file
https://gist.github.com/szakitom/36677a8d7ac77253174b344b2353bbd0

Thank you, I really appreciate your help, but it still doesn’t work. :frowning: I checked the files, and they are there, maybe the problem is that it is looking for the files at:

C:/workspace/offline/build/offline-repo/org/grails/grails-gradle-plugin/3.1.4/grails-gradle-plugin-3.1.4.pom

but they are actually at:

C:/workspace/offline/build/offline-repo/org.grails/grails-gradle-plugin\3.1.4/grails-gradle-plugin-3.1.4.pom

I don’t really know if it could be the problem or not.

Also if I enable the online repositories, gradle says

configurations = [configurations.testRuntime, buildscript.configurations.classpath]

line is problematic: Cannot get property ‘testRuntime’ on null object

but they are actually at: C:/workspace/offline/build/offline-repo/org.grails/grails-gradle-plugin\3.1.4/grails-gradle-plugin-3.1.4.pom

Looks like you’ll need to tweak the copyJars and copyPoms methods to replace dots with slashes in the group

eg change:

File moduleDir = new File(repoDir, "${moduleVersionId.group}/...")
to
File moduleDir = new File(repoDir, "${moduleVersionId.group.replace('.', '/')}/...")

Also if I enable the online repositories, gradle says
configurations = [configurations.testRuntime, buildscript.configurations.classpath]
line is problematic: Cannot get property ‘testRuntime’ on null object

Ah, that’s because the configurations task parameter is overriding project.configurations… try this instead:
configurations = [project.configurations.testRuntime, buildscript.configurations.classpath]

It is starting to look good :smiley: , but unfortunately I still get an error.

What went wrong:
Could not add entry ‘:offline’ to cache taskArtifacts.bin (C:\workspace\offline.gradle\2.9\taskArtifacts\taskArtifacts.bin).
> Unable to store task input properties. Property ‘configurations’ with value ‘[configuration ‘:testRuntime’, configuration ‘:classpath’]’ cannot be serialized.