0

I have a Java application that uses Maven, so the Maven POM file (pom.xml) lists all of the dependencies needed to compile and run the application.

I'd like to use groovy during my development and testing and then plug in the code into the Java files when I'm done. I haven't done this yet so it might not be feasible but if it is, that is what I'd like to try and do.

I would like to know if there is a function I can call that will read my pom.xml file and add all of the dependencies to my classpath so I can just import the packages that I need and goovy will already have them loaded (or at least know where to load them).

The groovy script might look like this:

addAllPomDependenciesToMyClasspath();

import my.code.pkg1;
import my.more.code.pkg2;


(new ClassInMyCode()).doCoolStuff();

Without this, I need to specify all my jars on the CLASSPATH and it would look something like this (I haven't yet done this because there are too many dependencies).

#!/bin/sh
groovy -cp "../lib1/target/MyJar1.jar:../lib2/target/MyJar2.jar:$HOME/.m2/repository/com/google/guava/guava/30.1.1-jre/guava-30.1.1-jre.jar: KEEP LISTING THESE UNTIL ALL DEPENDENCIES ARE INCLUDED"

Perhaps a better solution would be just to create a gradle file. Let me know if that is the preferred way (if there is no way to autoload all of the maven dependencies).

What have I tried

I've tried searching for solutions and found several ways to add your dependencies to the class path but they don't answer my question.

Some results were:

I'm guessing there is a "groovy" way to do this that approaches the problem differently and that is why I haven't found an answer to this question.

Misc Notes

(Q) How can I add multiple jar files to my groovy classpath? (A) Use "-cp jar1;jar2" (in Windows use semi-colons to separate the names of the jar files, and in Unix use colons to separate the filenames. See https://stackoverflow.com/a/219801/3281336

PatS
  • 8,833
  • 12
  • 57
  • 100
  • 1
    You may consider using Grab for fetching your dependencies. You won't be able to reuse the definition of deps from the pom, but it's very easy to use http://docs.groovy-lang.org/latest/html/documentation/grape.html – polypiel Apr 02 '21 at 18:13
  • Would it be possible for you to do the other way around - add Groovy to pom.xml and have scripts as part of the project, thus having all its dependencies? – Yevgen Apr 02 '21 at 18:22
  • I thought about that as a work around/another approach, and I'll definitely consider that approach. Prior to asking the question I found this https://www.baeldung.com/groovy-java-applications which explains how to add groovy to your existing application. Good suggestion. – PatS Apr 02 '21 at 18:26
  • 1
    why not to use maven itself to start groovy? https://groovy.github.io/gmaven/groovy-maven-plugin/execute.html – daggett Apr 05 '21 at 12:30

0 Answers0