Release Engineer’s are inevitable part in software engineering field, who is concerned with compilation, wrapping up or assembly and delivery (deployment) of the source code. These days bigger projects are using Gradle as the build tool since it makes release of the code much faster and hassle free. Gradleware is the Enterprise Automation Company supporting gradle doing a great job in keeping gradle open source, community developed and they are giving high quality training in build and automation using gradle.
In this post I am sharing few gradle tips and tricks which are going to be useful for build engineers. You might be aware of most of these tips, but I think it will be useful for newbies.
Gradle tip #1
How to add a line in MANIFEST.MF inside the war/jar which is created.
MANIFEST.MF is generally found in META-INF directory of jar/war that you’ve created which is used to define extensions and package related data. If you want to specify anything in MANIFEST.MF file, you should do it explicitly as gradle will not take the file in the project automatically to the war/jar. For example, if you want to add dependency,
attributes("Manifest-Version" : "1.0", "dependencies" : "org.infinispan export")
Specify the above code inside build.gradle after applying the war plugin(If you are generating a jar, specify ‘jar.doFirst’ instead ). The output will be a MANIFEST.MF with following lines:
Manifest-Version : 1.0
dependencies : org.infinispan export
Include extra source directory
The below image shows the project layout that gradle assumes. It is not mandatory that these directories should exist. If the directory is found, java plugin will compile the files in it.
The java plugin introduces the concepts of of a source set. It is a group of source files which are compiled and executed together. If you have a an extra resource directory in your project such as “src/main/examples” it will not be included in your generated war/jar. There is a simple way to include it.
Make sure that the above code is in your build.gradle file.
Exclude jars from ear/war
Sometimes you might be noticed that the generated ear/war contains jars which you don’t even specified as a dependency. This is because when you download dependencies from a maven repository online, it will automatically include all the dependent jar files with the required jar. To exclude these jars from the archive,
Insert the above code to build.gradle. If you want to exclude all jars, replace “name_of_the_jar_you_want_to_exclude.jar” with “*.jar”.
Include dependencies from an independent directory
It is possible to include dependencies from a directory inside your project so that gradle will find all the files (or selected ones) and include as a dependency for the project.
compile fileTree("../Root/SubProject/EarContent/lib",includes: ['*.jar'])
The above code will include all the jar files from the specified directory as dependency.
Install a jar to your Local Maven Repository
Generally all the jar dependencies should be avaialable in the Maven Repository. In case if you do not found the jar in maven repo (Like oracle jdbc driver ojdbc6.jar), you can manually install it your local repo and use it. Gradle can help you with this.
apply plugin: 'maven'
group = 'com.googlecode.gwtgl'
version = '0.9.1'
repository url: 'file://' + new File(System.getProperty('user.home'),'.m2/repository').absolutePath
task install(dependsOn: uploadArchives)
The above code assumes both the files gwtgl-0.9.1-sources.jar and gwtgl-0.9.1.jar is in the same directory as the build.gradle.