mvn.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>local.my</groupId>
<artifactId>demo_jdk</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.version>2.2.0</kotlin.version>
<groovy.version>4.0.24</groovy.version>
<gmavenplus.version>4.0.1</gmavenplus.version>
<lombok.version>1.18.38</lombok.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>3.10.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala3-library_3</artifactId>
<version>3.7.2</version>
</dependency>
<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${groovy.version}</version>
<type>pom</type>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy-ginq</artifactId>
<version>${groovy.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>4.9.5</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>${gmavenplus.version}</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>
<configuration>
<sources>
<source>
<directory>${project.basedir}/src/main/groovy</directory>
<includes>
<include>**/*.groovy</include>
</includes>
</source>
<source>
<directory>${project.basedir}/src/main/java</directory>
<includes>
<include>**/*.groovy</include>
</includes>
</source>
</sources>
<testSources>
<source>
<directory>${project.basedir}/src/test/groovy</directory>
<includes>
<include>**/*.groovy</include>
</includes>
</source>
<source>
<directory>${project.basedir}/src/test/java</directory>
<includes>
<include>**/*.groovy</include>
</includes>
</source>
</testSources>
</configuration>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>${maven.compiler.target}</jvmTarget>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<excludes>
<exclude>logs/**</exclude>
<exclude>**/*.log</exclude>
</excludes>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy</artifactId>
<version>${groovy.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy</artifactId>
<version>${groovy.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.3</version>
</plugin>
</plugins>
</build>
</project>
测试代码
Kotlin和Groovy,Kotlin和和Scala的结合都不太好
groovy
package local.my.t
class GroovyTest {
static void fn(Object obj){
println "GroovyTest fn by ${obj.class.getSimpleName()}"
}
@org.junit.jupiter.api.Test
void test() {
JavaTest.fn this
ScalaTest.fn this
//错误用法:`KotlinTest.Companion`被groovy识别为一个类
//KotlinTest.Companion.fn(this)
//正确用法通过['Companion']获取半生类单例对象
KotlinTest['Companion'].fn(this)
println "GroovyTest ok"
}
}
java
package local.my.t;
public class JavaTest {
static public void fn(Object obj){
System.out.println("JavaTest fn by "+obj.getClass().getSimpleName());
}
@org.junit.jupiter.api.Test
public void test1(){
KotlinTest.Companion.fn(this);
GroovyTest.fn(this);
ScalaTest.fn(this);
System.out.println("JavaTest ok");
}
}
kotlin
package local.my.t
class KotlinTest {
@org.junit.jupiter.api.Test
fun test() {
JavaTest.fn(this)
GroovyTest.fn(this)
ScalaTest.fn(this)
println("KotlinTest ok")
}
companion object{
fun fn(obj: Any){
println("KotlinTest fn by ${obj.javaClass.simpleName}")
}
}
}
scala
package local.my.t
class ScalaTest {
@org.junit.jupiter.api.Test
def test(): Unit = {
JavaTest.fn(this)
GroovyTest.fn(this)
// 需要使用`Companion`获取单例对象
KotlinTest.`Companion`.fn(this)
println("ScalaTest ok")
}
}
object ScalaTest {
def fn(obj: Object): Unit = {
println(s"ScalaTest fn by ${obj.getClass.getSimpleName}")
}
}
mvn test测试结果
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running local.my.t.GroovyTest
JavaTest fn by GroovyTest
ScalaTest fn by GroovyTest
KotlinTest fn by GroovyTest
GroovyTest ok
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.244 s -- in local.my.t.GroovyTest
[INFO] Running local.my.t.KotlinTest
JavaTest fn by KotlinTest
GroovyTest fn by KotlinTest
ScalaTest fn by KotlinTest
KotlinTest ok
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.012 s -- in local.my.t.KotlinTest
[INFO] Running local.my.t.ScalaTest
JavaTest fn by ScalaTest
GroovyTest fn by ScalaTest
KotlinTest fn by ScalaTest
ScalaTest ok
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 s -- in local.my.t.ScalaTest
[INFO] Running local.my.t.JavaTest
KotlinTest fn by JavaTest
GroovyTest fn by JavaTest
ScalaTest fn by JavaTest
JavaTest ok
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 s -- in local.my.t.JavaTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS