From 06e4f69b93db55d9642298a4ccf248bd056262b6 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Wed, 13 Oct 2010 19:11:08 -0500 Subject: [PATCH] HHH-5655 - In Gradle build, better account for non-standard local maven repo cache locations --- build.gradle | 4 +- .../gradle/maven/LocalMavenRepoSniffer.java | 122 ++++++++++++++++++ release/release.gradle | 2 - 3 files changed, 125 insertions(+), 3 deletions(-) create mode 100644 buildSrc/src/main/java/org/hibernate/build/gradle/maven/LocalMavenRepoSniffer.java diff --git a/build.gradle b/build.gradle index a20d759f38..0dd035b273 100644 --- a/build.gradle +++ b/build.gradle @@ -2,11 +2,13 @@ apply plugin: 'eclipse' apply plugin: 'idea' allprojects { + apply plugin: org.hibernate.build.gradle.maven.LocalMavenRepoSniffer + repositories { mavenCentral() mavenRepo name: 'jboss-nexus', urls: "https://repository.jboss.org/nexus/content/groups/public/" mavenRepo name: "jboss-snapshots", urls: "http://snapshots.jboss.org/maven2/" - mavenRepo urls: "file://" + System.getProperty('user.home') + "/.m2/repository/" +// mavenRepo urls: "file://" + System.getProperty('user.home') + "/.m2/repository/" } } diff --git a/buildSrc/src/main/java/org/hibernate/build/gradle/maven/LocalMavenRepoSniffer.java b/buildSrc/src/main/java/org/hibernate/build/gradle/maven/LocalMavenRepoSniffer.java new file mode 100644 index 0000000000..b0dff0917b --- /dev/null +++ b/buildSrc/src/main/java/org/hibernate/build/gradle/maven/LocalMavenRepoSniffer.java @@ -0,0 +1,122 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @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.maven; + +import java.io.File; +import java.lang.reflect.Field; +import java.net.MalformedURLException; +import java.util.Collections; + +import org.apache.maven.settings.DefaultMavenSettingsBuilder; +import org.apache.maven.settings.MavenSettingsBuilder; +import org.apache.maven.settings.Settings; +import org.gradle.api.Plugin; +import org.gradle.api.Project; +import org.gradle.api.internal.artifacts.publish.maven.pombuilder.PlexusLoggerAdapter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * TODO : javadoc + * + * @author Steve Ebersole + */ +public class LocalMavenRepoSniffer implements Plugin { + private static final Logger log = LoggerFactory.getLogger( LocalMavenRepoSniffer.class ); + + private final File userHome = new File( System.getProperty( "user.home" ) ); + + @Override + public void apply(Project project) { + File mavenLocal = new File( new File( userHome, ".m2" ), "repository" ); + + File userSettings = new File( new File( System.getProperty( "user.home" ), ".m2" ), "settings.xml" ); + if ( userSettings.exists() ) { + File overriddenMavenLocal = extractMavenLocal( userSettings ); + if ( overriddenMavenLocal != null ) { + mavenLocal = overriddenMavenLocal; + } + } + + try { + project.getLogger().trace( "Adding local maven repo [" + mavenLocal.toString() + "]" ); +// adam: here is where mavenLocal() would come in... + project.getRepositories().mavenRepo( + Collections.singletonMap( "urls", mavenLocal.toURI().toURL().toExternalForm() ) + ); + } + catch ( MalformedURLException e ) { + project.getLogger().warn( "Unable to process local maven repo url", e ); + } + + } + + private static final String USER_HOME_MARKER = "${user.home}/"; + + private File extractMavenLocal(File userSettings) { + Settings settings = extractSettings( userSettings ); + String override = settings.getLocalRepository(); + if ( override != null ) { + override = override.trim(); + if ( override.length() > 0 ) { + // Nice, it does not even handle the interpolation for us, so we'll handle some common cases... + if ( override.startsWith( USER_HOME_MARKER ) ) { + override = userHome.getAbsolutePath() + '/' + override.substring( USER_HOME_MARKER.length() ); + } + return new File( override ); + } + } + return null; + } + + private Settings extractSettings(File userSettings) { + try { + MavenSettingsBuilder builder = buildSettingsBuilder( userSettings ); + return builder.buildSettings(); + } + catch ( Exception e ) { + log.debug( "Unable to build Maven settings : " + e ); + } + return null; + } + + private MavenSettingsBuilder buildSettingsBuilder(File userSettings) throws Exception { + final String userSettingsPath = userSettings.getAbsolutePath(); + + DefaultMavenSettingsBuilder builder = new DefaultMavenSettingsBuilder(); + builder.enableLogging( new PlexusLoggerAdapter( log ) ); + + Field userSettingsPathField = DefaultMavenSettingsBuilder.class.getDeclaredField( "userSettingsPath" ); + userSettingsPathField.setAccessible( true ); + userSettingsPathField.set( builder, userSettingsPath ); + + Field globalSettingsPathField = DefaultMavenSettingsBuilder.class.getDeclaredField( "globalSettingsPath" ); + globalSettingsPathField.setAccessible( true ); + globalSettingsPathField.set( builder, userSettingsPath ); + + builder.initialize(); + + return builder; + } +} diff --git a/release/release.gradle b/release/release.gradle index 7bd2d48cce..f097212816 100644 --- a/release/release.gradle +++ b/release/release.gradle @@ -107,8 +107,6 @@ aggregateJavadocs.doLast { } } -System.out.println("Archives config : " + parent.project( 'hibernate-core' ).configurations.archives.files ); - // release bundles ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ releaseBuildDir = dir( buildDirName )