2
votes

We have just started using Gradle and do have a few (noob) questions - hopefully someone can shed some light on those issues :)

We're using Angular and Grails to build our web-app. We want to be as modular as possible and hence put all the Angular-related artifacts (mainly *.js and *.html files) in a separate project in our Gradle multiproject build.

Our project structure is as follows:

- root
-- build.gradle
-- settings.gradle
|-- web-grails (grails project)
|----- build.gradle
|-- web-js-html (angular / js / html sources)
|----- build.gradle

As a start, we simply want to package web-js-html project accordingly. What we've come up so far (other suggestions very welcome) is to apply a webjars structure to it, i.e. have a .jar file with the required resources under /META-INF/resources. Online, we found the following config that seems to work just fine:

// file :web-js-grails/build.gradle
apply plugin: 'java'

ext {
    webjarconfig = [
            staticHTMLFilesDir : "${projectDir}/src/main/webfrontend",
            baseDir: "META-INF/resources/",
            subDir : "webjars/" + deployed_app_name
    ]
}

configurations {
    webjar
}

task webjar(type: Jar, dependsOn: 'jar') {
    from(fileTree(webjarconfig.staticHTMLFilesDir)) {
        into webjarconfig.baseDir + webjarconfig.subDir
    }
    outputs.file archivePath
}


artifacts {
    webjar(webjar.archivePath) {
        type 'jar'
        builtBy webjar
    }
}

By invoking 'gradle webjar', the jar gets created with the files in the correct place.

Question 1: What I would have expected is that this jar also gets properly created if I invoke 'gradle build'. As far as I understand, 'gradle build' is a task defined by the java plugin which, at some point, invokes the 'jar' task. Once that 'jar' task is done, I would expect the webjar task to be invoked. But it's not, so clearly I'm missing something. Does it follow that webjar only ever gets executed if explicitly invoked (either from command-line or from within the build.gradle file)?

Now, we would like the webjar to be included in the web-grails war-file. The config of :web-grails/build.gradle is as follows:

apply plugin: "grails"

repositories {
    mavenLocal()
    maven { url artifactory_url }
}

buildscript {
    repositories {
        mavenLocal()
        maven { url artifactory_url }
    }

    dependencies {
        classpath 'org.grails:grails-gradle-plugin:2.0.1-SNB1'
    }
}

grails {
    grailsVersion = '2.3.8'
    groovyVersion = '2.3.0'
}

dependencies {
    bootstrap 'org.grails.plugins:tomcat:7.0.50'
    compile project(':web-js-html')
}

After try-and-error and quite a bit of reading, I arrived at this (possibly wrong) conclusion: when I invoke 'gradle build' on :web-grails, then (I assume) :build will also be invoked on the referenced :web-js-html project. I say this because the jar gets re-created in the build/lib folder, but obviously not using the webjar-task. Hence, the resulting jar only contains the MANIFEST.MF only.

Question 2: Do I use Gradle correctly in that case and am I only overseeing a little thing or is this whole approach questionable? How can I get the :web-js-html jar into the war properly?

Thank you for your help in advance!

1

1 Answers

2
votes

Your part where you define the new artifact doesn't make any sense for me. Change

artifacts {
    webjar(webjar.archivePath) {
        type 'jar'
        builtBy webjar
    }
}

to

artifacts {
    webjar webjar
}

Maybe you should rename either your configuration or your task. However the first webjar is your configuration and the second one your task which creates the new jar. Note that this will create a new artifact, so you have to give it a different name with

task webjar(type: Jar, dependsOn: 'jar') {
    baseName = 'newJar'
    from(fileTree(webjarconfig.staticHTMLFilesDir)) {
        into webjarconfig.baseDir + webjarconfig.subDir
    }
    outputs.file archivePath
}

But I think you don't want to create a second jar, but change the original one. In that case your don't have to write a new task, but configure the default jar task like this:

jar {
    from(fileTree(webjarconfig.staticHTMLFilesDir)) {
        into webjarconfig.baseDir + webjarconfig.subDir
    }
    outputs.file archivePath
}