简体   繁体   English

gradle构建之后,依赖关系应该怎么办?

[英]What is supposed to happen to dependencies after gradle build?

I am trying out Gradle, and am wondering, what is supposed to happen to a project's dependencies after you run gradle build ? 我正在尝试Gradle,并且想知道,在运行gradle build之后,项目的依赖项应该如何处理? For example, my sample projects don't run on the command line after they are built, because they are missing dependencies. 例如,我的示例项目在构建后不会在命令行上运行,因为它们缺少依赖项。 They seem to compile fine, as gradle doesn't give me errors or warnings about finding the dependencies. 它们似乎编译良好,因为gradle不会给我有关查找依赖项的错误或警告。

Gradle projects I've made in IntelliJ Idea have the same problem. 我在IntelliJ Idea中所做的Gradle项目也存在相同的问题。 They compile and run inside the IDE, but are missing dependencies and can't run on the command line. 它们可以在IDE内编译和运行,但是缺少依赖项,因此不能在命令行上运行。

So what is supposed to happen to the dependencies I declare in the build.gradle file? 那么我在build.gradle文件中声明的依赖关系应该怎么办? Shouldn't they be output somewhere together with my .class files? 它们不应该与我的.class文件一起输出吗? Otherwise, what is the point of gradle when I could manage this by editing my classpath? 否则,当我可以通过编辑类路径进行管理时,gradle有什么意义?

Edit: Here is my build.gradle file: 编辑:这是我的build.gradle文件:

apply plugin: 'java'

jar {
    manifest {
        attributes('Main-Class': 'Animals')
    }
}

repositories {
    flatDir{
        dirs "D:\\libs\\gradleRepo"
    }
}

dependencies {
    compile name: "AnimalTypes-1.0-SNAPSHOT"
}

sourceSets{
    main{
        java {
            srcDirs=['src']
        }
    }
}

Your Gradle build only takes care of the compile time and allows you to use the specified dependencies in your code (it adds them to the compile classpath). 您的Gradle构建只需要考虑编译时间,并允许您在代码中使用指定的依赖项(它将它们添加到编译类路径中)。 But it does not take care of the runtime. 但这并不会影响运行时。 Once the JAR is build, you need to specify the runtime classpath and provide all required dependencies. 构建JAR后,您需要指定运行时类路径并提供所有必需的依赖项。

You may think, that this is bad or a disadvantage, but actually it is totally fine and intended, because if you build a Java library, you won't need to execute it, you just want to specify it as a dependency for another project. 您可能会认为这是不好的或不利的,但是实际上它是完全可以实现的,因为如果您构建Java库,则无需执行它,只需将其指定为另一个项目的依赖项即可。 。 If you would distribute your library to a Maven repository, all dependencies from Maven repositories (module dependencies) would end up in a POM descriptor as transitive dependencies. 如果将库分发到Maven存储库,则来自Maven存储库的所有依赖项(模块依赖项)都将最终成为POM描述符中的传递依赖项。

Now, if you want to build a runnable Java application, simply use the Gradle Application Plugin ( apply plugin: 'application' ), which will create a ZIP file containing the dependencies and start scripts providing your runtime classpath for execution. 现在,如果要构建可运行的Java应用程序,只需使用Gradle Application插件( apply plugin: 'application' ),它将创建一个包含依赖项的ZIP文件,并提供提供执行时运行时类路径的启动脚本。

Third-party plugins can also produce so-called fat JARs, which are JAR files with all dependencies included. 第三方插件还可以生成所谓的胖JAR,这是包含所有依赖项的JAR文件。 It depends on your use case if you should use them, because often dependency management via repositories is the better way to go. 是否应该使用它们取决于您的用例,因为通常通过存储库进行依赖项管理是更好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM