53

I have a fresh installation of Groovy 2.1.4 and I'd like to create a script that uses HTTP builder.

I've added the following line at the top of the script:

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.6')

When I run the script in GroovyConsole, I get the following error:

1 compilation error:

Exception thrown
VI 01, 2013 12:15:39 ODP. org.codehaus.groovy.runtime.StackTraceUtils sanitize

WARNING: Sanitizing stacktrace:

java.lang.RuntimeException: Error grabbing Grapes -- [download failed: commons-logging#commons-logging;1.1.1!commons-logging.jar]

    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)

    ... (aso) ...

java.lang.RuntimeException: Error grabbing Grapes -- [download failed: commons-logging#commons-logging;1.1.1!commons-logging.jar]

When I run the following command on the Windows command line:

grape -V resolve org.codehaus.groovy.modules.http-builder http-builder 0.6

I get the same error, i.e.:

:: problems summary ::
:::: WARNINGS
        [NOT FOUND  ] commons-logging#commons-logging;1.1.1!commons-logging.jar (0ms)

    ==== localm2: tried

      file:C:\Documents and Settings\Administrator/.m2/repository/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar


:: USE VERBOSE OR DEBUG MESSAGE LEVEL FOR MORE DETAILS
Error in resolve:
    Error grabbing Grapes -- [download failed: commons-logging#commons-logging;1.1.1!commons-logging.jar]

When I try to resolve commons-logging directly, via the following command:

grape -V resolve commons-logging commons-logging 1.1.1

I get the same error, i.e. not found.

When I instead try to download the latest version, i.e. 1.1.3, via the appropriate command, it works. But even after that, resolving the http-client still fails.

Where's the problem?

Dušan Rychnovský
  • 11,699
  • 8
  • 41
  • 65
  • Was my answer helpful at all? – jesseplymale Aug 15 '13 at 23:51
  • Sorry, I happened to reinstall my environment before I was able to try to follow your advice and see if it would solve my problem. Everything works fine now. As your answer has been the only one for more than a month, it seems fair to accept it. – Dušan Rychnovský Aug 17 '13 at 10:24

9 Answers9

45

Try deleting ~/.m2 directory and also ~/.groovy/grapes directories.

It worked for me.

Jamie Counsell
  • 7,730
  • 6
  • 46
  • 81
Pavel Černocký
  • 573
  • 4
  • 12
  • 2
    I was actually able to get the same results simply deleting the offending directories from my ~/.groovy/grapes/ directory (xerxes and commons-lang). – jonnybot Nov 21 '14 at 17:29
  • 3
    I had to delete the `~/.m2` directory for this error to go away. Just `~/.groovy/grapes` was not enough. – Ethan Mick Nov 17 '15 at 23:42
  • 4
    Don't just delete `~/.m2`, you might lose your `settings.xml` this way! I recommend to delete `~/.m2/repository` instead. – dokaspar Sep 08 '17 at 06:22
  • 2
    More specifically, try deleting `~/.m2/repository/common-logging/` and `~/.groovy/grapes/common-logging/`. – Kyle Shrader Jan 02 '18 at 16:32
  • I tried renaming the ~/.m2/repository and ~/.groovy/grapes and put it back once my issue resolved and it worked – Vivek Jun 06 '20 at 09:59
35

It looks like your Grape settings are set only to look in your local .m2 directory for the library. Check your grapeConfig.xml configuration file. (If you don't have one, you can create it--it should go in the same directory where your groovysh.history and grapes cache directory are created by Groovy.) You can copy the example file shown on the Groovy Grape reference page.

If that doesn't help, I would try deleting your grapes cache directory and try it again.

Community
  • 1
  • 1
jesseplymale
  • 704
  • 5
  • 7
  • 3
    I'm using Groovy 2.3.2 to write a command-line app in Intellij IDEA 13 EAP. I had no ~/.groovy/grapeConfig.xml file, so I created one with the suggested content and then my `@Grab` processing in IDEA worked. Here's my grapeConfig.xml file: http://d.pr/n/rWmo – duma Nov 21 '13 at 16:31
  • I have the same issue. The trick is that I was using an older version of Groovy but trying to @Grab most recent version of JARs, that causes the download failure. – sozhen Apr 30 '14 at 06:35
  • 1
    @duma: Your `d.pr` link seems to be dead – Mark K Cowan Jul 05 '14 at 16:18
  • 1
    I had the same problem `Error grabbing Grapes -- [download failed: jakarta-regexp#jakarta-regexp;1.4!jakarta-regexp.jar]` To fix it, I had to configure _grapeConfig.xml_ in my _homeFolder/.groovy_ I got the file from here: https://gist.github.com/lalyos/9366690 – razvang Nov 24 '16 at 12:41
  • 1
    In my case on Windows 7, the solution was to use the recent ```grapeConfig.xml```, from the provided reference page and add `file:` as a protocol in the `root` parameter of the `localm2` resolver, before "`${user.home.url}`". It was there before, but got [removed later](https://github.com/apache/groovy/commit/412edb87e78a62de9b0e4f6de09416cee0475cae#diff-07228b96d60a1ab78f57e5825fecd217L29). – imy Nov 21 '19 at 11:42
18

My problem was, groovy (v2.4.8) was looking in the maven repository fist (~/.m2/repository/) and finding the pom file but not finding the associated artifact/JAR. Instead of just moving on to the next resolver, which would have succeeded, it just gives up. The workaround would be to remove the pom file, the specific directory cache, or just temporarily rename the repository and run groovy again as other suggested. Or you could try to manually add it to the repository. But these are just temporary and you'll likely run into the issue again if you clear your groovy cache or with another dependency.

To troubleshoot this issue you can turn on verbose logging and try to manually install the dependency. So if your error is something like:

java.lang.RuntimeException: Error grabbing Grapes -- [download failed: commons-logging#commons-logging;1.1.1!commons-logging.jar, download failed: commons-codec#commons-codec;1.6!commons-codec.jar, download failed: commons-lang#commons-lang;2.4!commons-lang.jar]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)

Which means your dependency is commons-logging-1.1.1.jar. You can run:

grape -V install commons-logging commons-logging 1.1.1

What ended up solving the problem for me was overriding the default configuration and setting usepoms="false" in the localm2 resolver. It works in my case because I have the pom but not the JAR, so since Ivy isn't considering the POM now and the JAR was never there to begin with, it goes onto the next resolver which does find it. So in summary:

  • Create this file: ~/.groovy/grapeConfig.xml
  • With these contents:

<ivysettings>
  <settings defaultResolver="downloadGrapes"/>
  <resolvers>
 <chain name="downloadGrapes" returnFirst="true">
   <filesystem name="cachedGrapes">
  <ivy pattern="${user.home}/.groovy/grapes/[organisation]/[module]/ivy-[revision].xml"/>
  <artifact pattern="${user.home}/.groovy/grapes/[organisation]/[module]/[type]s/[artifact]-[revision](-[classifier]).[ext]"/>
   </filesystem>
   <ibiblio name="localm2" root="file:${user.home}/.m2test/repository/" checkmodified="true" changingPattern=".*" changingMatcher="regexp" m2compatible="true" usepoms="false"/>
   <ibiblio name="jcenter" root="https://jcenter.bintray.com/" m2compatible="true"/>
   <ibiblio name="ibiblio" m2compatible="true"/>
 </chain>
  </resolvers>
</ivysettings>

I was tempted not to use the maven 2 cache at all, but if I removed the line, I got errors about not being able to find "localm2". Although updating it to point to fictitious directory worked.

Mike
  • 791
  • 8
  • 18
  • Amazing solution, thanks. Note: This will work without using a directory like `.m2test` used in the snippet above, you can use `.m2`. – Daniel Jul 06 '17 at 23:31
  • That unfortunately did not solve the problem for me, with either `.m2test` or `.m2`. I have the same Groovy version 2.4.7. Deleting the folders worked, but a solution without folder deletion would definitely be better. – Nicolas Raoul Aug 23 '17 at 07:10
  • FYI removing temporarily the `m2` line in fact worked for me on groovy 2.4.12 – akostadinov Oct 24 '17 at 19:53
  • 1
    This was the only thing that worked. Would be nice if this was somehow a default. – Nathan Dunn Jun 21 '18 at 02:17
1

Deleting .m2 and grapes did not resolve the issue for me as when they were repopulated, the jar was not being downloaded. In my case, I was missing xml-apis-1.3.04.jar

I eventually resolved the issue by manually downloading the jar file and copying it into ~/.m2/repository/xml-apis/xml-apis/<version>

Hopefully this is useful if simply deleting the directories isn't working.

Trebla
  • 1,164
  • 1
  • 13
  • 28
  • Very useful if you do not want to delete the .m2 directory (it takes a lot of time to download all the dependencies again for all your other maven builds). – joanlofe Mar 21 '17 at 16:46
1

this might help someone down the line as i faced similar issue in windows

java.lang.RuntimeException: Error grabbing Grapes -- [download failed: commons-collections#commons-collections;3.2.2!commons-collections.jar]

the required jar above was not being downloaded in C:/Users/%USER%/.groovy/grapes/common-collections/jars/ folder.
so manually downloaded the required version from https://jar-download.com/
and added in the corresponding folder

Abhishek D K
  • 2,257
  • 20
  • 28
0

Not quite the same case as the question, adding it in case someone finds himself in a similar case with me where I did not have any direct access on the machine to check the grapeConfig.xml configuration file, as suggested by the accepted answer. What worked for me was to configure a grab resolver.

I did something similar with what is stated in the documentation and it worked (documentation link):

@GrabResolver(name='restlet', root='http://maven.restlet.org/')
@Grab(group='org.restlet', module='org.restlet', version='1.1.6')
Dragos Geornoiu
  • 527
  • 2
  • 8
  • 17
0

I solved a similar issue by using a newer version of commons-codec:commons-codec in groovy script using the @Grab annotation:

@Grapes([
  @Grab('org.slf4j:slf4j-simple:1.7.25'),
  @Grab('commons-codec:commons-codec:1.14'),
  @Grab('io.github.http-builder-ng:http-builder-ng-apache:1.0.4')
])
Ibrahim.H
  • 1,062
  • 1
  • 13
  • 22
0

you need create ~/.groovy/grapeConfig.xml coments

<ivysettings>
  <settings defaultResolver="downloadGrapes"/>
  <resolvers>
    <chain name="downloadGrapes" returnFirst="true">
      <filesystem name="cachedGrapes">
        <ivy pattern="${user.home}/.groovy/grapes/[organisation]/[module]/ivy-[revision].xml"/>
        <artifact pattern="${user.home}/.groovy/grapes/[organisation]/[module]/[type]s/[artifact]-[revision](-[classifier]).[ext]"/>
      </filesystem>
      <ibiblio name="localm2" root="file:${user.home}/.m2test/repository/" checkmodified="true" changingPattern=".*" changingMatcher="regexp" m2compatible="true" usepoms="false"/>
      <ibiblio name="jcenter" root="https://jcenter.bintray.com/" m2compatible="true"/>
      <ibiblio name="ibiblio" m2compatible="true"/>
    </chain>
  </resolvers>
</ivysettings>
lucky
  • 1
-1

I had a similar error when using Groovy + Java 7. The error from groovy was:

General error during conversion: Error grabbing Grapes -- [unresolved dependency: com.microsoft.sqlserver#mssql-jdbc;6.4.0.jre7: not found]

When trying the

grape -V resolve com.microsoft.sqlserver mssql-jdbc 6.4.0.jre7

command i had these errors:

  1. java.net.SocketException: Connection reset
  2. javax.net.ssl.SSLException: Received fatal alert: protocol_version

The way to fix this was adding the -Dhttps.protocols=TLSv1.2 parameter:

grape -Dhttps.protocols=TLSv1.2 -V resolve com.microsoft.sqlserver mssql-jdbc 6.4.0.jre7

Then the package is downloaded and i can use it from Groovy

Paco Zarate
  • 1,925
  • 1
  • 15
  • 13