簡體   English   中英

將 kotlin 項目從 pom.xml 轉換為 build.gradle

[英]Converting a kotlin project from pom.xml to build.gradle

我試圖將我的 kotlin 項目從 pom.xml 轉換為 build.gradle,但沒有成功。 我有一個工作 pom.xml,當我用“mvn compile”編譯時,它運行良好並運行測試。 試圖構建一個新的 build.gradle 來與 gradle 一起工作,但沒有成功。 pom.xml:

    <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
             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>com.mycompany.app</groupId>
        <artifactId>my-app</artifactId>
        <version>1.0-SNAPSHOT</version>

        <properties>
            <maven.compiler.source>1.7</maven.compiler.source>
            <maven.compiler.target>1.7</maven.compiler.target>
            <maven.compiler.release>11</maven.compiler.release>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <avro.version>1.9.1</avro.version>
            <kotlin.version>1.3.61</kotlin.version>
        </properties>

        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.0.0-M3</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.avro</groupId>
                    <artifactId>avro-maven-plugin</artifactId>
                    <version>${avro.version}</version>
                    <executions>
                        <execution>
                            <id>schemas</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>schema</goal>
                                <goal>protocol</goal>
                                <goal>idl-protocol</goal>
                            </goals>
                            <configuration>
                                <sourceDirectory>${project.basedir}/src/test/java/avro/</sourceDirectory>
                                <outputDirectory>${project.basedir}/src/test/java/</outputDirectory>
                                <stringType>String</stringType>
                            </configuration>
                        </execution>
                    </executions>
                </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>1.8</jvmTarget>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <executions>
                        <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>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                    <configuration>
                        <filesets>
                            <fileset>
                                <directory>${basedir}/src/test/java/org/mashov/generated/</directory>
                            </fileset>
                        </filesets>
                    </configuration>
                </plugin>
            </plugins>
        </build>

        <dependencies>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-api</artifactId>
                <version>5.0.0-M5</version>
            </dependency>
            <dependency>
                <groupId>com.github.dozermapper</groupId>
                <artifactId>dozer-core</artifactId>
                <version>6.5.0</version>
            </dependency>
            <dependency>
                <groupId>org.apache.avro</groupId>
                <artifactId>avro</artifactId>
                <version>${avro.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>
            </dependency>
            <dependency>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-reflect</artifactId>
                <version>1.3.61</version>
            </dependency>
            <dependency>
                <groupId>io.github.microutils</groupId>
                <artifactId>kotlin-logging</artifactId>
                <version>1.7.7</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-jdk14</artifactId>
                <version>1.7.25</version>
            </dependency>
        </dependencies>
    </project>

新的 build.gradle:

import com.commercehub.gradle.plugin.avro.GenerateAvroJavaTask

group = 'com.mycompany.app'
version = '1.0-SNAPSHOT'

buildscript {
    ext.kotlin_version = '1.3.61'

    repositories {
        jcenter()
    }
    dependencies {
        classpath "com.commercehub.gradle.plugin:gradle-avro-plugin:0.17.0"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.junit.platform:junit-platform-gradle-plugin:1.2.0"
    }
}

apply plugin: "org.junit.platform.gradle.plugin"
apply plugin: "kotlin"
apply plugin: "java"
apply plugin: "maven-publish"
apply plugin: "com.commercehub.gradle.plugin.avro"

task generateAvro(type: GenerateAvroJavaTask) {
    source("src/test/java/avro/")
    outputDir = file("src/test/java/")
}

compileJava.source(generateAvro.outputs)

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

repositories {
    mavenCentral()
    mavenLocal()
    jcenter()
}

dependencies {
    // JAX-B dependencies for JDK 9+
    implementation "jakarta.xml.bind:jakarta.xml.bind-api:2.3.2"
    implementation "org.glassfish.jaxb:jaxb-runtime:2.3.2"

//    implementation 'org.junit.jupiter:junit-jupiter-api:5.0.0-M5'
    implementation 'com.github.dozermapper:dozer-core:6.5.0'
    implementation 'org.apache.avro:avro:1.9.1'
    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.61'
    //implementation 'org.jetbrains.kotlin:kotlin-test:1.3.61'
    implementation 'org.jetbrains.kotlin:kotlin-reflect:1.3.61'
    implementation 'io.github.microutils:kotlin-logging:1.7.7'
    implementation 'org.slf4j:slf4j-jdk14:1.7.25'
    testImplementation 'io.kotlintest:kotlintest-runner-junit5:3.0.2'
    testImplementation 'org.jetbrains.kotlin:kotlin-test:1.3.61'
    //testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
    //testImplementation ('org.junit.jupiter:junit-jupiter-params:5.6.0')
    //testImplementation ('org.mockito:mockito-junit-jupiter:3.2.4')
    testImplementation("org.junit.platform:junit-platform-launcher:1.6.0")
    testRuntimeOnly ('org.junit.jupiter:junit-jupiter-engine:5.6.0')

    test.useJUnitPlatform() // fix "test events not received" bug in IDEA

}

test {
    useJUnitPlatform()
    testLogging {
        events("passed", "skipped", "failed")
    }
}

sourceCompatibility = '1.8'

publishing {
    publications {
        maven(MavenPublication) {
            from(components.java)
        }
    }
}

我究竟做錯了什么? 如何通過使用 gradle 運行測試來構建項目?

  1. 導航到 pom 文件目錄
  2. 運行gradle init這會將 Maven 構建轉換為 Gradle 構建,生成一個settings.gradle
  3. 應出現此消息: Found a Maven build. Generate a Gradle build from this? (default: yes) [yes, no] Found a Maven build. Generate a Gradle build from this? (default: yes) [yes, no]
  4. 按是

嘗過你的 pom 文件

------------------------------------------------------------
Gradle 5.6.2
------------------------------------------------------------

Build time:   2019-09-05 16:13:54 UTC
Revision:     55a5e53d855db8fc7b0e494412fc624051a8e781

Kotlin:       1.3.41
Groovy:       2.5.4
Ant:          Apache Ant(TM) version 1.9.14 compiled on March 12 2019
JVM:          11.0.1 (Oracle Corporation 11.0.1+13)
OS:           Mac OS X 10.15.3 x86_64

Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-24T21:41:47+03:00)
Maven home: /Users/naortedgi/.sdkman/candidates/maven/current
Java version: 11.0.1, vendor: Oracle Corporation, runtime: /Users/naortedgi/.sdkman/candidates/java/11.0.1-open
Default locale: en_IL, platform encoding: UTF-8
OS name: "mac os x", version: "10.15.3", arch: "x86_64", family: "mac"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM