Guillaume Laforge <glaforge@...> writes:

> 
> 
> Hi all,
> 
> I have a build.gradle file where I have the version of my project within a 
variable: version = '1.1'.
> And I also have a source file with a method returning the version, say 
something like String getVersion() { '1.1' }.
> 
> I'd like to know what's the easiest way to factor that version number, for 
example in some build.properties file, and have gradle taking care of replacing 
some token (like the Ant token replace mechanism) in my source file, and I 
could 
reference that properties file from my build file as well.
> 
> Thanks for your help.-- Guillaume LaforgeGroovy Project ManagerHead of Groovy 
Development at SpringSourcehttp://www.springsource.com/g2one
> 
> 
> Twitter:  <at> glaforge
> Google+: http://gplus.to/glaforge
> 
> 
> 
> 

Hi Guillaume,

If you really want to replace tokens straight in your source files, which 2 
other posters already discouraged, i've gona ahead and made a small sample that 
replaces tokens in your main sourceset:

apply plugin: 'java'

def version = '1.1'
def filteredSourceDir = file("${buildDir}/filtered")

sourceSets {
    // This source set will contain all sources that we filter
    filtered {
        java {
            srcDirs = [filteredSourceDir]
        }
    }
}

// tell the compileJava task to compile the filtered source 
compileJava.source = sourceSets.filtered.java


// copy the main sources and filter any '$version' occurences.
task processVersion (type: Copy) {
    from sourceSets.main.java
    into filteredSourceDir
    expand(version: '1.1')
}

compileJava.dependsOn processVersion


The above buildscript would transform a function like this

public String getVersion () {
    return "$version";
}

Into:

public String getVersion () {
    return "1.1";
}

Whether or not this is good practice is not for me to judge ;) 

Hope this helps!

Greets Rolf



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply via email to