1

I am new to gradle. I want to setup a multi-module project environment with the following properties:

  1. The main project is called 'webapp' which has two subprojects 'webapp-client' and 'webapp-server' where client is javascript-based project and server is a RESTFull stack.
  2. Each of 'webapp-client' and 'webapp-server' should be independent projects, i.e. one can develop them as if there is no 'webapp' super-project. 'webapp-client' uses Node.js, Bower, and Grunt internally but I tried to integrate them as gradle tasks.
  3. 'webapp-client' builds its output into 'dist' folder of the project. 'webapp-server' creates a war file which is deployed to JFrog Artifactory.
  4. There is another artifact which is created by the 'webapp' project. It combines the output of 'webapp-client' and 'webapp-server' into one single war file. It is also deployed to Artifactory (see war.dependsOn parts in webapp:build.gradle file)
  5. Each project is version controlled under a separate Git repository (This is the reason to define project(':webapp-server').projectDir = new File('../webapp-server') like code in webapp:settings.gradle)
  6. What I tried is to refactor all variables into gradle.properties files

I made the following structure:

project structure

And here is the source of those files: webapp: build.gradle:

apply plugin: 'java'
apply plugin: 'war'

sourceCompatibility = theSourceCompatibility
group = theProjectGroup
version = theProjectVersion

repositories {
    maven {
        url "${artifactoryContextUrl}/remote-repos"
    }
}

buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }

    dependencies {
        classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.1.1"
    }
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

war.dependsOn(":webapp-client:gruntBuild")
war.dependsOn(":webapp-server:explodedWar")
war {
    from "../webapp-client/dist"
    from { project(":webapp-server").explodedWar }
}

apply plugin: "com.jfrog.artifactory"
apply plugin: 'maven-publish'

artifactory {
    contextUrl = "${artifactoryContextUrl}"
    publish {
        repository {
            repoKey = 'libs-release-local'
            username = "${artifactory_user}"
            password = "${artifactory_password}"
            maven = true
        }
        defaults {
            publications('webApp')
        }
    }
}

publishing {
    publications {
        webApp(MavenPublication) {
            from components.web
        }
    }
}

webapp: settings.gradle:

rootProject.name = theProjectName

include ':webapp-server'
project(':webapp-server').projectDir = new File('../webapp-server')

include ':webapp-client'
project(':webapp-client').projectDir = new File('../webapp-client')

webapp: gradle.properties:

theProjectGroup=com.ourcompany
theProjectName=webapp
theProjectVersion=1.0
theSourceCompatibility=1.8

artifactoryContextUrl=http://192.168.0.12:7078/artifactory
artifactory_user=admin
artifactory_password=password

webapp-client: build.gradle:

apply plugin: 'java'

group = theProjectGroup
version = theProjectVersion

repositories {
    maven {
        url "${artifactoryContextUrl}/remote-repos"
    }
}

buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }

    dependencies {
        classpath 'com.moowork.gradle:gradle-node-plugin:0.11'
        classpath 'com.moowork.gradle:gradle-grunt-plugin:0.11'
    }
}

apply plugin: 'com.moowork.node'
apply plugin: 'com.moowork.grunt'

node {
    version = '4.2.6'
    npmVersion = '3.7.1'
    download = true
}

task npmCacheConfig(type: NpmTask) {
    description = "Configure the NPM cache"
    def npmCacheDir = "${gradle.getGradleUserHomeDir()}/caches/npm"
    outputs.files file(npmCacheDir)
    args = ['config', 'set', 'cache', npmCacheDir]
}

task npmPackages(type: NpmTask, dependsOn: npmCacheConfig) {
    description = "Install Node.js packages"
    args = ['install']
    inputs.files file('package.json')
    outputs.files file('node_modules')
}

