mark-vieira / gradle-maven-settings-plugin

Gradle Maven settings plugin
Apache License 2.0
60 stars 27 forks source link

Add more info or debug messages for applyRepoCredentials and addCredentials #5

Open sslavic opened 8 years ago

sslavic commented 8 years ago

Is there a better way already to debug, which repositories in build.gradle were discovered, and whether matching server credentials in settings.xml have been found and applied?

It doesn't work for me, I'm getting 401, although credentials are configured correctly in settings.xml, server id and repository name match. I cannot see even with debug if credentials are actually being sent, will have to use wireshark or something.

It's a multi-module project. I want to exclude root project and extra specific project from publishing. In build.gradle there is:

plugins {
    id 'java'
    id 'net.linguica.maven-settings' version '0.5'
}

configure(subprojects - project(':foo')) {
    apply plugin: 'maven-publish'
    publishing {
        String repo = version.endsWith('SNAPSHOT') ? 'snapshots' : 'releases'
        repositories {
            maven {
                name repo
                url "https://repos.bar.com/${repo}"
            }
        }

        publications {
            mavenJava(MavenPublication) {
                from components.java
            }
        }
    }
}
sslavic commented 8 years ago

Custom modified plugin with extra info messages helped me troubleshoot the problem. I had to apply maven-settings plugin to subprojects.

Here is the diff of custom changes:

diff --git a/src/main/groovy/net/linguica/gradle/maven/settings/MavenSettingsPlugin.groovy b/src/main/groovy/net/linguica/gradle/maven/settings/MavenSettingsPlugin.groovy
index bc45f2b..37c7beb 100644
--- a/src/main/groovy/net/linguica/gradle/maven/settings/MavenSettingsPlugin.groovy
+++ b/src/main/groovy/net/linguica/gradle/maven/settings/MavenSettingsPlugin.groovy
@@ -58,8 +58,8 @@ public class MavenSettingsPlugin implements Plugin<Project> {
             loadSettings(project, extension)
             activateProfiles(project, extension)
             registerMirrors(project)
-            applyRepoCredentials(project.repositories)
-            applyRepoCredentials(project.extensions.findByType(PublishingExtension)?.repositories)
+            applyRepoCredentials(project, project.repositories)
+            applyRepoCredentials(project, project.extensions.findByType(PublishingExtension)?.repositories)
         }
     }

@@ -132,16 +132,24 @@ public class MavenSettingsPlugin implements Plugin<Project> {
         }
     }

-    private void applyRepoCredentials(@Nullable Collection<ArtifactRepository> repositories) {
+    private void applyRepoCredentials(Project project, @Nullable Collection<ArtifactRepository> repositories) {
         repositories?.all { repo ->
+            project.logger.info "Discovered repository '${repo.name}'"
             if (repo instanceof MavenArtifactRepository) {
                 settings.servers.each { server ->
                     if (repo.name == server.id) {
-                        addCredentials(server, repo)
+                        project.logger.info "Found a matching server '${server.id}' in settings.xml"
+                        addCredentials(project, server, repo)
                     }
                 }
+            } else {
+                project.logger.info "Repository '${repo.name}' is not a MavenArtifactRepository"
             }
         }
+
+        if (repositories == null || repositories.isEmpty()) {
+            project.logger.info "No repositories found for project '${project.name}'"
+        }
     }

     private void createMirrorRepository(Project project, Mirror mirror) {
@@ -169,12 +177,15 @@ public class MavenSettingsPlugin implements Plugin<Project> {
         }
     }

-    private addCredentials(Server server, MavenArtifactRepository repo) {
+    private addCredentials(Project project, Server server, MavenArtifactRepository repo) {
         if (server?.username != null && server?.password != null) {
             repo.credentials {
                 username = server.username
                 password = server.password
             }
+            project.logger.info "Applied server '${server.id}' credentials to ${repo.name} repository"
+        } else {
+            project.logger.info "No valid credentials found for '${server.id}' in settings.xml, username and/or password is missing"
         }
     }
 }
mark-vieira commented 8 years ago

Some additional logging seems like a no-brainer to me. I'd appreciate a pull request for this if you wouldn't mind.