【问题标题】:how to tell gradle to build and upload archives of dependent projects to local maven如何告诉 gradle 构建依赖项目的档案并将其上传到本地 maven
【发布时间】:2013-12-30 21:18:47
【问题描述】:

我有一个多项目问题,Gradle 的文档没有任何信息(也无法在 stackoverflow 上找到)。任何帮助/指导将不胜感激。

这是我的项目布局:

  • 项目LeftArm:生成一个jar文件
  • 项目RightArm:生成一个jar文件
  • 项目AllArms:依赖于项目LeftArmRightArm并产生war文件

当我在 AllArms 项目中运行“gradle build uploadArchives”时,它会构建 LeftArmRightArm 项目,但不会上传 @ 987654322@ 文件(由 LeftArmRightArm 项目生成)到本地 Maven 存储库。

这是我的build.gradle 文件:

project LeftArm:
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'maven'

ext.artifactId = 'LeftArm'
archivesBaseName = ext.artifactId // sets filename of warfile/jarfile output
group = 'mygroup'
version = '1.0'

buildscript 
{
    repositories 
    {
    mavenLocal()
    }
}

jar 
{
    from(sourceSets['main'].allJava)
}

dependencies 
{
    compile group: 'javax.validation', name:'validation-api', version:'1.0.0.GA'
    compile group: 'javax.validation', name:'validation-api-sources', version:'1.0.0.GA'
}

def localRepoURL = 'file://' + new File(System.getProperty('user.home'), '.m2/repository').absolutePath

// NOTE: this project will publish to the local Maven repo.
uploadArchives 
{
    repositories 
    {
    mavenDeployer 
    {
        repository(url: localRepoURL)
        addFilter('jar') 
        { 
            artifact, file -> artifact.ext == 'jar'
        }
    }
    }
}

...

project RightArm:
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'maven'

ext.artifactId = 'RightArm'
archivesBaseName = ext.artifactId // sets filename of warfile/jarfile output
group = 'mygroup'
version = '1.0'

buildscript 
{
    repositories 
    {
    mavenLocal()
    }
}

jar 
{
    from(sourceSets['main'].allJava)
}

dependencies 
{
    compile group: 'javax.validation', name:'validation-api', version:'1.0.0.GA'
    compile group: 'javax.validation', name:'validation-api-sources', version:'1.0.0.GA'
}

def localRepoURL = 'file://' + new File(System.getProperty('user.home'), '.m2/repository').absolutePath

// NOTE: this project will publish to the local Maven repo.
uploadArchives 
{
    repositories 
    {
    mavenDeployer 
    {
        repository(url: localRepoURL)
        addFilter('jar') 
        { 
            artifact, file -> artifact.ext == 'jar'
        }
    }
    }
}

...

project AllArms:
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'maven'
apply plugin: 'idea'
apply plugin: 'eclipse'

ext.artifactId = 'AllArms'
ext.sourceCompatibility = 1.6
archivesBaseName = artifactId // sets filename of warfile/jarfile output
group = 'mygroup'
version = '1.0'

def gwtVersion = '2.2.0'

repositories
{
    mavenLocal()
    mavenCentral()
}

dependencies
{
    compile project(':LeftArm') 
    compile project(':RightArm') 
    // ###   NOTE:  this is where the build breaks b/c uploadArchives task is not executed  ###
    compile group: group, name: 'LeftArm', version:version
    compile group: group, name: 'RightArm', version:version
    providedCompile group: 'javax.validation', name:'validation-api', version:'1.0.0.GA'
    providedCompile group: 'javax.validation', name:'validation-api-sources', version:'1.0.0.GA'
    providedCompile group: 'com.google.gwt', name:'gwt-user', version:gwtVersion
    providedCompile group: 'com.google.gwt', name:'gwt-dev', version:gwtVersion
    providedCompile group: 'org.gwtext', name:'gwtext', version:'2.0.4'
}

