有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

gradle不导入本地java库

我有以下文件夹结构:

-垃圾箱

-建造/建造。格雷德尔(格雷德尔剧本)

-lib/[*.jar](项目正在使用的库)

-src/folder/folder/[*.java](项目源代码)

构建的以下内容。渐变脚本:

plugins {
    id 'java'
    id 'groovy'
}

buildDir = new File('../bin') 
sourceCompatibility = JavaVersion.VERSION_1_8 

sourceSets { 
    main { 
        java { 
            allJava.srcDirs = [ '../src/folder/folder/ '] 
            compileClasspath = fileTree('../bin') 
        } 
    } 
} 

repositories {
    flatDir {
        dirs '../lib'
    }
}

dependencies {
    implementation fileTree('../lib')
}

tasks.register('javac', JavaCompile) { 
    println 'Call javac' 
    source.forEach { e -> println e} 

    classpath = sourceSets.main.compileClasspath 
    destinationDirectory = file('../bin') 
    source sourceSets.main.allJava.srcDirs 
    includes.add('*.java') 
    sourceCompatibility = JavaVersion.VERSION_1_8 
}

运行gradle javac 时,我得到了错误: error: cannot find symbol import com...

文件中明确指出:

dependencies {
.
.
.
    //putting all jars from 'libs' onto compile classpath
    implementation fileTree('libs')
}

我用的是Gradle 7.3.1


共 (1) 个答案

  1. # 1 楼答案

    请允许我先给你一些一般性的建议。 我强烈建议使用Kotlin DSL而不是Groovy DSL。 您可以立即在构建脚本中获得强类型代码,并获得更好的IDE支持

    也应该考虑更改项目布局,以更像其他大多数java项目,尤其是不要使用^ {}目录,而是在存储依赖关系的存储库中使用正常依赖关系,然后自动处理传递依赖关系。p>

    但为了回答您的实际问题,这是您想要的Groovy DSL中的完整构建脚本:

    plugins {
        id 'java'
    }
    
    buildDir = '../bin'
    
    java {
        toolchain {
            languageVersion.set(JavaLanguageVersion.of(8))
        }
    }
    
    sourceSets {
        main {
            java {
                srcDirs = ['../src/folder/folder']
            }
        }
    }
    
    dependencies {
        implementation fileTree('../lib')
    }
    

    这是匹配的Kotlin DSL版本:

    plugins {
        java
    }
    
    setBuildDir("../bin")
    
    java {
        toolchain {
            languageVersion.set(JavaLanguageVersion.of(8))
        }
    }
    
    sourceSets {
        main {
            java {
                setSrcDirs(listOf("../src/folder/folder"))
            }
        }
    }
    
    dependencies {
        implementation(fileTree("../lib"))
    }