War layout


(Maciek Opała) #1

Hi, Is it possible (and how) to build a war archive in the following way: - all the java classes from src/main/java will be compiled, zipped to jar and copied under WEB-INF/lib - the configuration from src/main/resources won’t be included in the jar mentioned above but only copied under WEB-INF/classes?


(Vikas Varma) #2

By default when you apply the war plugin, it disables the default JAR archive generation.

In order to enable it…

jar{ enabled=true }

The gradle userguide has a very good example of the various customizations.

http://gradle.org/docs/current/userguide/war_plugin.html


(Maciek Opała) #3

Well… I know that it’s possible the enable the and all the classes will be ‘jarred’ and added to war. But I don’t want resources to be also jarred and when I enable the jar all the resources will automatically jarred. I want them to be copied to some.war/WEB-INF/classes. Is it possible/configurable?


(spencer_allain) #4

Most things that you wish to attempt are possible, it’s just a matter of wiring things up.

Below is an example of one possible solution to what you’ve asked for: (1) resources are not placed into the jar file, (2) the jar file is placed into WEB-INF/lib within the war, (3) resources are copied into the WEB-INF/classes directory within the war.

apply plugin: 'war'
  processResources.enabled = false
jar.enabled = true
  war {
  classpath = jar.outputs.files +
              configurations.runtime - configurations.providedRuntime
    into('WEB-INF/classes') {
    from sourceSets.main.resources
  }
}

There are probably better solutions, but this at least is a starting point which I believe satisifies your currently stated requirements.

-Spencer


(Maciek Opała) #5

Yes, that’s much better. According to ‘wiring things up’ You’re right but I’m still learning. Actually I also need some resources processing (because tokens needs to be replaced with filter) but I’ll try it on my own.


(spencer_allain) #6

If you still want resource processing then two slightly different paths might be an option:

apply plugin: 'war'
  jar {
  enabled = true
  includeEmptyDirs = false
    eachFile { copyDetails ->
    if (copyDetails.file.path.contains('resources')) {
      copyDetails.exclude()
    }
  }
}
  war {
  classpath = jar.outputs.files +
              configurations.runtime - configurations.providedRuntime
    into('WEB-INF/classes') {
    from { -> sourceSets.main.output.resourcesDir }
  }
}

or something like this (which is probably nicer):

apply plugin: 'war'
  task myjar(type: Jar) {
  from sourceSets.main.output.classesDir
}
  war {
  classpath = myjar.outputs.files +
              configurations.runtime - configurations.providedRuntime
    into('WEB-INF/classes') {
    from { -> sourceSets.main.output.resourcesDir }
  }
}

-Spencer


(Maciek Opała) #7

Wow! Will investigate it in the nearest future.