Resolving dynamically added configuration with changing dependency does not work


(Crazyjavahacking) #1

Hi,

I am trying to create a Gradle task that would wait till a dependency appears in Maven repository. This is part of a much more difficult build pipeline. For some reason the following code only asks the Maven repository once and never again, even if the configuration.resolve() is called in loop.

ext {
    CONFIG_NAME = 'log4j_group'
    
    REPO_URL = 'http://repo1.maven.org/maven2/'

    GROUP    = 'log4j'
    NAME     = 'log4j'
    VERSION  = '1.2.18'
    EXT      = 'jar'
    CHANGING = 'true'

    COUNTER = 1
}


task test << {
    while (!checkArtifactIsAvailableInRepository())
        Thread.sleep 3000
}

def checkArtifactIsAvailableInRepository() {
    def configName = CONFIG_NAME + COUNTER++

    createFreshConfiguration(configName)

    addAs3LogicSwfsMavenRepository()

    addDependency(configName)

    try {
        logger.lifecycle "Resolving configuration '$configName'."
        project.configurations[configName].resolve()
        logger.lifecycle "Configuration '$configName' resolved successfully."
        true
    }
    catch (org.gradle.api.artifacts.ResolveException e) {
        logger.lifecycle "Configuration '$configName' not resolved successfully."
        false
    }
}


//== GRADLE MODEL MODIFICATION METHODS ================================================================================
def createFreshConfiguration(String configName) {
    if (project.configurations.findByName(configName) != null) {
        // APOLLO-4663: Once configuration is resolved, it will get into RESOLVED or RESOLVED_WITH_FAILURES state. It will never try to resolve artifacts again
        logger.lifecycle "Removing configuration '$configName'."

        configurations.remove(configurations.findByName(configName))
    }

    logger.lifecycle "Creating configuration '$configName' with 0 caching interval for changing modules."

    project.configurations.create(configName)
    project.configurations.findByName(configName).resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
    project.configurations.findByName(configName).resolutionStrategy.cacheDynamicVersionsFor 0, 'seconds'

    logger.lifecycle "Configuration state: ${project.configurations.getByName(configName).state}"
}

def addAs3LogicSwfsMavenRepository() {
    def repository = project.repositories.find { it.url == new URI(REPO_URL) }

    if (repository != null)
        repositories.remove repository

    logger.lifecycle "Creating Maven repository with URL '$REPO_URL'."
    project.repositories.maven {
        url REPO_URL
    }
}

def addDependency(String configName) {
    if (project.configurations.getByName(configName).state == Configuration.State.UNRESOLVED) {
        logger.lifecycle "Adding dependency [group: '$GROUP', name: '$NAME', version: '$VERSION', ext: '$EXT', changing: '$CHANGING'] to configuration '$configName'."

        project.dependencies.add(configName, [
                group    : GROUP,
                name     : NAME,
                version  : VERSION,
                ext      : EXT,
                changing : CHANGING
        ])
    }
    else
        logger.lifecycle "Not adding dependency as the configuration '$configName' is already resolved."
}