task compileGwt (dependsOn: classes, type: JavaExec) {
    project.ext 
    {
    gwtDir = "${project.buildDir}/gwt"
    extraDir = "${project.buildDir}/extra"
    gwtModuleName = 'MyModuleName'
    }
    inputs.source sourceSets.main.java.srcDirs
    inputs.dir sourceSets.main.output.resourcesDir
    outputs.dir project.gwtDir

    // Workaround for incremental build (GRADLE-1483)
    outputs.upToDateSpec = new org.gradle.api.specs.AndSpec()

    doFirst
    {
    file(project.gwtDir).mkdirs()
    }

    main = 'com.google.gwt.dev.Compiler'

    classpath
    {
    [
        sourceSets.main.java.srcDirs,           // Java source
        sourceSets.main.output.resourcesDir,    // Generated resources
        sourceSets.main.output.classesDir,      // Generated classes
        sourceSets.main.compileClasspath,       // Deps
    ]
    }

    args =
    [
    project.gwtModuleName, // Your GWT module
    '-war', project.gwtDir,
    '-logLevel', 'INFO',
    '-localWorkers', '2',
    '-compileReport',
    '-extra', project.extraDir,
    ]

    maxHeapSize = '256M'
}

buildscript 
{
    repositories 
    {
    mavenLocal()
    }
}

sourceSets 
{
    main 
    {
    resources 
    {           srcDir('src/main/java').include('**/client/**').include('**/public/**').include('**/*.gwt.xml')
    }
    }
}

war.dependsOn(compileGwt)
war 
{
    def gwtOutputTree = project.fileTree(project.gwtDir)
    from(gwtOutputTree)
    baseName = artifactId
}

task classesJar(type: Jar) {
    dependsOn(classes)
    baseName = artifactId
}

artifacts 
{
    archives war, classesJar
}

def localRepoURL = 'file://' + new File(System.getProperty('user.home'), '.m2/repository').absolutePath

// NOTE: this project will publish to the local Maven repo.
uploadArchives 
{
    repositories 
    {
    mavenDeployer 
    {
        repository(url: localRepoURL)

        addFilter('war') 
        { 
            artifact, file -> artifact.ext == 'war'
        }
    }
    }
}

【问题讨论】:

  • 提示:不要硬编码本地 repo url:repository(url: mavenLocal().url)

标签: gradle multi-project


【解决方案1】:

uploadArchives 并不打算用于安装到本地 Maven 存储库。相反,请使用 install 任务。

【讨论】:

  • 在 AllArms 项目中,我运行了“gradle build install”,但它没有发布/上传/安装 LeftArm 和 RightArm jar。还有其他建议吗?感谢您的回复。
  • 您需要为要安装的每个项目运行install 任务。通常,这是通过为父项目运行 gradle install 来实现的。顺便说一句,compile group: group, name: 'LeftArm', version:version 没有意义,因为已经有一个项目依赖于LeftArmRightArm 也一样。另请注意,发布到本地 Maven 存储库并声明 mavenLocal() 的唯一充分理由是与 Maven 构建交换工件。
  • 我同意你的编译行。我最初将它作为复制/粘贴残余物放在那儿,然后发现依赖项目未安装到本地 Maven 存储库的问题。我需要更新本地 Maven repo b/c,当他们使用 Maven 在他们的环境中构建我们的代码时,repo 将被 tar 并发送给我的客户。我确实有 settings.gradle 来定义所有 Gradle 项目。在每个项目上运行 gradle install 的问题是我们将连接到 Jenkins 自动构建,这将在最顶层的项目(而不是每个单独的子项目)开始构建。
  • 你是说我无法在顶级项目上运行 gradle 命令,将所有子/依赖项目上传/发布到我的本地 Maven 存储库?
  • 安装任务不相互依赖,但如果您为根项目运行gradle install,则将运行所有三个子项目的install 任务。此功能称为名称匹配执行
猜你喜欢
  • 2020-10-18
  • 2017-11-29
  • 2015-05-13
  • 1970-01-01
  • 1970-01-01
  • 2013-01-30
  • 2012-08-20
  • 2018-01-23
  • 1970-01-01
相关资源
最近更新 更多