Why is gradle not excluding resources from my jar?


(Deepak Sharma) #1

This is my jar task:

jar{

description = 'Build with jar files'                                              
                                                                                          
exclude("src/main/resources/**")                                                                                  
                                                                                
manifest {                                                                        
    attributes 'Implementation-Title': 'title'
            'Implementation-Version': 'text,                                    
            'Main-Class': 'mainclass',                
            'Class-Path': './lib/*'                                               
}                                                                                 

}

My application resources are in src/main/resources and neatly organized per environment (dev, uat and prod). I have tried several versions of the above exclude statement, using relative and absolute paths but the resultant jar always contains my resources directory. How do I exclude my resources from my jar?

Edit: Is there a way to do this without editing the sourceSets[“main”].resources property?


(James Justinic) #2

The exclude paths are relative to the JAR file, not the project directory. You would need to remove src/main/resources from your exclude path to actually target the files at src/main/resources/dev, src/main/resources/uat, and src/main/resources/prod. However, you can’t just do exactly that because that would also remove anything from src/main/java as well.

It’s not clear what you actually end up doing with these files. Here’s a few ways to consider, but your best option might depend on what you ultimately do with them.

  • In the jar {...} configuration, exclude the folders: exclude('dev', 'uat', 'prod')
  • Disable the processResources task or exclude * from it instead of from jar.
  • If these aren’t resources that should end up in the jar, move them to src/main/envproperties or similar rather than the location that by convention does end up in the jar.

(uklance) #3

Personally, I wouldn’t mess around with the gradle (and maven) directory conventions. It’s always better to include extra directories where you need them rather than exclude them where you don’t. Eg: You’re trying to exclude the directory from the jar but the files are still part of the runtime configuration (eg they’re still on the test classpath)

Have you considered using src/dev/resources and src/uat/resources instead?

You might also be interested in the gradle-java-flavours plugin where you could specify dev, uat and prod as flavours and have specific resources, java code and/or tests for each flavour as well as a separate jar for each