Easily publishing 3rd party sources to my own artifact server


(Raman Gupta) #1

I often build 3rd party projects locally and then wish to deploy the results into a local artifact server like Nexus, so that the build artifacts can be shared across my team as well as our CI systems. This is easy to do with Maven, without having to modify the actual build file, by using command-line parameters which override the settings in the build file, since those will generally only be usable by the project owner. For example, regardless of the settings in the POM, I can publish to my own repo via:

mvn \
  -Dgpg.skip \
  -DaltDeploymentRepository=$REPO_ID::default::$REPO_URL \
  source:jar javadoc:jar deploy

With Gradle, it seems like I have to edit the build file to achieve a similar result. Is this right?


(Chris Doré) #2

You can inject your own configuration code into the gradle build from the command line by using an init script (-I).

Example of adding a custom repository to projects that use the maven-publish plugin and disable publications to any other repositories:

gradle -I overriderepo.gradle publish

build.gradle:

apply plugin: 'java'
apply plugin: 'maven-publish'
group = 'com.some.company'
publishing {
    publications {
        java( MavenPublication ) {
            from components.java
        }
    }
    repositories {
        maven {
            name = 'somewhere'
            url = 'http://somewhere'
        }
    }
}

overriderepo.gradle:

def repoName = 'my-custom-repo'
def repoUrl = 'http://my.custom.repo'
allprojects {
    pluginManager.withPlugin( 'maven-publish' ) {
        publishing {
            repositories {
                maven {
                    name = repoName
                    url = repoUrl
                }
            }
        }
        tasks.withType( PublishToMavenRepository ) {
            onlyIf {
                repository.name == repoName
            }
        }
    }
}

(Chris Doré) #3

You can also have init scripts applied to all projects automatically by putting them in ~/.gradle/init.d. There’s a couple other places as well https://docs.gradle.org/current/userguide/init_scripts.html#sec:using_an_init_script


(Raman Gupta) #4

Thanks @Chris_Dore – what is the easiest way to combine the two approaches? IOW, have a script in init.d, but that is only enabled if a particular property is set, for example? That way, the script doesn’t always initialize when it doesn’t have to, but I can enable it at any time.


(Chris Doré) #5

You could just wrap all the code with a property check and then add -PdoRepoOverride to the commandline when needed.

def repoName = 'my-custom-repo'
def repoUrl = 'http://my.custom.repo'
allprojects {
    if( project.hasProperty( 'doRepoOverride' ) ) {
        pluginManager.withPlugin( 'maven-publish' ) {
            publishing {
                repositories {
                    maven {
                        name = repoName
                        url = repoUrl
                    }
                }
            }
            tasks.withType( PublishToMavenRepository ) {
                onlyIf {
                    repository.name == repoName
                }
            }
        }
    }
}