gradle/groovy: A simple way to check if a gradle task name has been invoked (e.g. “assembleRelease” for Android developers)

If you google for this question, you’ll find a bunch of crap answers about creating tasks and checking the task graph, bullshit.

All you need to do is check if a parameter has been passed to gradle.

Keep it simple and stupid:

boolean isAssembleRelease = gradle.startParameter.taskNames.contains("assembleRelease")

(If you’re working with an android project, you can define that variable before the android { section starts)

Then, if you need to do something different somewhere else down in your script, say, ask for a key alias and key password to sign your release (because they invoked ./gradlew assembleRelease you do:

signingConfigs {
        release {
            if (isAssembleRelease) {
                // your code here to ask for the key alias and password
            }
}

GRADLE: How to specify resources from different folders on your sourceSet

Sometimes you need to have resources in your sourceset and these may come from different locations on disk, the official documentation is lacking in real world examples, or at least I just didn’t understand it very well, but from reading forums I finally got it to work.

In the example I specify what files to include/exclude from two different folders.
When the final .jar is created, they’ll keep the package path structure that lives inside the given srcDir folders.

If you just want to add these files (for some reason to the root of your resulting jar, you should make srcDir the full path to where the files live)

sourceSets {
main {
java {
//your java source paths and exclusions go here…
}

resources {
srcDir ‘components/resources/src/main/resources’
include ‘**/*.properties’
include ‘**/*.png’
include ‘**/*.gif’
include ‘**/*.jpg’
include ‘**/*.html’
include ‘**/*.js’
include ‘**/*.sh’
include ‘**/*.dat’
include ‘**/*.icc’
exclude ‘**/*.DS_Store’

srcDir ‘common/vuze/azureus2/src’
include ‘**/Messages*.properties’
exclude ‘**/*.class’
exclude ‘**/*.java’
}
}
}

GRADLE: How to copy files from another .jar into your resulting output .jar

In our project we like to deliver a single jar as the final product, if you need to copy files that live on an existing jar into the Gradle’s output jar, this example shows you how to do that (and more)

jar {
//this is how you change the name of the output jar
archiveName=’frostwire.jar’

//some exclusion rules to keep your .jar clean
exclude(‘META-INF/*.SF’, ‘META-INF/*.DSA’, ‘META-INF/*.RSA’, ‘META-INF/*.MF’)

//here we grab all the .class files inside messages.jar and we put them in our resulting jar
from (zipTree(‘lib/jars/messages.jar’)) {
include ‘**/*.class’
}

//how to manipulate the jar’s manifest
manifest {
attributes ‘Main-Class’: ‘com.limegroup.gnutella.gui.Main’
}
}

GRADLE: How to add a list of local .jar files to the build classpath

Sometimes you don’t want/cant use maven repos, all you have is a bunch of local jars on disk that you want to use as part of your compilation classpath, and the freaking gradle documentation is too vague.

Here is an example:

dependencies {
compile files(‘lib/jars/gettext-commons.jar’,
‘lib/jars/lucene-3.5.0.jar’,
‘lib/jaudiotagger.jar’,
‘lib/jars/h2-1.3.164.jar’,
‘lib/jars/messages.jar’,
‘lib/jars/slf4j-api-1.7.5.jar’,
‘lib/jars/jaudiotagger.jar’,
‘lib/jars/metadata-extractor-2.6.2.jar’
)
}

[SOLVED] Gradle: How to increase the Java Compiler’s available Heap Memory

The documentation is not very clear on what all the available options are…
after much Googling and many different attempts finally figured out how to raise the maximum heap of the compiler from within the gradle.build script.

apply plugin: ‘java’

compileJava {
//raise heap
options.fork = ‘true’
options.forkOptions.with {
memoryMaximumSize = “2048m”
}
}

Update:
So I’ve noticed this works great on MacOSX, but it doesn’t work at all on Windows 8.

The solution to increasing the JVM’s Heap has been to remove those options from gradle.build script and add a new file on the same folder as where your gradle.build file lives called ‘gradle.properties’

These are the contents that made it work for both Mac and Windows (I’ve still not tested this on my linux box)

org.gradle.jvmargs=-Xms256m -Xmx1024m