xmvn/0003-Implement-Java-home-resolver.patch
2013-06-05 18:11:56 +02:00

210 lines
8.5 KiB
Diff

From 4114b3d2fd431c9fe82c4a0da1b44dafcf2f9c9c Mon Sep 17 00:00:00 2001
From: Mikolaj Izdebski <mizdebsk@redhat.com>
Date: Wed, 5 Jun 2013 15:36:57 +0200
Subject: [PATCH 3/3] Implement Java home resolver
---
.../maven/resolver/DefaultResolver.java | 1 +
.../fedoraproject/maven/resolver/DepmapReader.java | 2 +-
.../maven/resolver/JavaHomeResolver.java | 62 ++++++++++++++++++++
.../maven/resolver/JavaHomeResolverTest.java | 68 ++++++++++++++++++++++
.../tools-jar-depmap.xml | 17 ++++++
5 files changed, 149 insertions(+), 1 deletion(-)
create mode 100644 xmvn-core/src/main/java/org/fedoraproject/maven/resolver/JavaHomeResolver.java
create mode 100644 xmvn-core/src/test/java/org/fedoraproject/maven/resolver/JavaHomeResolverTest.java
create mode 100644 xmvn-core/src/test/resources/java-home-resolver-depmaps/tools-jar-depmap.xml
diff --git a/xmvn-core/src/main/java/org/fedoraproject/maven/resolver/DefaultResolver.java b/xmvn-core/src/main/java/org/fedoraproject/maven/resolver/DefaultResolver.java
index c6a82ad..89ba190 100644
--- a/xmvn-core/src/main/java/org/fedoraproject/maven/resolver/DefaultResolver.java
+++ b/xmvn-core/src/main/java/org/fedoraproject/maven/resolver/DefaultResolver.java
@@ -100,6 +100,7 @@ public class DefaultResolver
File root = new File( prefix );
if ( root.isDirectory() )
{
+ resolvers.add( new JavaHomeResolver( root, settings, logger ) );
Resolver resolver = new SystemResolver( root, settings, logger );
resolvers.add( new CachingResolver( resolver, logger ) );
}
diff --git a/xmvn-core/src/main/java/org/fedoraproject/maven/resolver/DepmapReader.java b/xmvn-core/src/main/java/org/fedoraproject/maven/resolver/DepmapReader.java
index 69398d7..a69ec5d 100644
--- a/xmvn-core/src/main/java/org/fedoraproject/maven/resolver/DepmapReader.java
+++ b/xmvn-core/src/main/java/org/fedoraproject/maven/resolver/DepmapReader.java
@@ -107,7 +107,7 @@ class DepmapReader
{
for ( String path : settings.getMetadataRepositories() )
{
- File file = new File( path );
+ File file = root.toPath().resolve( path ).toFile();
if ( file.isDirectory() )
{
diff --git a/xmvn-core/src/main/java/org/fedoraproject/maven/resolver/JavaHomeResolver.java b/xmvn-core/src/main/java/org/fedoraproject/maven/resolver/JavaHomeResolver.java
new file mode 100644
index 0000000..964b6fa
--- /dev/null
+++ b/xmvn-core/src/main/java/org/fedoraproject/maven/resolver/JavaHomeResolver.java
@@ -0,0 +1,62 @@
+/*-
+ * Copyright (c) 2013 Red Hat, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.fedoraproject.maven.resolver;
+
+import static org.fedoraproject.maven.utils.FileUtils.followSymlink;
+
+import java.io.File;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import org.codehaus.plexus.logging.Logger;
+import org.fedoraproject.maven.config.ResolverSettings;
+import org.fedoraproject.maven.model.Artifact;
+
+/**
+ * @author Mikolaj Izdebski
+ */
+class JavaHomeResolver
+ extends AbstractResolver
+{
+ private final DependencyMap depmap;
+
+ public JavaHomeResolver( File root, ResolverSettings settings, Logger logger )
+ {
+ depmap = DepmapReader.readArtifactMap( root, settings, logger );
+ }
+
+ @Override
+ public File resolve( Artifact requestedArtifact )
+ {
+ for ( Artifact artifact : depmap.translate( requestedArtifact.clearVersionAndExtension() ) )
+ {
+ artifact = artifact.copyMissing( requestedArtifact );
+ String javaHome = System.getProperty( "java.home" );
+
+ if ( artifact.getGroupId().equals( "JAVA_HOME" ) && javaHome != null )
+ {
+ Path javaHomeDir = followSymlink( new File( javaHome ) ).toPath();
+ Path artifactPath = Paths.get( artifact.getArtifactId() + "." + artifact.getExtension() );
+ File artifactFile = javaHomeDir.resolve( artifactPath ).toFile();
+ artifactFile = followSymlink( artifactFile );
+ if ( artifactFile.exists() )
+ return artifactFile;
+ }
+ }
+
+ return null;
+ }
+}
diff --git a/xmvn-core/src/test/java/org/fedoraproject/maven/resolver/JavaHomeResolverTest.java b/xmvn-core/src/test/java/org/fedoraproject/maven/resolver/JavaHomeResolverTest.java
new file mode 100644
index 0000000..ff5215d
--- /dev/null
+++ b/xmvn-core/src/test/java/org/fedoraproject/maven/resolver/JavaHomeResolverTest.java
@@ -0,0 +1,68 @@
+/*-
+ * Copyright (c) 2013 Red Hat, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.fedoraproject.maven.resolver;
+
+import java.io.File;
+
+import org.codehaus.plexus.PlexusTestCase;
+import org.codehaus.plexus.logging.Logger;
+import org.fedoraproject.maven.config.ResolverSettings;
+import org.fedoraproject.maven.model.Artifact;
+import org.fedoraproject.maven.utils.FileUtils;
+
+/**
+ * @author Mikolaj Izdebski
+ */
+public class JavaHomeResolverTest
+ extends PlexusTestCase
+{
+ /**
+ * Test if artifacts are resolved correctly from Java home.
+ *
+ * @throws Exception
+ */
+ public void testJavaHomeResolver()
+ throws Exception
+ {
+ ResolverSettings settings = new ResolverSettings();
+ assertTrue( settings.getPrefixes().isEmpty() );
+ assertTrue( settings.getMetadataRepositories().isEmpty() );
+ settings.addPrefix( FileUtils.CWD.getAbsolutePath() );
+ settings.addMetadataRepository( "src/test/resources/java-home-resolver-depmaps" );
+
+ File root = new File( settings.getPrefixes().iterator().next() );
+ assertTrue( root.isDirectory() );
+ JavaHomeResolver javaHomeResolver = new JavaHomeResolver( root, settings, lookup( Logger.class ) );
+
+ Artifact comSunToolsArtifact = new Artifact( "com.sun", "tools", "SYSTEM", "jar" );
+ File comSunToolsFile = javaHomeResolver.resolve( comSunToolsArtifact );
+ assertNotNull( comSunToolsFile );
+ assertTrue( comSunToolsFile.exists() );
+
+ Artifact toolsArtifact = new Artifact( "JAVA_HOME", "../lib/tools", "SYSTEM", "jar" );
+ File toolsFile = javaHomeResolver.resolve( toolsArtifact );
+ assertNotNull( toolsFile );
+ assertTrue( toolsFile.exists() );
+
+ Artifact toolzArtifact = new Artifact( "JAVA_HOME", "../lib/toolz", "SYSTEM", "jar" );
+ File toolzFile = javaHomeResolver.resolve( toolzArtifact );
+ assertNull( toolzFile );
+
+ Artifact xpp3Artifact = new Artifact( "JPP/xpp3", "xpp3", "SYSTEM", "jar" );
+ File xpp3File = javaHomeResolver.resolve( xpp3Artifact );
+ assertNull( xpp3File );
+ }
+}
diff --git a/xmvn-core/src/test/resources/java-home-resolver-depmaps/tools-jar-depmap.xml b/xmvn-core/src/test/resources/java-home-resolver-depmaps/tools-jar-depmap.xml
new file mode 100644
index 0000000..86b85c4
--- /dev/null
+++ b/xmvn-core/src/test/resources/java-home-resolver-depmaps/tools-jar-depmap.xml
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="US-ASCII"?>
+<!-- Dependency map for com.sun:tools so that packages don't
+ need to use system scope and provide systemPath. -->
+<dependencyMap>
+ <dependency>
+ <maven>
+ <groupId>com.sun</groupId>
+ <artifactId>tools</artifactId>
+ <version>SYSTEM</version>
+ </maven>
+ <jpp>
+ <groupId>JAVA_HOME</groupId>
+ <artifactId>../lib/tools</artifactId>
+ <version>SYSTEM</version>
+ </jpp>
+ </dependency>
+</dependencyMap>
--
1.8.1.4