How to add an Artifactory WAR as a Gradle dependency

(rattek) #1

I’m working on a Spring MVC project (using Gradle) to produce a template to be integrated on different projects. The template project contains few HTML files and some classes to handle the properties. I’ve configured the artifactory-gradle-plugin to generate and publish a WAR containing my HTML files and classes. Everything works just fine and my WAR and POM files are now available under my Artifactory server. Here’s a part of my build.gradle file

war {

artifactory {
    publish {
	    contextUrl = "myurl"  
        repository {
            repoKey = 'myrepo'
            username = "myuser"
            password = "mypasswd"
            maven = true
        defaults {
            publications ('mavenJava')
publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.web

Unfortunately when I try to add this artifact as dependency on an other project, Gradle successfully finds the WAR but nothing is integrated in the project. I can’t find any HTML, classes or war files in the project.

dependencies {
    compile(group: 'test.spring', name: 'test-layout', version: '0.0.1', ext: 'war')

Is there something missing or am I completely of the track with this?

(Lance Java) #2

Unlike maven, you can’t use a war as a template like this. You could possibly achieve what you want by

apply plugin: 'war'
configurations {
   warTemplate { transitive = false }
dependencies {
   warTemplate 'test.spring:test-layout:0.0.1@war'
   compile fileTree("$buildDir/warTemplate/WEB-INF/lib").matching {
      include '*.jar'
   compile files("$buildDir/warTemplate/WEB-INF/classes")

task explodeWarTemplate(type:Copy) {
   from zipTree(configurations.warTemplate.singleFile)
   into "$buildDir/warTemplate"

compile {
   dependsOn explodeWarTemplate

war {
   from fileTree("$buildDir/warTemplate").matching {
      exclude 'WEB-INF/**'
   webInf { 
      from fileTree("$buildDir/warTemplate/WEB-INF").matching {
         exclude 'WEB-INF/lib/**'
         exclude 'WEB-INF/classes/**'

(rattek) #3

Thanks for your help!! Would it be easier with a Jar instead of War?

(Lance Java) #4

If it were me I’d probably have two artifacts

  1. A jar for the WEB-INF/classes (and transitive dependencies) which can be added to the compile classpath

  2. A zip for the html/jsp/images and web.xml

This way the dependencies could properly participate in dependency resolution (since the meta data isn’t lost when unzipping the war)

(rattek) #5

Sounds good. I’ll proceed using your suggestion. Unfortunately, I was wondering to build a single artifact but it seems that itsn’t supported yet (or at all). Thanks for your help!

I’ve also asked this question on stackoverflow Feel free to answer the question there and I’ll accept it. Otherwise, I’ll add a link to this post on my stackoverflow post.