java 为什么Gradle构建即使没有所需的依赖也能成功?

e3bfsja2  于 2023-06-20  发布在  Java
关注(0)|答案(1)|浏览(143)

我没有build.gradle文件,因此也没有指定依赖项。
不知何故,gradle成功地构建了一个包含此文件的项目,该文件包含Apache Commons依赖项:

package demo;

import org.apache.commons.math3.analysis.interpolation.InterpolatingMicrosphere;
import org.apache.commons.math3.random.UnitSphereRandomVectorGenerator;

public class Library {
    public Library() {
        var d = new InterpolatingMicrosphere(3, 0, 0d, 0d, 0d, new UnitSphereRandomVectorGenerator(3));
    }
}

BUILD SUCCESSFUL in 809ms
如果我故意引入一个错误来检查文件是否正在编译:

package demo;

import org.apache.commons.math3.analysis.interpolation.InterpolatingMicrosphere;
import org.apache.commons.math3.random.UnitSphereRandomVectorGenerator;

public class Library {
    public Library() {
        var d = new InterpolatingMicrosphere(3, 0, 0d, 0d, 0d, new UnitSphereRandomVectorGenerator(3));
        groudebarnismeasicalopmenstear
    }
}

现在我得到了gradle build错误

> Task :lib:compileJava FAILED
lib/src/main/java/demo/Library.java:9: error: not a statement
        groudebarnismeasicalopmenstear
        ^
lib/src/main/java/demo/Library.java:9: error: ';' expected
        groudebarnismeasicalopmenstear
                                      ^

即使我没有在任何地方指定依赖项,gradle如何成功构建第一个代码块?我已经尝试过gradle cleangradle --refresh-dependencies很多次了。
如何复制:

$ mkdir demo
$ cd demo
$ gradle init

Select type of project to generate:
  1: basic
  2: application
  3: library
  4: Gradle plugin
Enter selection (default: basic) [1..4] 3

Select implementation language:
  1: C++
  2: Groovy
  3: Java
  4: Kotlin
  5: Scala
  6: Swift
Enter selection (default: Java) [1..6] 

Select build script DSL:
  1: Groovy
  2: Kotlin
Enter selection (default: Groovy) [1..2] 

Select test framework:
  1: JUnit 4
  2: TestNG
  3: Spock
  4: JUnit Jupiter
Enter selection (default: JUnit Jupiter) [1..4] 

Project name (default: demo): 
Source package (default: demo): 

Enter target version of Java (min. 7) (default: 20): 
Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no] 

> Task :init
Get more help with your project: https://docs.gradle.org/8.1.1/samples/sample_building_java_libraries.html

BUILD SUCCESSFUL in 10s
2 actionable tasks: 2 executed
$ rm lib/src/test/java/demo/LibraryTest.java
$ echo "package demo;

import org.apache.commons.math3.analysis.interpolation.InterpolatingMicrosphere;
import org.apache.commons.math3.random.UnitSphereRandomVectorGenerator;

public class Library {
    public Library() {
        var d = new InterpolatingMicrosphere(3, 0, 0d, 0d, 0d, new UnitSphereRandomVectorGenerator(3));
    }
}" > lib/src/main/java/demo/Library.java
$ gradle build

BUILD SUCCESSFUL in 814ms
2 actionable tasks: 2 executed
vqlkdk9b

vqlkdk9b1#

build.gradle文件实际上位于lib中,它不是顶级文件。
gradle init- library 生成的build.gradle恰好指定了您正在使用的apache commons数学依赖项。

相关问题