我可以向gradle.properties添加自定义存储库吗?

我可以向gradle.properties添加自定义存储库吗?,gradle,Gradle,我希望能够在设置中定义存储库(最好是用户gradle.properties) 最终目标如下: repositories { mavenCentral() // Can't/don't want to use this nexusCentral() // Can use these - on network Nexus server nexusSnapshot() } maven { credentials {

我希望能够在设置中定义存储库(最好是用户gradle.properties)

最终目标如下:

repositories {
    mavenCentral() // Can't/don't want to use this
    nexusCentral() // Can use these - on network Nexus server
    nexusSnapshot()
}
   maven {
            credentials {
                username getCredentialsMavenUsername()
                password getCredentialsMavenPassword()
            }
            url 'xxxxx'
   }

/**
 * Returns the credential username used by Maven repository
 * Set this value in your ~/.gradle/gradle.properties with CREDENTIALS_USERNAME key
 * @return
 */
def getCredentialsMavenUsername() {
    return hasProperty('CREDENTIALS_USERNAME') ? CREDENTIALS_USERNAME : ""
}

/**
 * Returns the credential password used by Maven repository
 * Set this value in your ~/.gradle/gradle.properties with CREDENTIALS_PASSWORD key
 * @return
 */
def getCredentialsMavenPassword() {
    return hasProperty('CREDENTIALS_PASSWORD') ? CREDENTIALS_PASSWORD : ""
}
我该怎么做呢?同样,这最好放在用户级的gradle.properties文件中,所以我们不必在每个模块中引用它

这只是maven提供的一个普通maven风格的工件库,手动方式是:

maven {
        url "http://path/to/nexus"
    }
另一个要求是使用“发布”任务,该任务具有为存储库定义的凭据(Jenkins用于发布模块):


普通用户不知道这些凭据,但最好在Jenkin的gradle.properties中配置。我们不希望用户生成失败,因为他们无法解析凭据-他们甚至不会使用“发布”任务。

不确定这是否回答了您的问题,但您可以将其放在gradle.properties文件中:

nexusUrl=http://path/to/nexus
并在build.gradle中执行此操作:

maven {
    url project.property(nexusUrl)
}
编辑:

关于你的证书,你所需要的只是

if (project.hasProperty('uploaderUser') && project.hasProperty('uploaderPassword')) {
    credentials {
        username = project.property('uploaderUser')
        password = project.property('uploaderPassword')
    }
}

不确定这是否回答了您的问题,但您可以将其放在gradle.properties文件中:

nexusUrl=http://path/to/nexus
并在build.gradle中执行此操作:

maven {
    url project.property(nexusUrl)
}
编辑:

关于你的证书,你所需要的只是

if (project.hasProperty('uploaderUser') && project.hasProperty('uploaderPassword')) {
    credentials {
        username = project.property('uploaderUser')
        password = project.property('uploaderPassword')
    }
}

您可以使用以下内容:

repositories {
    mavenCentral() // Can't/don't want to use this
    nexusCentral() // Can use these - on network Nexus server
    nexusSnapshot()
}
   maven {
            credentials {
                username getCredentialsMavenUsername()
                password getCredentialsMavenPassword()
            }
            url 'xxxxx'
   }

/**
 * Returns the credential username used by Maven repository
 * Set this value in your ~/.gradle/gradle.properties with CREDENTIALS_USERNAME key
 * @return
 */
def getCredentialsMavenUsername() {
    return hasProperty('CREDENTIALS_USERNAME') ? CREDENTIALS_USERNAME : ""
}

/**
 * Returns the credential password used by Maven repository
 * Set this value in your ~/.gradle/gradle.properties with CREDENTIALS_PASSWORD key
 * @return
 */
def getCredentialsMavenPassword() {
    return hasProperty('CREDENTIALS_PASSWORD') ? CREDENTIALS_PASSWORD : ""
}

如果用户没有凭据,脚本不会失败。

您可以使用以下内容:

repositories {
    mavenCentral() // Can't/don't want to use this
    nexusCentral() // Can use these - on network Nexus server
    nexusSnapshot()
}
   maven {
            credentials {
                username getCredentialsMavenUsername()
                password getCredentialsMavenPassword()
            }
            url 'xxxxx'
   }

/**
 * Returns the credential username used by Maven repository
 * Set this value in your ~/.gradle/gradle.properties with CREDENTIALS_USERNAME key
 * @return
 */
def getCredentialsMavenUsername() {
    return hasProperty('CREDENTIALS_USERNAME') ? CREDENTIALS_USERNAME : ""
}

/**
 * Returns the credential password used by Maven repository
 * Set this value in your ~/.gradle/gradle.properties with CREDENTIALS_PASSWORD key
 * @return
 */
def getCredentialsMavenPassword() {
    return hasProperty('CREDENTIALS_PASSWORD') ? CREDENTIALS_PASSWORD : ""
}

如果用户没有凭据,脚本不会失败。

通过在buildscript和所有项目下将Project/andoird/build.gradle中的jcenter()替换为maven{url'}解决了此问题:

buildscript {
    repositories {
        google()
        maven { url 'http://nexusUrl' }
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
    }
}

allprojects {
    repositories {
        google()
        maven { url 'http://nexusUrl' }
    }
}
在flatterSDK/packages/flatter_tools/gradle/flatter.gradle中,在buildscript下用maven{url'}替换了jcenter:

buildscript {
    repositories {
        google()
        maven { url 'nexusUrl' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
    }
}

通过将Project/andoird/build.gradle中的jcenter()替换为buildscript和所有项目下的maven{url'}解决了此问题:

buildscript {
    repositories {
        google()
        maven { url 'http://nexusUrl' }
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
    }
}

allprojects {
    repositories {
        google()
        maven { url 'http://nexusUrl' }
    }
}
在flatterSDK/packages/flatter_tools/gradle/flatter.gradle中,在buildscript下用maven{url'}替换了jcenter:

buildscript {
    repositories {
        google()
        maven { url 'nexusUrl' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
    }
}

我认为这应该涵盖大部分内容。剩下的唯一一件事是其中一个存储库设置了凭据(这样Jenkins就可以发布),例如:我希望以这样的方式隐藏所有这些:*Jenkins从属性中获取用户/密码*普通用户甚至从不关心用户/密码(并且在未设置值时不要失败)在原始答案中添加了另一个片段。谢谢我认为这应该涵盖大部分内容。剩下的唯一一件事是其中一个存储库设置了凭据(这样Jenkins就可以发布),例如:我希望以这样的方式隐藏所有这些:*Jenkins从属性中获取用户/密码*普通用户甚至从不关心用户/密码(并且在未设置值时不要失败)在原始答案中添加了另一个片段。谢谢谢谢,这可能就是我的结局。谢谢,这可能就是我的结局。