添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
一直单身的匕首  ·  Project cannot be ...·  2 小时前    · 
英姿勃勃的绿豆  ·  Gradle 7.3 Release Notes·  2 小时前    · 
虚心的橙子  ·  Gradle & Third-party ...·  2 小时前    · 
痴情的拖把  ·  Synchronize Gradle ...·  2 小时前    · 
成熟的刺猬  ·  Buildship plugin does ...·  2 小时前    · 
无邪的丝瓜  ·  多媒体·  11 月前    · 
  • Structuring Projects with Gradle
  • Declaring Dependencies between Subprojects
  • Sharing Build Logic between Subprojects
  • Composite Builds
  • Authoring Tasks
  • Writing Gradle Task Types
  • Configuring Tasks Lazily
  • Avoiding Unnecessary Task Configuration
  • Developing Parallel Tasks
  • Starting Plugin Development
  • Designing Plugins
  • Implementing Plugins
  • Testing Plugins
  • Publishing Plugins
  • Organizing Build Logic
  • Following Best Practices
  • Gradle managed Directories
  • Working with Files
  • Working with Logging
  • Avoiding Traps
  • Configuring the Build Environment
  • Initialization Scripts
  • Writing Custom Gradle Types and Service Injection
  • Shared Build Services
  • Dataflow Actions
  • Testing a Build with TestKit
  • Using Ant from Gradle
  • Authoring JVM Builds

  • Building Java & JVM projects
  • Testing Java & JVM projects
  • Toolchains for JVM projects
  • Toolchain Resolver Plugins
  • Managing Dependencies
  • Java Library Plugin
  • Java Application Plugin
  • Java Platform Plugin
  • Groovy Plugin
  • Scala Plugin
  • Working with Dependencies

  • Terminology
  • What is Dependency Management?
  • Declaring Repositories
  • Declaring Dependencies
  • Understanding Library and Application Differences
  • Viewing and Debugging Dependencies
  • Understanding Resolution
  • Verifying dependencies
  • Declaring Versions and Ranges
  • Declaring Rich Versions
  • Handling Changing Versions
  • Locking Versions
  • Upgrading Versions
  • Downgrading and Excluding
  • Sharing Versions
  • Aligning Dependencies
  • Handling Mutually Exclusive Dependencies
  • Fixing Metadata
  • Customizing Resolution
  • Preventing accidental upgrades
  • Declaring Capabilities of a Library
  • Modeling Feature Variants and Optional Dependencies
  • Understanding Variant Selection
  • Declaring Variant Attributes
  • Sharing Outputs of Projects
  • Transforming Artifacts
  • Setting up Publishing
  • Understanding Gradle Module Metadata
  • Signing Artifacts
  • Customizing Publishing
  • Maven Publish Plugin
  • Ivy Publish Plugin
  • Optimizing Build Performance

  • Improving Performance of Gradle Builds
  • Gradle Daemon
  • File System Watching
  • Incremental Build
  • Enabling and Configuring
  • Why use the Build Cache?
  • Understanding the Impact
  • Learning Basic Concepts
  • Caching Java Project
  • Caching Android Project
  • Debugging Caching Issues
  • Troubleshooting
  • Using the Configuration Cache
  • Inspecting Gradle Builds
  • Configuring Gradle
  • Project Properties
  • Gradle Networking
  • Authoring C++/Swift Builds

  • Building C++ projects
  • Testing C++ projects
  • Building Swift projects
  • Testing Swift projects
  • Gradle on CI

  • Jenkins
  • TeamCity
  • GitHub Actions
  • Travis CI
  • Reference

  • Javadoc
  • Groovy DSL Primer
  • Groovy DSL Reference
  • Kotlin DSL Primer
  • Kotlin DSL API
  • Groovy to Kotlin DSL Migration
  • Samples
  • Command-Line Interface
  • Gradle Wrapper
  • Core Plugins
  • Gradle & Third-party Tools
  • User Manual PDF
  • Building JVM applications
  • Building applications using the Java Module System
  • Building a distribution
  • Tasks
  • Application extension
  • License of start scripts
  • Convention properties (deprecated)
  • The Application plugin facilitates creating an executable JVM application. It makes it easy to start the application locally during development, and to package the application as a TAR and/or ZIP including operating system specific start scripts.

    Applying the Application plugin also implicitly applies the Java plugin . The main source set is effectively the “application”.

    Applying the Application plugin also implicitly applies the Distribution plugin . A main distribution is created that packages up the application, including code dependencies and generated start scripts.

    The only mandatory configuration for the plugin is the specification of the main class (i.e. entry point) of the application.

    Example 2. Configure the application main class

    You can run the application by executing the run task (type: JavaExec ). This will compile the main source set, and launch a new JVM with its classes (along with all runtime dependencies) as the classpath and using the specified main class. You can launch the application in debug mode with gradle run --debug-jvm (see JavaExec.setDebug(boolean) ).

    Since Gradle 4.9, the command line arguments can be passed with --args . For example, if you want to launch the application with command line arguments foo --bar , you can use gradle run --args="foo --bar" (see JavaExec.setArgsString(java.lang.String) .

    If your application requires a specific set of JVM settings or system properties, you can configure the applicationDefaultJvmArgs property. These JVM arguments are applied to the run task and also considered in the generated start scripts of your distribution.

    If your application’s start scripts should be in a different directory than bin , you can configure the executableDir property.

    Gradle supports the building of Java Modules as described in the corresponding section of the Java Library plugin documentation . Java modules can also be runnable and you can use the application plugin to run and package such a modular application. For this, you need to do two things in addition to what you do for a non-modular application.

    First, you need to add a module-info.java file to describe your application module. Please refer to the Java Library plugin documentation for more details on this topic.

    Second, you need to tell Gradle the name of the module you want to run in addition to the main class name like this:

    application {
        mainModule = "org.gradle.sample.app" // name defined in module-info.java
        mainClass = "org.gradle.sample.Main"
    
    application {
        mainModule = 'org.gradle.sample.app' // name defined in module-info.java
        mainClass = 'org.gradle.sample.Main'
    

    That’s all. If you run your application, by executing the run task or through a generated start script, it will run as module and respect module boundaries at runtime. For example, reflective access to an internal package from another module can fail.

    The configured main class is also baked into the module-info.class file of your application Jar. If you run the modular application directly using the java command, it is then sufficient to provide the module name.

    You can also look at a ready made example that includes a modular application as part of a multi-project.

    A distribution of the application can be created, by way of the Distribution plugin (which is automatically applied). A main distribution is created with the following content:

    Table 1. Distribution content

    Static files to be added to the distribution can be simply added to src/dist. More advanced customization can be done by configuring the CopySpec exposed by the main distribution.

    val createDocs by tasks.registering {
        val docs = layout.buildDirectory.dir("docs")
        outputs.dir(docs)
        doLast {
            docs.get().asFile.mkdirs()
            docs.get().file("readme.txt").asFile.writeText("Read me!")
    distributions {
        main {
            contents {
                from(createDocs) {
                    into("docs")
    
    tasks.register('createDocs') {
        def docs = layout.buildDirectory.dir('docs')
        outputs.dir docs
        doLast {
            docs.get().asFile.mkdirs()
            docs.get().file('readme.txt').asFile.write('Read me!')
    distributions {
        main {
            contents {
                from(createDocs) {
                    into 'docs'
    

    By specifying that the distribution should include the task’s output files (see incremental builds), Gradle knows that the task that produces the files must be invoked before the distribution can be assembled and will take care of this for you.

    You can run gradle installDist to create an image of the application in build/install/projectName. You can run gradle distZip to create a ZIP containing the distribution, gradle distTar to create an application TAR or gradle assemble to build both.

    Customizing start script generation

    The application plugin can generate Unix (suitable for Linux, macOS etc.) and Windows start scripts out of the box. The start scripts launch a JVM with the specified settings defined as part of the original build and runtime environment (e.g. JAVA_OPTS env var). The default script templates are based on the same scripts used to launch Gradle itself, that ship as part of a Gradle distribution.

    The start scripts are completely customizable. Please refer to the documentation of CreateStartScripts for more details and customization examples.

    Depends on: jar, startScripts

    Creates a full distribution ZIP archive including runtime libraries and OS specific scripts.

    distTarTar

    Depends on: jar, startScripts

    Creates a full distribution TAR archive including runtime libraries and OS specific scripts.

    The Application Plugin adds an extension to the project, which you can use to configure its behavior. See the JavaApplication DSL documentation for more information on the properties available on the extension.

    You can configure the extension via the application {} block shown earlier, for example using the following in your build script:

    This plugin also adds some convention properties to the project, which you can use to configure its behavior. These are deprecated and superseded by the extension described above. See the Project DSL documentation for information on them.

    Unlike the extension properties, these properties appear as top-level project properties in the build script. For example, to change the application name you can just add the following to your build script: