How to create a class path for a Gradle project?

I have a gradle project with several packages. After the build, each package generates its own jar files in the build / libs file. External jar dependencies drag into ~ / .gradle. Now I would like to start the service locally from the command line using the appropriate class path. For this purpose, I am writing a script that creates a class path. The problem is that the script does not understand all external dependencies and therefore cannot build the path to the classes. Is there any way for gradle to help with this? Ideally, I would like to dump all the dependencies into a folder at the end of the build.

+4
source share
2 answers

Firstly, I would suggest using the application plugin if you can, since it will take care of it already.

If you want to reset the file path yourself, the easiest way:

task writeClasspath << {
    buildDir.mkdirs()
    new File(buildDir, "classpath.txt").text = configurations.runtime.asPath + "\n"
}

If you want to copy all the libraries in the directory path, you can do:

task copyDependencies(type: Copy) {
    from configurations.runtime
    into new File(buildDir, "dependencies")
}
+10
source

You can try something like this in your build script:

// add an action to the build task that creates a startup shell script
build << {
    File script = file('start.sh')

    script.withPrintWriter {
        it.println '#!/bin/sh'
        it.println "java -cp ${getRuntimeClasspath()} com.example.Main \"\$@\""
    }

    // make it executable
    ant.chmod(file: script.absolutePath, perm: 'u+x')
}

String getRuntimeClasspath() {
    sourceSets.main.runtimeClasspath.collect { it.absolutePath }.join(':')
}
+1
source

All Articles