Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add Groovy scripts to a Java project with classical Maven structure?

I have a Java Maven project with its usual structure. It was generated by Spring Boot project creator.

Now I want to add some Groovy scripts to it, such that

  1. I can access some of the functionality implemented in Java from these scripts and
  2. it's sufficient, if I can run those Groovy scripts from the IDE only (I don't intend to package them, nor distribute them).

I'm using IntelliJ Idea Community Edition.

What do I need to do (incl. where to put the Groovy scripts) in order to write and execute a Groovy script in the IDE, which can access my Java class com.mycompany.SomeComplexClass that depends on libraries managed by Maven (encoded in pom.xml) ?

like image 392
Dmitrii Pisarenko Avatar asked Oct 30 '15 07:10

Dmitrii Pisarenko


1 Answers

Made a little repo for this case, started with Spring Boot and just added few things so the Groovy code can be run. Also, tested it with IDEA Community Edition (most recent version, using bundle Maven).

There are two ways you can achieve what you want:

  1. just add one dependency to your pom.xml dependencies section - the dependency on groovy

    <!--  added so you can run Groovy scripts from IDEA -->
    <dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-all</artifactId>
        <version>2.3.11</version>
        <scope>test</scope>
    </dependency>
    

And you should be able to create run configuration for the groovy scripts.

  1. If you want them to be seen as test source folder in IDEA and by Maven you need to add the plugin build-helper-maven-plugin to your pom.xml section.

To test this clone this repo and import to IDEA as Maven project.

like image 128
MikeMatusiak Avatar answered Oct 02 '22 19:10

MikeMatusiak