头图

Groovy + Java hybrid programming solution: GMaven

Hava a look

Look at the effect first. Create a random one in the project SayHiUtil.groovy , and then add an execution print in main

 package com.cmb.lr20.zxb.dialog.utils

class SayHIUtil {

    static def sayHi() {
        println "Hi groovy"
    }
}
 public class DialogApplication {

    public static void main(String[] args) {
        SayHIUtil.sayHi();
        SpringApplication.run(DialogApplication.class, args);
    }
}

image.png

DO IT!

To use GMavenPlus , just add a plugin and dependency:

 <dependencies>
        <!-- Groovy -->
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy</artifactId>
            <version>3.0.9</version>
        </dependency>
    </dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.gmavenplus</groupId>
            <artifactId>gmavenplus-plugin</artifactId>
            <version>1.13.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>addSources</goal>
                        <goal>addTestSources</goal>
                        <goal>generateStubs</goal>
                        <goal>compile</goal>
                        <goal>generateTestStubs</goal>
                        <goal>compileTests</goal>
                        <goal>removeStubs</goal>
                        <goal>removeTestStubs</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <!-- if including source jars, use the no-fork goals
                     otherwise both the Groovy sources and Java stub sources
                     will get included in your jar -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <!-- source plugin \> = 2.1 is required to use the no-fork goals -->
            <version>3.2.1</version>
            <executions>
                <execution>
                    <id>attach-sources</id>
                    <goals>
                        <goal>jar-no-fork</goal>
                        <goal>test-jar-no-fork</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

If you encounter compilation problems, it should be a problem with the source directory. There are two workarounds:

  • Put the groovy file under src/main/groovy
  • Customize the source directory in the plugin, see the wiki of GMavenPlugin for details
    image.png

Shell

image.png

image.png


骚铭科技
1 声望2 粉丝

条路自己行,扑街唔好喊