task bowerInstall(type: NodeTask) {
    script = file('node_modules/bower/bin/bower')
    args = ["--config.storage.cache=${gradle.getGradleUserHomeDir()}/caches/bower/cache",
            "--config.storage.packages=${gradle.getGradleUserHomeDir()}/caches/bower/packages",
            "--config.storage.registry=${gradle.getGradleUserHomeDir()}/caches/bower/registry",
            'install']
    inputs.files file('bower.json')
    outputs.files file('bower_components')
    dependsOn npmPackages
}

grunt_build.inputs.dir file('app')
grunt_build.dependsOn 'installGrunt'
grunt_build.dependsOn 'bowerInstall'

task gruntBuild() {
    dependsOn grunt_build
}

task gruntServe() {
    dependsOn grunt_serve
}

webapp-client: gradle.properties:

theProjectGroup=com.ourcompany
theProjectName=webapp-client
theProjectVersion=1.0-SNAPSHOT

artifactoryContextUrl=http://192.168.0.12:7078/artifactory
artifactory_user=admin
artifactory_password=password

webapp-server: build.gradle:

apply plugin: 'java'
apply plugin: 'war'

group = theProjectGroup
version = theProjectVersion
sourceCompatibility = theSourceCompatibility

repositories {
    maven {
        url "${artifactoryContextUrl}/libs-snapshot"
    }
    maven {
        url "${artifactoryContextUrl}/remote-repos"
    }
}

buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }

    dependencies {
        classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.1.1"
    }
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

apply plugin: "com.jfrog.artifactory"
apply plugin: 'maven-publish'

artifactory {
    contextUrl = "${artifactoryContextUrl}"
    publish {
        repository {
            repoKey = 'libs-snapshot-local'
            username = "${artifactory_user}"
            password = "${artifactory_password}"
            maven = true
        }
        defaults {
            publications('webApp')
        }
    }
}

publishing {
    publications {
        webApp(MavenPublication) {
            from components.web
        }
    }
}

task explodedWar(type: Copy, dependsOn: war) {
    def zipFile = project.war.archivePath
    def outputDir = file("${buildDir}/unpacked/war")

    from zipTree(zipFile)
    into outputDir
}

webapp-server: gradle.properties:

theProjectGroup=com.ourcompany
theProjectName=webapp-server
theProjectVersion=1.1-SNAPSHOT

artifactoryContextUrl=http://192.168.0.12:7078/artifactory
artifactory_user=admin
artifactory_password=password

webapp-server, webapp-client: settings.gralde:

rootProject.name = theProjectName

As I told, I'm new to gralde and do not really know how good is the job done. I do not know whether gradle.properties are good place to put variables. Also publishing into artifactory runs into problem. Running gradle artifactoryPublish --info results:

Executing task ':webapp-server:artifactoryPublish' (up-to-date check took 0.0 secs) due to:
  Task has not declared any outputs.
:webapp-server:artifactoryPublish (Thread[main,5,main]) completed. Took 0.008 secs.
:artifactoryPublish (Thread[main,5,main]) started.
:artifactoryPublish
Executing task ':artifactoryPublish' (up-to-date check took 0.0 secs) due to:
  Task has not declared any outputs.
Deploying artifact: http://192.168.0.12:7078/artifactory/libs-snapshot-local/com/ourcompany/webapp-server/1.1-SNAPSHOT/webapp-server-1.1-SNAPSHOT.war
Deploying artifact: http://192.168.0.12:7078/artifactory/libs-snapshot-local/com/ourcompany/webapp-server/1.1-SNAPSHOT/webapp-server-1.1-SNAPSHOT.pom
Deploying artifact: http://192.168.0.12:7078/artifactory/libs-snapshot-local/com/ourcompany/webapp/1.0/webapp-1.0.war
:artifactoryPublish FAILED
:artifactoryPublish (Thread[main,5,main]) completed. Took 1.36 secs.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':artifactoryPublish'.
> java.io.IOException: Failed to deploy file: HTTP response code: 409. HTTP response message: Conflict

Seems that when trying to push webapp artifact into repository, some variables are for 'webapp-client' subproject!

rabolfazl
  • 435
  • 1
  • 8
  • 24

0 Answers0