How can gradle use repository settings from maven's settings.xml to publish artifacts to a repository?

Problem: Want gradle to build (a custom plugin) and publish it to my internal artifact repository without hard coding credentials in my build.gradle file

To do this we need to * have maven settings.xml file configured properly * enable maven-publish * enable maven-publish-auth ** set buildscript dependencies and repositories to fine auth

  • configure maven publish

We enable the two plugins and add auth plugin + repository info to the build script’s environment

apply plugin: 'maven'
apply plugin: 'maven-publish'
apply plugin: 'maven-publish-auth'
  buildscript {
  dependencies {
    classpath 'org.hibernate.build.gradle:gradle-maven-publish-auth:2.0.1'
  }
      repositories {
    maven {
      url "http://repository.jboss.org/nexus/content/groups/public/"
    }
    mavenLocal()
    mavenCentral()
  }
}

Define the publishing section so we can publish and add in the name + url of a repository from the maven settings.xml

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }
  repositories {
  maven {
    name "snapshots"
    url "http://my-private-repository.myco.com/nexus/content/repositories/snapshots"
  }
}

$HOME/.m2/settings.xml

<settings>
  <servers>
    <server>
      <id>snapshots</id>
      <username>mysnapuser</username>
      <password>snap password</password>
    </server>
    ...
  </servers>
</settings>
1 Like