Problem with "filter" in a Jar task

Strange trouble with Filter in Jar task.

I am using the Filter functionality on a Jar task as documented here:

http://www.gradle.org/releases/1.0-milestone-4/docs/userguide/userguide_single.html#filterOnCopy

in this code:

jar {
    manifest {
        instruction 'Export-Package', '*'
        instruction 'Import-Package', '*'
    }
      filter { String line ->
         line.replace "@application.version", "8.2.5.0"
    }
}

The replace seems to work on the one instance of a .properties file that needs this functionality. But it’s having a side effect. A project that depends on the filter project with the filter no longer can compile because of a class in the filter project that appears to be corrupted.

I get this error:

bad class file: com/ccs/engine/conf/ModelRepositoryProperties.class(com/ccs/engine/conf:ModelRepositoryProperties.class) bad constant pool tag: 108 at 169 Please remove or make sure it appears in the correct subdirectory of the classpath. import com.ccs.engine.conf.ModelRepositoryProperties;

If I comment out the filter, this problem goes away. The “@application.version” string doesn’t appear anywhere in the code - just the properties file.

This makes me suspect there is a bug in the filter functionality. But I’m not sure. One work-around would be so apply the filter only to the one .properties file, but I am not sure how to do that.

ideas?

Thanks phil

^

I’m not familiar with the filter functionality, but if all you are doing is replacing a token in the properties file, I would use the expand method on the processResources task.

processResources {
  expand '@application.version':'8.2.5.0'
}

You’ll have to tweak that to fit how your properties file references the property. Hope that helps.

processResources is definitely the way to go. However, expand() is fairly limited, and I generally recommend to use an Ant filter instead. For example:

import org.apache.tools.ant.filters.ReplaceTokens
  processResources {
    filter(ReplaceTokens, tokens: ['application.version': '8.2.5.0'])
    }

This will replace every occurrence of ‘@application.version@’.

Thanks guys.

I did this and it solved the problem:

processResources {
    from(sourceSets.main.resources.srcDirs) {
        include '**/appversion.properties'
        filter { String line ->
            line.replace "@application.version", "8.2.5.0"
        }
    }
}

This approach is better than my original in that it only applies to the filtered file(s), instead of trying the replace on all.

But I still don’t understand why my original approach corrupted a class file as “@appversion.version” is not in any classes. I suspect a bug in the filter…

Well, it probably reads in the whole binary file as a text file and spits it out again. Not too difficult to imagine that this causes some damage.

ah,yes. this makes sense. i had it in my head that the filter was being applied to the source sets. It’s a jar task, so obviously it’s being applied to the class files.

got it, thanks!

the bug may be this one http://issues.gradle.org/browse/GRADLE-1566 or similar