You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by rf...@apache.org on 2015/04/28 22:04:01 UTC

svn commit: r1676605 - in /maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact: deploy/internal/ install/internal/ resolve/ resolve/internal/

Author: rfscholte
Date: Tue Apr 28 20:04:01 2015
New Revision: 1676605

URL: http://svn.apache.org/r1676605
Log:
Add implementations for ArtifactResolver

Added:
    maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/
    maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/ArtifactResolver.java
    maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/ArtifactResolverException.java
    maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/internal/
    maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/internal/DefaultArtifactResolver.java
    maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/internal/Invoker.java
    maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/internal/Maven30ArtifactResolver.java
    maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/internal/Maven31ArtifactResolver.java
Modified:
    maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/deploy/internal/DefaultArtifactDeployer.java
    maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/install/internal/DefaultArtifactInstaller.java

Modified: maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/deploy/internal/DefaultArtifactDeployer.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/deploy/internal/DefaultArtifactDeployer.java?rev=1676605&r1=1676604&r2=1676605&view=diff
==============================================================================
--- maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/deploy/internal/DefaultArtifactDeployer.java (original)
+++ maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/deploy/internal/DefaultArtifactDeployer.java Tue Apr 28 20:04:01 2015
@@ -38,7 +38,7 @@ public class DefaultArtifactDeployer
     implements ArtifactDeployer, Contextualizable
 {
 
-    protected PlexusContainer container;
+    private PlexusContainer container;
     
     public void deploy( ProjectBuildingRequest request, Collection<Artifact> mavenArtifacts )
         throws ArtifactDeployerException

Modified: maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/install/internal/DefaultArtifactInstaller.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/install/internal/DefaultArtifactInstaller.java?rev=1676605&r1=1676604&r2=1676605&view=diff
==============================================================================
--- maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/install/internal/DefaultArtifactInstaller.java (original)
+++ maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/install/internal/DefaultArtifactInstaller.java Tue Apr 28 20:04:01 2015
@@ -39,7 +39,7 @@ public class DefaultArtifactInstaller
     implements ArtifactInstaller, Contextualizable
 {
 
-    protected PlexusContainer container;
+    private PlexusContainer container;
 
     public void install( ProjectBuildingRequest request, Collection<Artifact> mavenArtifacts )
         throws ArtifactInstallerException

Added: maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/ArtifactResolver.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/ArtifactResolver.java?rev=1676605&view=auto
==============================================================================
--- maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/ArtifactResolver.java (added)
+++ maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/ArtifactResolver.java Tue Apr 28 20:04:01 2015
@@ -0,0 +1,33 @@
+package org.apache.maven.shared.artifact.resolve;
+
+import java.util.List;
+
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.project.ProjectBuildingRequest;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+public interface ArtifactResolver
+{
+    public org.apache.maven.artifact.Artifact resolveArtifact( ProjectBuildingRequest buildingRequest,
+                                 org.apache.maven.artifact.Artifact mavenArtifact, List<ArtifactRepository> remoteRepositories )
+        throws ArtifactResolverException;
+
+}

Added: maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/ArtifactResolverException.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/ArtifactResolverException.java?rev=1676605&view=auto
==============================================================================
--- maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/ArtifactResolverException.java (added)
+++ maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/ArtifactResolverException.java Tue Apr 28 20:04:01 2015
@@ -0,0 +1,30 @@
+package org.apache.maven.shared.artifact.resolve;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+public class ArtifactResolverException extends Exception
+{
+
+    public ArtifactResolverException( String message, Exception e )
+    {
+        super( message, e );
+    }
+
+}

Added: maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/internal/DefaultArtifactResolver.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/internal/DefaultArtifactResolver.java?rev=1676605&view=auto
==============================================================================
--- maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/internal/DefaultArtifactResolver.java (added)
+++ maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/internal/DefaultArtifactResolver.java Tue Apr 28 20:04:01 2015
@@ -0,0 +1,96 @@
+package org.apache.maven.shared.artifact.resolve.internal;
+
+import java.util.List;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.project.ProjectBuildingRequest;
+import org.apache.maven.shared.artifact.resolve.ArtifactResolver;
+import org.apache.maven.shared.artifact.resolve.ArtifactResolverException;
+import org.codehaus.plexus.PlexusConstants;
+import org.codehaus.plexus.PlexusContainer;
+import org.codehaus.plexus.component.annotations.Component;
+import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
+import org.codehaus.plexus.context.Context;
+import org.codehaus.plexus.context.ContextException;
+import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+@Component( role = ArtifactResolver.class, hint = "default" )
+public class DefaultArtifactResolver
+    implements ArtifactResolver, Contextualizable
+{
+    private PlexusContainer container;
+
+    public org.apache.maven.artifact.Artifact resolveArtifact( ProjectBuildingRequest buildingRequest,
+                                                               Artifact mavenArtifact,
+                                                               List<ArtifactRepository> remoteRepositories )
+        throws ArtifactResolverException
+    {
+        try
+        {
+            String hint = isMaven31() ? "maven31" : "maven3";
+
+            ArtifactResolver effectiveArtifactResolver = container.lookup( ArtifactResolver.class, hint );
+
+            return effectiveArtifactResolver.resolveArtifact( buildingRequest, mavenArtifact, remoteRepositories );
+        }
+        catch ( ComponentLookupException e )
+        {
+            throw new ArtifactResolverException( e.getMessage(), e );
+        }
+    }
+
+    /**
+     * @return true if the current Maven version is Maven 3.1.
+     */
+    protected static boolean isMaven31()
+    {
+        return canFindCoreClass( "org.eclipse.aether.artifact.Artifact" ); // Maven 3.1 specific
+    }
+
+    private static boolean canFindCoreClass( String className )
+    {
+        try
+        {
+            Thread.currentThread().getContextClassLoader().loadClass( className );
+
+            return true;
+        }
+        catch ( ClassNotFoundException e )
+        {
+            return false;
+        }
+    }
+
+    /**
+     * Injects the Plexus content.
+     *
+     * @param context Plexus context to inject.
+     * @throws ContextException if the PlexusContainer could not be located.
+     */
+    public void contextualize( Context context )
+        throws ContextException
+    {
+        container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
+    }
+
+}

Added: maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/internal/Invoker.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/internal/Invoker.java?rev=1676605&view=auto
==============================================================================
--- maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/internal/Invoker.java (added)
+++ maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/internal/Invoker.java Tue Apr 28 20:04:01 2015
@@ -0,0 +1,105 @@
+package org.apache.maven.shared.artifact.resolve.internal;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import java.lang.reflect.InvocationTargetException;
+
+import org.apache.maven.shared.artifact.resolve.ArtifactResolverException;
+
+/**
+ * Invokes method on objects using reflection.
+ */
+final class Invoker
+{
+    private Invoker()
+    {
+        // do not instantiate
+    }
+
+    public static Object invoke( Object object, String method )
+        throws ArtifactResolverException
+    {
+        return invoke( object.getClass(), object, method );
+    }
+
+    public static Object invoke( Class<?> objectClazz, Object object, String method )
+        throws ArtifactResolverException
+    {
+        try
+        {
+            return objectClazz.getMethod( method ).invoke( object );
+        }
+        catch ( IllegalAccessException e )
+        {
+            throw new ArtifactResolverException( e.getMessage(), e );
+        }
+        catch ( InvocationTargetException e )
+        {
+            throw new ArtifactResolverException( e.getMessage(), e );
+        }
+        catch ( NoSuchMethodException e )
+        {
+            throw new ArtifactResolverException( e.getMessage(), e );
+        }
+    }
+
+    public static Object invoke( Object object, String method, Class<?> argClazz, Object arg )
+        throws ArtifactResolverException
+    {
+        try
+        {
+            final Class<?> objectClazz = object.getClass();
+            return objectClazz.getMethod( method, argClazz ).invoke( object, arg );
+        }
+        catch ( IllegalAccessException e )
+        {
+            throw new ArtifactResolverException( e.getMessage(), e );
+        }
+        catch ( InvocationTargetException e )
+        {
+            throw new ArtifactResolverException( e.getMessage(), e );
+        }
+        catch ( NoSuchMethodException e )
+        {
+            throw new ArtifactResolverException( e.getMessage(), e );
+        }
+    }
+    
+    public static Object invoke( Class<?> objectClazz, String staticMethod, Class<?> argClazz, Object arg )
+                    throws ArtifactResolverException
+    {
+        try
+        {
+            return objectClazz.getMethod( staticMethod, argClazz ).invoke( null, arg );
+        }
+        catch ( IllegalAccessException e )
+        {
+            throw new ArtifactResolverException( e.getMessage(), e );
+        }
+        catch ( InvocationTargetException e )
+        {
+            throw new ArtifactResolverException( e.getMessage(), e );
+        }
+        catch ( NoSuchMethodException e )
+        {
+            throw new ArtifactResolverException( e.getMessage(), e );
+        }
+    }
+}

Added: maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/internal/Maven30ArtifactResolver.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/internal/Maven30ArtifactResolver.java?rev=1676605&view=auto
==============================================================================
--- maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/internal/Maven30ArtifactResolver.java (added)
+++ maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/internal/Maven30ArtifactResolver.java Tue Apr 28 20:04:01 2015
@@ -0,0 +1,79 @@
+package org.apache.maven.shared.artifact.resolve.internal;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import java.util.List;
+
+import org.apache.maven.RepositoryUtils;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.project.ProjectBuildingRequest;
+import org.apache.maven.shared.artifact.resolve.ArtifactResolver;
+import org.apache.maven.shared.artifact.resolve.ArtifactResolverException;
+import org.codehaus.plexus.component.annotations.Component;
+import org.codehaus.plexus.component.annotations.Requirement;
+import org.sonatype.aether.RepositorySystem;
+import org.sonatype.aether.RepositorySystemSession;
+import org.sonatype.aether.artifact.Artifact;
+import org.sonatype.aether.repository.RemoteRepository;
+import org.sonatype.aether.resolution.ArtifactRequest;
+import org.sonatype.aether.resolution.ArtifactResolutionException;
+
+@Component( role = ArtifactResolver.class, hint = "maven3" )
+public class Maven30ArtifactResolver
+    implements ArtifactResolver
+{
+    @Requirement
+    private RepositorySystem repositorySystem;
+
+    public org.apache.maven.artifact.Artifact resolveArtifact( ProjectBuildingRequest buildingRequest,
+                                 org.apache.maven.artifact.Artifact mavenArtifact,
+                                 List<ArtifactRepository> remoteRepositories )
+        throws ArtifactResolverException
+    {
+        ArtifactRequest request = new ArtifactRequest();
+
+        Artifact aetherArtifact =
+            (Artifact) Invoker.invoke( RepositoryUtils.class, "toArtifact", org.apache.maven.artifact.Artifact.class,
+                                       mavenArtifact );
+        request.setArtifact( aetherArtifact );
+
+        List<RemoteRepository> aetherRepositories =
+                        (List<RemoteRepository>) Invoker.invoke( RepositoryUtils.class, "toRepos",
+                                                                 List.class, remoteRepositories );
+        request.setRepositories( aetherRepositories );
+        
+        RepositorySystemSession session =
+            (RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" );
+
+        try
+        {
+            Artifact resolvedArtifact = repositorySystem.resolveArtifact( session, request ).getArtifact();
+            
+            return (org.apache.maven.artifact.Artifact) Invoker.invoke( RepositoryUtils.class, "toArtifact",
+                                                                        Artifact.class, resolvedArtifact );
+        }
+        catch ( ArtifactResolutionException e )
+        {
+            throw new ArtifactResolverException( e.getMessage(), e );
+        }
+
+    }
+
+}

Added: maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/internal/Maven31ArtifactResolver.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/internal/Maven31ArtifactResolver.java?rev=1676605&view=auto
==============================================================================
--- maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/internal/Maven31ArtifactResolver.java (added)
+++ maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/internal/Maven31ArtifactResolver.java Tue Apr 28 20:04:01 2015
@@ -0,0 +1,78 @@
+package org.apache.maven.shared.artifact.resolve.internal;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import java.util.List;
+
+import org.apache.maven.RepositoryUtils;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.project.ProjectBuildingRequest;
+import org.apache.maven.shared.artifact.resolve.ArtifactResolver;
+import org.apache.maven.shared.artifact.resolve.ArtifactResolverException;
+import org.codehaus.plexus.component.annotations.Component;
+import org.codehaus.plexus.component.annotations.Requirement;
+import org.eclipse.aether.RepositorySystem;
+import org.eclipse.aether.RepositorySystemSession;
+import org.eclipse.aether.artifact.Artifact;
+import org.eclipse.aether.resolution.ArtifactRequest;
+import org.eclipse.aether.resolution.ArtifactResolutionException;
+import org.eclipse.aether.repository.RemoteRepository;
+
+@Component( role = ArtifactResolver.class, hint = "maven31" )
+public class Maven31ArtifactResolver
+    implements ArtifactResolver
+{
+    @Requirement
+    private RepositorySystem repositorySystem;
+
+    public org.apache.maven.artifact.Artifact resolveArtifact( ProjectBuildingRequest buildingRequest,
+                                 org.apache.maven.artifact.Artifact mavenArtifact,
+                                 List<ArtifactRepository> remoteRepositories )
+        throws ArtifactResolverException
+    {
+        ArtifactRequest request = new ArtifactRequest();
+
+        Artifact aetherArtifact =
+            (Artifact) Invoker.invoke( RepositoryUtils.class, "toArtifact", org.apache.maven.artifact.Artifact.class,
+                                       mavenArtifact );
+        request.setArtifact( aetherArtifact );
+
+        List<RemoteRepository> aetherRepositories =
+            (List<RemoteRepository>) Invoker.invoke( RepositoryUtils.class, "toRepos",
+                                                     List.class, remoteRepositories );        
+        request.setRepositories( aetherRepositories );
+
+        RepositorySystemSession session =
+            (RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" );
+
+        try
+        {
+            Artifact resolvedArtifact = repositorySystem.resolveArtifact( session, request ).getArtifact();
+            
+            return (org.apache.maven.artifact.Artifact) Invoker.invoke( RepositoryUtils.class, "toArtifact",
+                                                                        Artifact.class, resolvedArtifact );
+        }
+        catch ( ArtifactResolutionException e )
+        {
+            throw new ArtifactResolverException( e.getMessage(), e );
+        }
+    }
+
+}