0

It's possible to use Grape to download a dependency from Maven central:

@Grab(group='org.apache.commons', module='commons-lang3', version='3.4')

Specifying a type of jar within the Grab annotation does the same thing:

@Grab(group='org.apache.commons', module='commons-lang3', version='3.4', type='jar')

and according to the javadoc, the value of type can also be javadoc and sources, however trying the following in IntelliJ:

@Grab(group='org.apache.commons', module='commons-lang3', version='3.4', type = 'sources')

results in:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
General error during conversion: Error grabbing Grapes -- [download failed: org.apache.commons#commons-lang3;3.4!commons-lang3.sources]

java.lang.RuntimeException: Error grabbing Grapes -- [download failed: org.apache.commons#commons-lang3;3.4!commons-lang3.sources]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83)
    at org.codehaus.groovy.reflection.CachedConstructor.doConstructorInvoke(CachedConstructor.java:77)
    at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrap.callConstructor(ConstructorSite.java:84)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:60)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:235)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:247)
    at groovy.grape.GrapeIvy.getDependencies(GrapeIvy.groovy:426)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSite.invoke(PogoMetaMethodSite.java:169)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
    at groovy.grape.GrapeIvy.resolve(GrapeIvy.groovy:573)
    at groovy.grape.GrapeIvy$resolve$1.callCurrent(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:190)
    at groovy.grape.GrapeIvy.resolve(GrapeIvy.groovy:540)
    at groovy.grape.GrapeIvy$resolve$0.callCurrent(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:182)
    at groovy.grape.GrapeIvy.grab(GrapeIvy.groovy:258)
    at groovy.grape.Grape.grab(Grape.java:167)
    at groovy.grape.GrabAnnotationTransformation.visit(GrabAnnotationTransformation.java:378)
    at org.codehaus.groovy.transform.ASTTransformationVisitor$3.call(ASTTransformationVisitor.java:321)
    at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:931)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:593)
    at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:569)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:546)
    at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
    at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
    at groovy.lang.GroovyShell.run(GroovyShell.java:517)
    at groovy.lang.GroovyShell.run(GroovyShell.java:507)
    at groovy.ui.GroovyMain.processOnce(GroovyMain.java:653)
    at groovy.ui.GroovyMain.run(GroovyMain.java:384)
    at groovy.ui.GroovyMain.process(GroovyMain.java:370)
    at groovy.ui.GroovyMain.processArgs(GroovyMain.java:129)
    at groovy.ui.GroovyMain.main(GroovyMain.java:109)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:109)
    at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:131)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Eclipse gives a similar error. So is there a a way to use Grape to download sources/javadoc when editing a groovy script in an IDE?

M. Justin
  • 14,487
  • 7
  • 91
  • 130
Malt
  • 28,965
  • 9
  • 65
  • 105
  • Have you tried using the classifier attribute instead of the type ? `@Grab(group='org.apache.commons', module='commons-lang3', version='3.4', classifier='sources')` – sensei Oct 15 '16 at 12:54
  • You can actually see an example of the `classifier` attribute being used for this following your link, at the doc for the `value` parameter. – sensei Oct 15 '16 at 12:59
  • `classifier ='javadoc' ` and `classifier ='sources'` seems to work, I see the jars in `~/.groovy/grapes/`. But Intellij doesn't seem to use them - there's no documentation or source code within the IDE. – Malt Oct 15 '16 at 16:58
  • Well, I'm no expert and this "solution" looks pretty bad to me, but it looks like Grape support in Idea is incomplete: https://youtrack.jetbrains.com/issue/IDEA-56974 . So after placing your cursor on the @Grab annotation, pressing alt-enter and grabing the artifacts manually, you could copy them to module libraries from the projet structure's settings. That seems to do it for me, also I also feel it defies the whole point of using Grape... – sensei Oct 16 '16 at 15:35
  • @sensei would you like to post this as an answer? – Malt Oct 16 '16 at 15:49

1 Answers1

1

The doc you link to also mentions the possibility of using the classifier attribute, which seems to work better (Grape will indeed grab the corresponding jars):

@Grab(group='org.apache.commons', module='commons-lang3', version='3.4', classifier='sources')

However, it looks like Grape support in Idea is incomplete. In order for Idea to use the grabbed packages, you need to place your cursor on the @Grab annotation, press alt-enter and grab the artifacts manually. You could then copy them to module libraries from the projet structure's settings. That seems to do it for me, although I also feel it defies the whole point of using Grape...

sensei
  • 621
  • 7
  • 13