1

我是新手。我想设置一个具有以下属性的多模块项目环境:

  1. 主项目称为“webapp”,它有两个子项目“webapp-client”和“webapp-server”,其中客户端是基于 javascript 的项目,服务器是 RESTFull 堆栈。
  2. 'webapp-client' 和'webapp-server' 应该是独立的项目,即可以像没有'webapp' 超级项目一样开发它们。'webapp-client' 在内部使用 Node.js、Bower 和 Grunt,但我尝试将它们集成为 gradle 任务。
  3. “webapp-client”将其输出构建到项目的“dist”文件夹中。'webapp-server' 创建一个部署到 JFrog Artifactory 的 war 文件。
  4. 还有另一个由“webapp”项目创建的工件。它将“webapp-client”和“webapp-server”的输出组合到一个单一的war文件中。它也被部署到 Artifactory(参见war.dependsOnwebapp:build.gradle 文件中的部分)
  5. 每个项目都在单独的 Git 存储库下进行版本控制(这就是project(':webapp-server').projectDir = new File('../webapp-server')在 webapp:settings.gradle 中定义类似代码的原因)
  6. 我尝试将所有变量重构为 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
        }
    }
}

网络应用程序: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')

网络应用程序: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-客户端: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

网络应用服务器: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-服务器: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.grade:

rootProject.name = theProjectName

正如我所说,我是 gralde 的新手,真的不知道这项工作做得有多好。我不知道 gradle.properties 是否是放置变量的好地方。发布到工件中也会遇到问题。运行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-client”子项目!

4

0 回答 0