gpt4 book ai didi

gradle - Intellij 和 gradle 依赖问题

转载 作者:行者123 更新时间:2023-12-03 05:43:14 24 4
gpt4 key购买 nike

今天,我在一个使用 Intellij、Java 8 和 Gradle 从头开始​​的小项目中遇到了一个真正的问题。我对 Gradle 很陌生并且学到了很多东西,但我目前被阻止了一个似乎很简单的问题:

我将我的项目分成几个模块,比如有一个主模块,其中有几个模块,如下所示:

Project overview

它可以使用 gradle clean build 构建:

13:54:26: Executing task 'build'...

:obd2-common:compileJava
:obd2-common:processResources NO-SOURCE
:obd2-common:classes
:obd2-common:jar
:obd2-bt:compileJava
:obd2-bt:processResources
:obd2-bt:classes
:obd2-bt:jar
:obd2-net:compileJava NO-SOURCE
:obd2-net:processResources NO-SOURCE
:obd2-net:classes UP-TO-DATE
:obd2-net:jar
:obd2-simulator:compileJava
:obd2-simulator:processResources
:obd2-simulator:classes
:obd2-simulator:jar
:obd2-polling:compileJava
:obd2-polling:processResources
:obd2-polling:classes
:obd2-polling:jar
:compileJava NO-SOURCE
:processResources NO-SOURCE
:classes UP-TO-DATE
:jar
:assemble
:compileTestJava NO-SOURCE
:processTestResources NO-SOURCE
:testClasses UP-TO-DATE
:test NO-SOURCE
:check UP-TO-DATE
:build
:obd2-bt:assemble
:obd2-bt:compileTestJava NO-SOURCE
:obd2-bt:processTestResources NO-SOURCE
:obd2-bt:testClasses UP-TO-DATE
:obd2-bt:test NO-SOURCE
:obd2-bt:check UP-TO-DATE
:obd2-bt:build
:obd2-common:assemble
:obd2-common:compileTestJava NO-SOURCE
:obd2-common:processTestResources NO-SOURCE
:obd2-common:testClasses UP-TO-DATE
:obd2-common:test NO-SOURCE
:obd2-common:check UP-TO-DATE
:obd2-common:build
:obd2-net:assemble
:obd2-net:compileTestJava NO-SOURCE
:obd2-net:processTestResources NO-SOURCE
:obd2-net:testClasses UP-TO-DATE
:obd2-net:test NO-SOURCE
:obd2-net:check UP-TO-DATE
:obd2-net:build
:obd2-polling:assemble
:obd2-polling:compileTestJava NO-SOURCE
:obd2-polling:processTestResources NO-SOURCE
:obd2-polling:testClasses UP-TO-DATE
:obd2-polling:test NO-SOURCE
:obd2-polling:check UP-TO-DATE
:obd2-polling:build
:obd2-simulator:assemble
:obd2-simulator:compileTestJava NO-SOURCE
:obd2-simulator:processTestResources NO-SOURCE
:obd2-simulator:testClasses UP-TO-DATE
:obd2-simulator:test NO-SOURCE
:obd2-simulator:check UP-TO-DATE
:obd2-simulator:build

BUILD SUCCESSFUL in 0s
13 actionable tasks: 13 executed
13:54:27: Task execution finished 'build'.

但是由于一个未知但令人讨厌的原因,IntelliJ 不想在其中一个模块中运行 main。此类包含对外部类的引用(来自另一个子模块)。代码中的 IntelliJ 说没问题,构建时 Gradle 说没问题,但是当我尝试运行代码并且 IntelliJ 构建时,它失败了。

以下是不同的 build.gradle:
  • 主模块(“obd2”):

  • 构建.gradle:
    plugins {

    id 'java'
    id 'idea'
    }

    group 'tech.necko.obd2'
    version '1.0-SNAPSHOT'

    sourceCompatibility = 1.8

    repositories {
    mavenCentral()
    }

    dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    }

    task zip(type: Zip) {
    from configurations.runtime.allArtifacts.files
    from configurations.runtime
    into 'release'
    }
  • obd2轮询

  • 构建.gradle:
    plugins {
    id 'java'
    id 'idea'
    }

    group 'tech.necko.obd2'
    version '1.0-SNAPSHOT'

    sourceCompatibility = 1.8

    repositories {
    mavenCentral()
    jcenter()
    }

    dependencies {
    compile 'com.opencsv:opencsv:4.2'
    compile project(':obd2-common')
    compile project(':obd2-simulator')
    compile project(':obd2-bt')
    compile project(':obd2-net')
    testCompile group: 'junit', name: 'junit', version: '4.12'
    }

    jar {
    manifest {
    attributes 'Main-Class' : 'tech.necko.obd2.Poller'
    }
    }
  • obd2-模拟器:

  • 构建.gradle:
    plugins {
    id 'java'
    id 'idea'
    }

    group 'tech.necko.obd2'
    version '1.0-SNAPSHOT'

    sourceCompatibility = 1.8

    repositories {
    mavenCentral()
    jcenter()
    }

    dependencies {
    compile project(':obd2-common')
    compile 'com.opencsv:opencsv:4.2'
    testCompile group: 'junit', name: 'junit', version: '4.12'
    }
  • obd2-bt

  • 构建.gradle:
    plugins {
    id 'java'
    id 'idea'
    }

    group 'tech.necko.obd2'
    version '1.0-SNAPSHOT'

    sourceCompatibility = 1.8

    repositories {
    mavenCentral()
    }

    dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile project(':obd2-common')
    }
  • obd2-net

  • 构建.gradle:
    plugins {
    id 'java'
    id 'idea'
    }

    group 'tech.necko.obd2'
    version '1.0-SNAPSHOT'

    sourceCompatibility = 1.8

    repositories {
    mavenCentral()
    }

    dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    }
  • obd2-常见

  • 构建.gradle:
    plugins {
    id 'java'
    id 'idea'
    }

    group 'tech.necko.obd2'
    version '1.0-SNAPSHOT'

    sourceCompatibility = 1.8

    repositories {
    mavenCentral()
    jcenter()
    }

    dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    }

    我不知道这是否是一个标准的 gradle 功能,但它将我的每个模块分成两个子模块:主模块和测试。我在 intelliJ 中检查了项目属性,它反射(reflect)了构建文件中的内容......

    谢谢 !

    最佳答案

    经过一番挣扎,尝试了很多解决方案,我发现我必须使用 IntelliJ 的 builder 重建整个项目:

    Build in IntelliJ

    关于gradle - Intellij 和 gradle 依赖问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51498200/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com