Reprint Address: http://www.gfzj.us/series/gradle/2014/12/12/gradle%E5%B0%8F%E7%B3%BB%E5%88%97 (4)--gradle%e6%89%93%e5%8c% 85java%e9%a1%b9%e7%9b%ae.html
As an example of the example in the Gradle small series, here are two ways to publish a Java project Gradle:
Fat jar Mode
This method takes resources such as the jar package that the project relies on into an executable jar package, and the resulting jar is large.
In the Gradletest project, we package the SubPro1, so we need to modify the Build.gradle file under SubPro1:
jar {from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }manifest {attributes ‘Main-Class‘: ‘hello.HelloWorld‘}}dependencies {compile project(":SubPro2")}
The above configuration adds the jar Task,from is the dependency of SubPro1 packaging, manifest generates the configuration information to main, that is, the Helloworld.java under the package Hello is the project portal
The gradle build jar package can be generated by executing commands at the root of the project Gradletest
Using the Gradle application plugin
Fat jars are not always a suitable choice, such as the need to rely on the separation from the jar, the software can be started in *nix and Windows Script, and so on. Then use the Gradle application plugin to do it.
In the Gradletest project, we made the following modifications to the SubPro1 build.gradle: Delete the jar task, add the application plugin, and configure the main class (the value of the Mainclassname is set). The result of the modification is as follows:
apply plugin: ‘application‘mainClassName = ‘hello.HelloWorld‘dependencies {compile project(":SubPro2")}
The application plugin provides 5 tasks to implement the project's release, namely
run、startScript、installApp、distZip、distTar
Assuming that you use Distzip, run the command in the Gradletest directory gradle distZip , in SubPro1 build/distributions, you can see the zip file that contains the bin and Lib folders, a software startup script under the bin, Lib is the software jar package and all its dependencies.
Gradle Packaging Java Projects