设置带有子项目的自定义gradle项目

设置带有子项目的自定义gradle项目,gradle,artifactory,multi-project,subproject,Gradle,Artifactory,Multi Project,Subproject,我是格拉德尔的新手。我要设置具有以下属性的多模块项目环境: 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 theP

我是格拉德尔的新手。我要设置具有以下属性的多模块项目环境:

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
theProjectGroup=com.ourcompany
theProjectName=webapp-client
theProjectVersion=1.0-SNAPSHOT

artifactoryContextUrl=http://192.168.0.12:7078/artifactory
artifactory_user=admin
artifactory_password=password
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”,它有两个子项目“webapp客户端”和“webapp服务器”,其中客户端是基于javascript的项目,服务器是RESTFull堆栈
  • “webapp客户端”和“webapp服务器”都应该是独立的项目,也就是说,可以像没有“webapp”超级项目一样开发它们webapp客户端在内部使用Node.js、Bower和Grunt,但我试图将它们集成为gradle任务
  • “webapp客户端”将其输出构建到项目的“dist”文件夹中webapp server'创建一个war文件,该文件部署到JFrog Artifactory
  • “webapp”项目创建了另一个工件。它将“webapp客户端”和“webapp服务器”的输出合并到一个war文件中。它还部署到Artifactory(请参见webapp:build.gradle文件中的
    war.dependsOn
    parts)
  • 每个项目都在一个单独的Git存储库下进行版本控制(这是定义
    project(':webapp server').projectDir=new File('../webapp server')
    类似于webapp:settings.gradle中的代码的原因)
  • 我尝试的是将所有变量重构为gradle.properties文件
  • 我提出了以下结构:

    以下是这些文件的来源: 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
            }
        }
    }
    
    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
    }
    
    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: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
    
    theProjectGroup=com.ourcompany
    theProjectName=webapp-client
    theProjectVersion=1.0-SNAPSHOT
    
    artifactoryContextUrl=http://192.168.0.12:7078/artifactory
    artifactory_user=admin
    artifactory_password=password
    
    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客户端: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
            }
        }
    }
    
    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
    }
    
    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客户端: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
    
    theProjectGroup=com.ourcompany
    theProjectName=webapp-client
    theProjectVersion=1.0-SNAPSHOT
    
    artifactoryContextUrl=http://192.168.0.12:7078/artifactory
    artifactory_user=admin
    artifactory_password=password
    
    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服务器: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
            }
        }
    }
    
    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
    }
    
    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服务器: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
    
    theProjectGroup=com.ourcompany
    theProjectName=webapp-client
    theProjectVersion=1.0-SNAPSHOT
    
    artifactoryContextUrl=http://192.168.0.12:7078/artifactory
    artifactory_user=admin
    artifactory_password=password
    
    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服务器,webapp客户端:settings.gralde:

    rootProject.name = theProjectName
    
    正如我所说的,我是格拉尔德的新手,不知道这项工作做得有多好。我不知道gradle.properties是否是放置变量的好地方。 同时,发布到artifactory也遇到了问题。运行
    gradle artifactoryPublish--info
    结果:

    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
    
    似乎在尝试将webapp工件推送到存储库时,有些变量是用于“webapp客户端”子项目的