Replace version tokens in source files and build.gradle

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Replace version tokens in source files and build.gradle

Guillaume Laforge
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 Laforge
Groovy Project Manager
Head of Groovy Development at SpringSource
http://www.springsource.com/g2one

Twitter: @glaforge

Reply | Threaded
Open this post in threaded view
|

RE: Replace version tokens in source files and build.gradle

jstuyts
> 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.

I usually don't store version numbers in source files, but instead I store them in properties files on the classpath instead. These properties files are very easy to create using the "echoproperties" Ant task. But you do need a bit of code to load the properties file in the class of course.

--
Regards, Johan


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

    http://xircles.codehaus.org/manage_email


--
Regards, Johan
Reply | Threaded
Open this post in threaded view
|

Re: Replace version tokens in source files and build.gradle

Rene Groeschke
In reply to this post by Guillaume Laforge
Hi Guillaume,
I think there are two ways of achieving your needs.
1. you could store a property file in your resource and replace it during
the build. mr. haki explained this briefly at
http://mrhaki.blogspot.com/2010/11/gradle-goodness-add-filtering-to.html
2. you could create a build.properties file DURING your build and store it
in the classes folder. the first draft of the griffon build uses this
approach. Therefore you can have a look at griffons build.gradle file
(https://github.com/griffon/griffon/blob/master/build.gradle) and the task
"createBuildProperties"

I strongly recommend the first approach ;-)

regards,
René



> 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 Laforge
> Groovy Project Manager
> Head of Groovy Development at SpringSource
> http://www.springsource.com/g2one
>
> Twitter: @glaforge
> Google+: http://gplus.to/glaforge
>





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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Replace version tokens in source files and build.gradle

Rolf Suurd
In reply to this post by Guillaume Laforge
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 | Threaded
Open this post in threaded view
|

Re: Replace version tokens in source files and build.gradle

Guillaume Laforge
Thanks a lot for your feedback guys!

On Mon, Aug 8, 2011 at 17:07, rolf <[hidden email]> wrote:
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





--
Guillaume Laforge
Groovy Project Manager
Head of Groovy Development at SpringSource
http://www.springsource.com/g2one

Twitter: @glaforge