Groovy development environment setup

1. Groovy development environment construction

1.1. A brief introduction of Groovy

1.1.1. What is Groovy

The website http://www.groovy-lang.org/

Groovy is an agile dynamic development language based on the JVM (Java Virtual Machine). It is a full-fledged object-oriented programming language that can be used for both object-oriented programming and pure scripting.

Groovy is a dynamic language that, like Java, runs in the Java Virtual Machine. When the Groovy script is run it is first compiled into Java class bytecode, which is then executed through the JVM virtual machine.

Groovy and Java can call each other and combine programming very well. For example, if you forget the syntax of Groovy, you can continue to write directly according to the syntax of Java, or you can call Groovy scripts from Java. Groovy’s syntax is much more flexible and concise than Java’s, and you can achieve the same functionality in Java with much less code.

1.1.2. Goovy advantage

2) No semicolons at the end of lines 3) Groovy Beans 4)Goovy syntax sugar

1.1.3.Groovy vs. the Java language

A typical example of Hello World written in Java looks like this:

public class HelloWorld { public static void main(String[] args) { System.out.println("java:Hello World"); }}

Writing a Hello World program in Groovy is as simple as this:

println "Hello World!"

Groovy supports loose Java syntax, for example, without the need for printing “Hello World!” Such simple operations define the class. Groovy allows you to type println instead of System.out.println.

1.2. Install and configure the Groovy SDK

1.2.1. Install the Groovy SDK

Groovy is Java-dependent, so you need to configure the JDK first. Next, download Groovy, which is available at http://groovy-lang.org/downlo… There are two formats:.exe is the installation version,.zip is the compressed file version, I downloaded apache-groovy-sdk-3.0.8.zip is the compressed file version, unzip its compressed file to any directory, you can see the following file path. Bin: This is where the utility commands are stored, and where the common command groovy is. Lib: This directory contains Groovy’s various utility JARs. Licenses: Various authorization documents associated with the Groovy project. When the download is completed, unzip it to the specified directory.

1.2.2. Configure environment variables

Configure the Groovy environment variable: The bin Path to add the bin directory to the system environment variable PATH is

D: \ dev_program \ groovy - 3.0.8 \ bin

Add process is brief, add after the completion of the test

groovy -v

1.3. The eclipse groovy

1.3.1. Eclipse installs the Groovy plugin

Help ->About Eclipse to check your Eclipse version:



Step 2: Enterhttps://github.com/groovy/gro…Find the corresponding version of Groovy for your Eclipse



Step 3: Copy the download addresshttps://dist.springsource.org…

Open Eclipse, select help-> Install New Software… :



Step 6: Restart Eclipse after the installation is complete

1.3.2. The groovy eclipse configuration

Windows–>Preferences–>Groovy–>Comiler

1.3.3. Eclipse uses Maven to develop Groovy HelloWorld

Create project groovy-se-helloword pom.xml

The < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > The < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Litongjava < / groupId > < artifactId > groovy - se - helloword < / artifactId > < version > 1.0 < / version > < properties > < project. Build. SourceEncoding > utf-8 < / project. Build. SourceEncoding > < Java version > 1.8 < / Java version > < maven.com piler. Source > ${Java version} < / maven.com piler source > <maven.compiler.target>${java.version}</maven.compiler.target> </properties> <build> <plugins> <plugin> < artifactId > maven -- the compiler plugin < / artifactId > < version > 3.1 < / version > < configuration > <compilerId>groovy-eclipse-compiler</compilerId> </configuration> <dependencies> <dependency> < the groupId > org. Codehaus. Groovy < / groupId > < artifactId > groovy eclipse - compiler < / artifactId > < version > 2.9.1-01 < / version > </dependency> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-eclipse-batch</artifactId> </version> 3.0.8-01</version> </ Dependencies > </plugins> </build> < Dependencies > <! -- https://mvnrepository.com/artifact/org.codehaus.groovy/groovy-all --> <dependency> < the groupId > org. Codehaus. Groovy < / groupId > < artifactId > groovy -all < / artifactId > < version > 3.0.8 < / version > < type > pom < / type > </dependency> </dependencies> </project>

HelloGroovy.groovy

/** * @author create by ping-e-lee on July 15, 2021 2:46:04 AM * @version 1.0 * @desc */ class Hellogroovy {static void main(args) { println "Hello World!" }}

Right-click Main –>Run As–>Java Application

The running results are shown in the figure



Take a look at the compiled class file

1.4. Project open source address

github

https://github.com/litongjava…

gitee

https://gitee.com/litongjava_…

2. Groovy syntax

https://www.jianshu.com/p/e8d…

3. Common mistakes

Groovy: Compiler mismatch project levelis :2.2 Workspace levelis 2.5

Reason: The Java compiler level does not match the project version. Correction: Right-click on the current project, property Properties, and the compiled version of the Groovy Compiler configuration is the same as the Java Compiler version.

4. Reference links

[1] Groovy development environment set up (for beginners) https://blog.csdn.net/cnds123… [2] in eclipse install groovy plugin https://www.cnblogs.com/testi…