Problem with "filter" in a Jar task

(Phil Swenson) #1

Strange trouble with Filter in Jar task.

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

in this code:

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

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.


Thanks phil


(andrew.oberstar) #2

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':''

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

(Peter Niederwieser) #3

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

  processResources {
    filter(ReplaceTokens, tokens: ['application.version': ''])

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

(Phil Swenson) #4

Thanks guys.

I did this and it solved the problem:

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

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…

(Peter Niederwieser) #5

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.

(Phil Swenson) #6

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!

(Łukasz L.) #7

the bug may be this one or similar