to assemble the project

simply using ./gradlew assemble or ./gradlew assembleDist
about ./gradlew assemble: Assembles the outputs of this project
about ./gradlew assembleDist: Assembles the main distributions(can be customized)

customize the distribution

sometimes we want to add some config files into our project, using log4j.properties for example

using relative path

  • step 1

LogTest.java using the relative path:

PropertyConfigurator.configure("myResource/log4j.properties");

to config the log4j.properties.

  • step 2

In build.gradle

add application plugin

apply plugin: 'application'

add distribution plugin

apply plugin: 'distribution'

add mainClass

mainClassName = 'LogTest'

customize distribution

distributions {
    main {
        baseName = 'Log4jTest'
        contents {
            from(new File("myResource")) {
                into "bin/myResource"
            }
        }
    }
}

remember using from(new File("myResource")) instead of from("myResource")

  • step 3

using ./gradlew assembleDist to assemble the distribution.

we can see the .zip and .tar in build/distribution:

  • step 4

unzip Log4jTest-1.0-SNAPSHOT.zip

using ./Log4jTest to run

using absolute path with JvmArgs

  • setp 1

set JvmArgs in debugRun task

  • step 2

LogClass.java using System.getProperty to get the JvmArgs:

  • step 3

using ./gradlew debugRun to run the mainClass set in debugRun(main = "LogClass")

  • step 4

In build.gradle

add application plugin

apply plugin: 'application'

add distribution plugin

apply plugin: 'distribution'

add mainClass

mainClassName = 'LogClass'

customize distribution

// pass the system properties via applicationDefaultJvmArgs
applicationDefaultJvmArgs = [
        "-DLog4j.file=MY_APP_HOME/myResource/log4j.properties"
]

// Creates start scripts for launching JVM applications
startScripts {
    doLast {
        unixScript.text = unixScript.text.replace('MY_APP_HOME', '\$APP_HOME')
        windowsScript.text = windowsScript.text.replace('MY_APP_HOME', '%~dp0..')

    }
}
// add myResource folder into distributions
distributions {
    main {
        baseName = 'Log4jClass'
        contents {
            from(new File("myResource")) {
                into "myResource"
            }
        }
    }
}
  • step 5

using ./gradlew assembleDist to assemble the distribution.

we can see the .zip and .tar in build/distribution:

  • step 6

unzip Log4jClass-1.0-SNAPSHOT.zip

using ./LogClass to run

using installDist task added by application plugin

you can use ./gradlew installDist to assemble the uncompressed distribution into “$buildDir/install”.

  • step 1

LogTest.java using the relative path:

PropertyConfigurator.configure("myResource/log4j.properties");

to config the log4j.properties.

  • step 2

In build.gradle

add application plugin

apply plugin: 'application'

add mainClass

mainClassName = 'LogTest'
  • step 3

add a task depends on installDist

//copy log4j.properties to bin/src/main/resources
task installPack(type: Copy, dependsOn: [clean, installDist]) {
    from('myResource/log4j.properties')
        into "$buildDir/install/$rootProject.name/bin/myResource"
}
  • step 4

using ./gradlew installPack to run installPack task, so you can create an image of the application in build/install/projectName

  • step 5

using ./Log4jTest to run

some tips

  • All of the files in the “src/$distribution.name/dist” directory will automatically be included in the distribution. (the dist file can be under src or src/main)

So you can put readme file under dist, and it will automatically be included in the distribution.

it will automatically be included both with ./gradlew assembleDist and with ./gradlew installDist

  • sourceSets

A SourceSet represents a logical group of Java source and resources.

using this to set sourceSets

apply plugin: 'java'
sourceSets{
    main.java.srcDir "src/main/java"
    main.resources.srcDir "src/main/resources"
}
  • exclude all the files in resources

by default, all the files in resources/ will be included in distribution, however we cannot find those files. So it would cause some trouble when we need to modify those config files in resources/. Since we have assemble those config files from others path into our distribution, we need to exclude all the files under resources/.

// exclude all the files in resources/
processResources {
    exclude { "**/*.*" }
}
  • APP_HOME

about $APP_HOME: look into the executable file in bin folder which is generated using ./gradlew assembleDist(or ./gradlew assemble). We can see it Attempt to set APP_HOME at first, and APP_HOME = pwd -P

the problem of using relative path

You can not run your executable file in other paths. For example, your executable file named myFile which is under files, so the path of your executable is home/yourname/files/myFile. You can run myFile using ./myFile at the path:home/yourname/files perfectly, however if you try to run myFile using ./files/myFile at the path:home/yourname, it will break!!

results matching ""

    No results matching ""