Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #12 from green-coder/issue-10
Code cleanup commit built on the top of the pull request from udat.
  • Loading branch information
sebersole committed Apr 11, 2018
2 parents 8a98506 + 06a2320 commit 296f1fa
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -36,7 +36,7 @@ Support for the new Gradle Publication DSL
------------------------------------------

One of the new (and still ongoing and incubating) developments in Gradle is the new Publication DSL as the means for
declaring how your project artifacts are pulished. See the Gradle UserGuide for more information.
declaring how your project artifacts are published. See the Gradle UserGuide for more information.

The "bridge" between Maven authentication credentials and Gradle is the repository id. For an example, lets assume
you have a Maven settings.xml that defines the following credentials:
Expand Down
10 changes: 6 additions & 4 deletions build.gradle
Expand Up @@ -28,8 +28,6 @@ dependencies {
compile 'dom4j:dom4j:1.6.1@jar'
compile 'org.sonatype.plexus:plexus-sec-dispatcher:1.3'
runtime 'org.codehaus.plexus:plexus-container-default:1.0.0'

groovy localGroovy()
}

idea {
Expand Down Expand Up @@ -62,10 +60,10 @@ task sourceJar(type: Jar, dependsOn: compileJava) {

// This is the kind of malarkey this plugin tries to address ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if ( ! hasProperty( "JBOSS_REPO_USER" ) ) {
JBOSS_REPO_USER = "";
ext.JBOSS_REPO_USER = "";
}
if ( ! hasProperty( "JBOSS_REPO_PASS" ) ) {
JBOSS_REPO_PASS = "";
ext.JBOSS_REPO_PASS = "";
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -130,3 +128,7 @@ publishing {
task wrapper(type: Wrapper) {
gradleVersion = '1.5'
}

tasks.withType(Compile) {
options.compilerArgs << "-Xlint:unchecked"
}
Expand Up @@ -43,6 +43,6 @@ public void apply(final Project project) {
legacyHandler.applyTo( project );

final RepositoryAuthenticationHandler repositoryHandler = new RepositoryAuthenticationHandler( registry );
repositoryHandler.applyTo(project);
repositoryHandler.applyTo( project );
}
}
Expand Up @@ -54,7 +54,7 @@ public void applyTo(Project project) {
project.getTasks().withType( Upload.class ).all(
new Action<Upload>() {
@Override
@SuppressWarnings( {"unchecked"})
@SuppressWarnings( {"unchecked"} )
public void execute(final Upload uploadTask) {
if ( ! uploadTask.getRepositories().withType( MavenDeployer.class ).isEmpty() ) {
uploadTask.doFirst( authAction );
Expand All @@ -68,6 +68,7 @@ public void execute(final Upload uploadTask) {
public void execute(Upload upload) {
upload.getRepositories().withType( MavenDeployer.class ).all(
new Action<MavenDeployer>() {
@Override
public void execute(MavenDeployer deployer) {
final Object repositoryDelegate = deployer.getRepository();
if ( repositoryDelegate != null ) {
Expand Down Expand Up @@ -206,6 +207,7 @@ public Method idGetterMethod() {
return idGetterMethod;
}

@SuppressWarnings( {"unchecked"} )
private Method locateIdGetterMethod() {
try {
return getRemoteRepositoryClass().getMethod( "getId" );
Expand All @@ -224,6 +226,7 @@ public Method urlGetterMethod() {
return urlGetterMethod;
}

@SuppressWarnings( {"unchecked"} )
private Method locateUrlGetterMethod() {
try {
return getRemoteRepositoryClass().getMethod( "getUrl" );
Expand All @@ -242,6 +245,7 @@ private Method authenticationAdderMethod() {
return addAuthenticationMethod;
}

@SuppressWarnings( {"unchecked"} )
private Method locateAuthenticationAdderMethod() {
try {
return getRemoteRepositoryClass().getMethod( "addAuthentication", getAuthClass() );
Expand Down Expand Up @@ -345,14 +349,14 @@ private Method locatePassphraseSetter() {
private static final String AUTH_CLASS_NAME = "org.apache.maven.artifact.ant.Authentication";
private Class authClass;

public Class getAuthClass() {
public Class<?> getAuthClass() {
if ( authClass == null ) {
authClass = locateAuthClass();
}
return authClass;
}

private Class locateAuthClass() {
private Class<?> locateAuthClass() {
try {
return classLoader.loadClass( AUTH_CLASS_NAME );
}
Expand Down
Expand Up @@ -26,7 +26,7 @@
import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.artifacts.repositories.MavenArtifactRepository;
import org.gradle.api.plugins.ExtraPropertiesExtension;
import org.gradle.api.artifacts.repositories.PasswordCredentials;
import org.gradle.api.publish.maven.tasks.PublishToMavenRepository;

/**
Expand Down Expand Up @@ -64,8 +64,9 @@ public void execute(PublishToMavenRepository publishToMavenRepository) {
return;
}

publishToMavenRepository.getRepository().getCredentials().setUsername( credentials.getUserName() );
publishToMavenRepository.getRepository().getCredentials().setPassword( credentials.getPassword() );
PasswordCredentials passwordCredentials = publishToMavenRepository.getRepository().getCredentials();
passwordCredentials.setUsername( credentials.getUserName() );
passwordCredentials.setPassword( credentials.getPassword() );
}

private Credentials locateAuthenticationCredentials(String repositoryId) {
Expand Down
@@ -1,9 +1,37 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.build.gradle.publish.auth.maven;

import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.artifacts.repositories.MavenArtifactRepository;
import org.gradle.api.artifacts.repositories.PasswordCredentials;

/**
*
* @author Artur Kotyrba
*/
public class RepositoryAuthenticationHandler implements Action<MavenArtifactRepository> {
private final CredentialsProviderRegistry credentialsProviderRegistry;

Expand All @@ -12,31 +40,29 @@ public RepositoryAuthenticationHandler(CredentialsProviderRegistry credentialsPr
}

public void applyTo(Project project) {
project.getRepositories().withType(MavenArtifactRepository.class).all(this);
project.getRepositories().withType( MavenArtifactRepository.class ).all( this );
}

@Override
public void execute(MavenArtifactRepository mavenArtifactRepository) {
final String id = mavenArtifactRepository.getName();
final Credentials credentials = locateAuthenticationCredentials(id);

if (credentials == null) {
final Credentials credentials = locateAuthenticationCredentials( id );
if ( credentials == null ) {
return;
}

mavenArtifactRepository.getCredentials().setUsername(credentials.getUserName());
mavenArtifactRepository.getCredentials().setPassword(credentials.getPassword());
PasswordCredentials passwordCredentials = mavenArtifactRepository.getCredentials();
passwordCredentials.setUsername( credentials.getUserName() );
passwordCredentials.setPassword( credentials.getPassword() );
}

private Credentials locateAuthenticationCredentials(String repositoryId) {
for (CredentialsProvider provider : credentialsProviderRegistry.providers()) {
Credentials authentication = provider.determineAuthentication(repositoryId);

if (authentication != null) {
for ( CredentialsProvider provider : credentialsProviderRegistry.providers() ) {
Credentials authentication = provider.determineAuthentication( repositoryId );
if ( authentication != null ) {
return authentication;
}
}

return null;
}
}

0 comments on commit 296f1fa

Please sign in to comment.