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

  • I can access some of the functionality implemented in Java from these scripts and
  • 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 ) ?


    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:

  • 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.

  • 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.

    链接地址: http://www.djcxy.com/p/88572.html

    上一篇: 如何标记HTML列表的标题

    下一篇: 如何将Groovy脚本添加到具有经典Maven结构的Java项目中?