Author: rfscholte Date: Sat May 9 11:48:46 2015 New Revision: 1678479 URL: http://svn.apache.org/r1678479 Log: Introduce RepositoryManager
Added: maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/ maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/RepositoryManager.java maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/RepositoryManagerException.java maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/internal/ maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/internal/DefaultRepositoryManager.java maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/internal/Invoker.java maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/internal/Maven302RepositoryManager.java maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/internal/Maven30RepositoryManager.java maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/internal/Maven31RepositoryManager.java maven/shared/trunk/maven-artifact-transfer/src/test/java/org/apache/maven/shared/artifact/repository/ maven/shared/trunk/maven-artifact-transfer/src/test/java/org/apache/maven/shared/artifact/repository/internal/ maven/shared/trunk/maven-artifact-transfer/src/test/java/org/apache/maven/shared/artifact/repository/internal/Maven30RepositoryManagerTest.java Removed: maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/install/internal/Maven302ArtifactInstaller.java Modified: maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/install/ArtifactInstaller.java maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/install/internal/DefaultArtifactInstaller.java maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/install/internal/Maven30ArtifactInstaller.java maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/install/internal/Maven31ArtifactInstaller.java maven/shared/trunk/maven-artifact-transfer/src/test/java/org/apache/maven/shared/artifact/install/internal/Maven30ArtifactInstallerTest.java Modified: maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/install/ArtifactInstaller.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/install/ArtifactInstaller.java?rev=1678479&r1=1678478&r2=1678479&view=diff ============================================================================== --- maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/install/ArtifactInstaller.java (original) +++ maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/install/ArtifactInstaller.java Sat May 9 11:48:46 2015 @@ -19,7 +19,6 @@ package org.apache.maven.shared.artifact * under the License. */ -import java.io.File; import java.util.Collection; import org.apache.maven.artifact.Artifact; @@ -30,25 +29,4 @@ public interface ArtifactInstaller public void install( ProjectBuildingRequest request, Collection<Artifact> mavenArtifacts ) throws ArtifactInstallerException; - - /** - * Create a new {@code ProjectBuildingRequest} with an adjusted repository session. - * - * @param request the project building request - * @param basedir the base directory of the local repository - * @return a new project building request - * @throws ArtifactInstallerException if an exception occurs - */ - public ProjectBuildingRequest setLocalRepositoryBasedir( ProjectBuildingRequest request, File basedir ) - throws ArtifactInstallerException; - - /** - * Get the localRepositryBasedir as specified in the repository session of the request - * - * @param request the build request - * @return the local repository base directory - * @throws ArtifactInstallerException if an exception occurs - */ - public File getLocalRepositoryBasedir( ProjectBuildingRequest request ) - throws ArtifactInstallerException; } 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=1678479&r1=1678478&r2=1678479&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 Sat May 9 11:48:46 2015 @@ -19,7 +19,6 @@ package org.apache.maven.shared.artifact * under the License. */ -import java.io.File; import java.util.Collection; import org.apache.maven.artifact.Artifact; @@ -58,40 +57,6 @@ public class DefaultArtifactInstaller } } - public ProjectBuildingRequest setLocalRepositoryBasedir( ProjectBuildingRequest request, File basedir ) - throws ArtifactInstallerException - { - try - { - String hint = isMaven31() ? "maven31" : isMaven302() ? "maven302" : "maven3"; - - ArtifactInstaller effectiveArtifactInstaller = container.lookup( ArtifactInstaller.class, hint ); - - return effectiveArtifactInstaller.setLocalRepositoryBasedir( request, basedir ); - } - catch ( ComponentLookupException e ) - { - throw new ArtifactInstallerException( e.getMessage(), e ); - } - } - - public File getLocalRepositoryBasedir( ProjectBuildingRequest request ) - throws ArtifactInstallerException - { - try - { - String hint = isMaven31() ? "maven31" : isMaven302() ? "maven302" : "maven3"; - - ArtifactInstaller effectiveArtifactInstaller = container.lookup( ArtifactInstaller.class, hint ); - - return effectiveArtifactInstaller.getLocalRepositoryBasedir( request ); - } - catch ( ComponentLookupException e ) - { - throw new ArtifactInstallerException( e.getMessage(), e ); - } - } - /** * @return true if the current Maven version is Maven 3.1. */ @@ -100,14 +65,6 @@ public class DefaultArtifactInstaller return canFindCoreClass( "org.eclipse.aether.artifact.Artifact" ); // Maven 3.1 specific } - /** - * @return true if the current Maven version is Maven 3.0.2 - */ - protected static boolean isMaven302() - { - return canFindCoreClass( "org.sonatype.aether.spi.localrepo.LocalRepositoryManagerFactory" ); - } - private static boolean canFindCoreClass( String className ) { try Modified: maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/install/internal/Maven30ArtifactInstaller.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/install/internal/Maven30ArtifactInstaller.java?rev=1678479&r1=1678478&r2=1678479&view=diff ============================================================================== --- maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/install/internal/Maven30ArtifactInstaller.java (original) +++ maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/install/internal/Maven30ArtifactInstaller.java Sat May 9 11:48:46 2015 @@ -19,13 +19,11 @@ package org.apache.maven.shared.artifact * under the License. */ -import java.io.File; import java.util.Collection; import org.apache.maven.RepositoryUtils; import org.apache.maven.artifact.metadata.ArtifactMetadata; import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata; -import org.apache.maven.project.DefaultProjectBuildingRequest; import org.apache.maven.project.ProjectBuildingRequest; import org.apache.maven.project.artifact.ProjectArtifactMetadata; import org.apache.maven.shared.artifact.install.ArtifactInstaller; @@ -37,10 +35,6 @@ import org.sonatype.aether.RepositorySys import org.sonatype.aether.artifact.Artifact; import org.sonatype.aether.installation.InstallRequest; import org.sonatype.aether.installation.InstallationException; -import org.sonatype.aether.repository.LocalRepository; -import org.sonatype.aether.repository.LocalRepositoryManager; -import org.sonatype.aether.util.DefaultRepositoryCache; -import org.sonatype.aether.util.DefaultRepositorySystemSession; import org.sonatype.aether.util.artifact.SubArtifact; @Component( role = ArtifactInstaller.class, hint = "maven3" ) @@ -98,45 +92,4 @@ public class Maven30ArtifactInstaller throw new ArtifactInstallerException( e.getMessage(), e ); } } - - public ProjectBuildingRequest setLocalRepositoryBasedir( ProjectBuildingRequest buildingRequest, File basedir ) - throws ArtifactInstallerException - { - ProjectBuildingRequest newRequest = new DefaultProjectBuildingRequest( buildingRequest ); - - RepositorySystemSession session = - (RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" ); - - // "clone" session and replace localRepository - DefaultRepositorySystemSession newSession = new DefaultRepositorySystemSession( session ); - - // Clear cache, since we're using a new local repository - newSession.setCache( new DefaultRepositoryCache() ); - - // keep same repositoryType - String repositoryType = resolveRepositoryType( session.getLocalRepository() ); - - LocalRepositoryManager localRepositoryManager = - repositorySystem.newLocalRepositoryManager( new LocalRepository( basedir, repositoryType ) ); - - newSession.setLocalRepositoryManager( localRepositoryManager ); - - Invoker.invoke( newRequest, "setRepositorySession", RepositorySystemSession.class, newSession ); - - return newRequest; - } - - public File getLocalRepositoryBasedir( ProjectBuildingRequest buildingRequest ) - throws ArtifactInstallerException - { - RepositorySystemSession session = - (RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" ); - - return session.getLocalRepository().getBasedir(); - } - - protected String resolveRepositoryType( LocalRepository localRepository ) - { - return localRepository.getContentType(); - } } Modified: maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/install/internal/Maven31ArtifactInstaller.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/install/internal/Maven31ArtifactInstaller.java?rev=1678479&r1=1678478&r2=1678479&view=diff ============================================================================== --- maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/install/internal/Maven31ArtifactInstaller.java (original) +++ maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/install/internal/Maven31ArtifactInstaller.java Sat May 9 11:48:46 2015 @@ -19,28 +19,22 @@ package org.apache.maven.shared.artifact * under the License. */ -import java.io.File; import java.util.Collection; import org.apache.maven.RepositoryUtils; import org.apache.maven.artifact.metadata.ArtifactMetadata; import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata; -import org.apache.maven.project.DefaultProjectBuildingRequest; import org.apache.maven.project.ProjectBuildingRequest; import org.apache.maven.project.artifact.ProjectArtifactMetadata; import org.apache.maven.shared.artifact.install.ArtifactInstaller; import org.apache.maven.shared.artifact.install.ArtifactInstallerException; import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Requirement; -import org.eclipse.aether.DefaultRepositoryCache; -import org.eclipse.aether.DefaultRepositorySystemSession; import org.eclipse.aether.RepositorySystem; import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.artifact.Artifact; import org.eclipse.aether.installation.InstallRequest; import org.eclipse.aether.installation.InstallationException; -import org.eclipse.aether.repository.LocalRepository; -import org.eclipse.aether.repository.LocalRepositoryManager; import org.eclipse.aether.util.artifact.SubArtifact; @Component( role = ArtifactInstaller.class , hint="maven31" ) @@ -98,55 +92,4 @@ public class Maven31ArtifactInstaller im throw new ArtifactInstallerException( e.getMessage(), e ); } } - - public ProjectBuildingRequest setLocalRepositoryBasedir( ProjectBuildingRequest buildingRequest, File basedir ) - throws ArtifactInstallerException - { - ProjectBuildingRequest newRequest = new DefaultProjectBuildingRequest( buildingRequest ); - - RepositorySystemSession session = - (RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" ); - - // "clone" session and replace localRepository - DefaultRepositorySystemSession newSession = new DefaultRepositorySystemSession( session ); - - // Clear cache, since we're using a new local repository - newSession.setCache( new DefaultRepositoryCache() ); - - // keep same repositoryType - String repositoryType = resolveRepositoryType( session.getLocalRepository() ); - - LocalRepositoryManager localRepositoryManager = - repositorySystem.newLocalRepositoryManager( newSession, new LocalRepository( basedir, repositoryType ) ); - - newSession.setLocalRepositoryManager( localRepositoryManager ); - - Invoker.invoke( newRequest, "setRepositorySession", RepositorySystemSession.class, newSession ); - - return newRequest; - } - - public File getLocalRepositoryBasedir( ProjectBuildingRequest buildingRequest ) - throws ArtifactInstallerException - { - RepositorySystemSession session = - (RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" ); - - return session.getLocalRepository().getBasedir(); - } - - protected String resolveRepositoryType( LocalRepository localRepository ) - { - String repositoryType; - if ( "enhanced".equals( localRepository.getContentType() ) ) - { - repositoryType = "default"; - } - else - { - // this should be "simple" - repositoryType = localRepository.getContentType(); - } - return repositoryType; - } } Added: maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/RepositoryManager.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/RepositoryManager.java?rev=1678479&view=auto ============================================================================== --- maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/RepositoryManager.java (added) +++ maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/RepositoryManager.java Sat May 9 11:48:46 2015 @@ -0,0 +1,49 @@ +package org.apache.maven.shared.artifact.repository; + +/* + * 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.io.File; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.project.ProjectBuildingRequest; +import org.apache.maven.shared.artifact.install.ArtifactInstallerException; + +public interface RepositoryManager +{ + + String getPathForLocalArtifact( ProjectBuildingRequest buildingRequest, Artifact artifact ); + + /** + * Create a new {@code ProjectBuildingRequest} with an adjusted repository session. + * + * @param request the project building request + * @param basedir the base directory of the local repository + * @return a new project building request + */ + public ProjectBuildingRequest setLocalRepositoryBasedir( ProjectBuildingRequest request, File basedir ); + + /** + * Get the localRepositryBasedir as specified in the repository session of the request + * + * @param request the build request + * @return the local repository base directory + */ + public File getLocalRepositoryBasedir( ProjectBuildingRequest request ); +} Added: maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/RepositoryManagerException.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/RepositoryManagerException.java?rev=1678479&view=auto ============================================================================== --- maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/RepositoryManagerException.java (added) +++ maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/RepositoryManagerException.java Sat May 9 11:48:46 2015 @@ -0,0 +1,30 @@ +package org.apache.maven.shared.artifact.repository; + +/* + * 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 RepositoryManagerException extends Exception +{ + + public RepositoryManagerException( String message, Throwable cause ) + { + super( message, cause ); + } + +} Added: maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/internal/DefaultRepositoryManager.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/internal/DefaultRepositoryManager.java?rev=1678479&view=auto ============================================================================== --- maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/internal/DefaultRepositoryManager.java (added) +++ maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/internal/DefaultRepositoryManager.java Sat May 9 11:48:46 2015 @@ -0,0 +1,140 @@ +package org.apache.maven.shared.artifact.repository.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.io.File; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.project.ProjectBuildingRequest; +import org.apache.maven.shared.artifact.repository.RepositoryManager; +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; + +@Component( role = RepositoryManager.class ) +public class DefaultRepositoryManager + implements RepositoryManager, Contextualizable +{ + private PlexusContainer container; + + /** + * {@inheritDoc} + */ + public String getPathForLocalArtifact( ProjectBuildingRequest buildingRequest, Artifact artifact ) + { + try + { + String hint = isMaven31() ? "maven31" : "maven3"; + + RepositoryManager effectiveRepositoryManager = container.lookup( RepositoryManager.class, hint ); + + return effectiveRepositoryManager.getPathForLocalArtifact( buildingRequest, artifact ); + } + catch ( ComponentLookupException e ) + { + throw new IllegalStateException( e.getMessage(), e ); + } + + } + + /** + * {@inheritDoc} + */ + public ProjectBuildingRequest setLocalRepositoryBasedir( ProjectBuildingRequest request, File basedir ) + { + try + { + String hint = isMaven31() ? "maven31" : isMaven302() ? "maven302" : "maven3"; + + RepositoryManager effectiveRepositoryManager = container.lookup( RepositoryManager.class, hint ); + + return effectiveRepositoryManager.setLocalRepositoryBasedir( request, basedir ); + } + catch ( ComponentLookupException e ) + { + throw new IllegalStateException( e.getMessage(), e ); + } + } + + /** + * {@inheritDoc} + */ + public File getLocalRepositoryBasedir( ProjectBuildingRequest request ) + { + try + { + String hint = isMaven31() ? "maven31" : isMaven302() ? "maven302" : "maven3"; + + RepositoryManager effectiveRepositoryManager = container.lookup( RepositoryManager.class, hint ); + + return effectiveRepositoryManager.getLocalRepositoryBasedir( request ); + } + catch ( ComponentLookupException e ) + { + throw new IllegalStateException( 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 + } + + /** + * @return true if the current Maven version is Maven 3.0.2 + */ + protected static boolean isMaven302() + { + return canFindCoreClass( "org.sonatype.aether.spi.localrepo.LocalRepositoryManagerFactory" ); + } + + 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/repository/internal/Invoker.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/internal/Invoker.java?rev=1678479&view=auto ============================================================================== --- maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/internal/Invoker.java (added) +++ maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/internal/Invoker.java Sat May 9 11:48:46 2015 @@ -0,0 +1,105 @@ +package org.apache.maven.shared.artifact.repository.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.repository.RepositoryManagerException; + +/** + * Invokes method on objects using reflection. + */ +final class Invoker +{ + private Invoker() + { + // do not instantiate + } + + public static Object invoke( Object object, String method ) + throws RepositoryManagerException + { + return invoke( object.getClass(), object, method ); + } + + public static Object invoke( Class<?> objectClazz, Object object, String method ) + throws RepositoryManagerException + { + try + { + return objectClazz.getMethod( method ).invoke( object ); + } + catch ( IllegalAccessException e ) + { + throw new RepositoryManagerException( e.getMessage(), e ); + } + catch ( InvocationTargetException e ) + { + throw new RepositoryManagerException( e.getMessage(), e ); + } + catch ( NoSuchMethodException e ) + { + throw new RepositoryManagerException( e.getMessage(), e ); + } + } + + public static Object invoke( Object object, String method, Class<?> argClazz, Object arg ) + throws RepositoryManagerException + { + try + { + final Class<?> objectClazz = object.getClass(); + return objectClazz.getMethod( method, argClazz ).invoke( object, arg ); + } + catch ( IllegalAccessException e ) + { + throw new RepositoryManagerException( e.getMessage(), e ); + } + catch ( InvocationTargetException e ) + { + throw new RepositoryManagerException( e.getMessage(), e ); + } + catch ( NoSuchMethodException e ) + { + throw new RepositoryManagerException( e.getMessage(), e ); + } + } + + public static Object invoke( Class<?> objectClazz, String staticMethod, Class<?> argClazz, Object arg ) + throws RepositoryManagerException + { + try + { + return objectClazz.getMethod( staticMethod, argClazz ).invoke( null, arg ); + } + catch ( IllegalAccessException e ) + { + throw new RepositoryManagerException( e.getMessage(), e ); + } + catch ( InvocationTargetException e ) + { + throw new RepositoryManagerException( e.getMessage(), e ); + } + catch ( NoSuchMethodException e ) + { + throw new RepositoryManagerException( e.getMessage(), e ); + } + } +} Added: maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/internal/Maven302RepositoryManager.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/internal/Maven302RepositoryManager.java?rev=1678479&view=auto ============================================================================== --- maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/internal/Maven302RepositoryManager.java (added) +++ maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/internal/Maven302RepositoryManager.java Sat May 9 11:48:46 2015 @@ -0,0 +1,49 @@ +package org.apache.maven.shared.artifact.repository.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 org.apache.maven.shared.artifact.repository.RepositoryManager; +import org.codehaus.plexus.component.annotations.Component; +import org.sonatype.aether.repository.LocalRepository; + +@Component( role = RepositoryManager.class, hint = "maven302" ) +public class Maven302RepositoryManager + extends Maven30RepositoryManager +{ + + /** + * Aether-1.9+ (i.e. M3.0.2+) expects "default", not "enhanced" as repositoryType + */ + @Override + protected String resolveRepositoryType( LocalRepository localRepository ) + { + String repositoryType; + if ( "enhanced".equals( localRepository.getContentType() ) ) + { + repositoryType = "default"; + } + else + { + // this should be "simple" + repositoryType = localRepository.getContentType(); + } + return repositoryType; + } +} Added: maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/internal/Maven30RepositoryManager.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/internal/Maven30RepositoryManager.java?rev=1678479&view=auto ============================================================================== --- maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/internal/Maven30RepositoryManager.java (added) +++ maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/internal/Maven30RepositoryManager.java Sat May 9 11:48:46 2015 @@ -0,0 +1,139 @@ +package org.apache.maven.shared.artifact.repository.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.io.File; + +import org.apache.maven.RepositoryUtils; +import org.apache.maven.project.DefaultProjectBuildingRequest; +import org.apache.maven.project.ProjectBuildingRequest; +import org.apache.maven.shared.artifact.repository.RepositoryManager; +import org.apache.maven.shared.artifact.repository.RepositoryManagerException; +import org.apache.maven.shared.artifact.repository.internal.Invoker; +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.LocalRepository; +import org.sonatype.aether.repository.LocalRepositoryManager; +import org.sonatype.aether.util.DefaultRepositoryCache; +import org.sonatype.aether.util.DefaultRepositorySystemSession; + +@Component( role = RepositoryManager.class, hint = "maven3" ) +public class Maven30RepositoryManager + implements RepositoryManager +{ + + @Requirement + private RepositorySystem repositorySystem; + + /** + * {@inheritDoc} + */ + public String getPathForLocalArtifact( ProjectBuildingRequest buildingRequest, + org.apache.maven.artifact.Artifact mavenArtifact ) + { + Artifact aetherArtifact; + RepositorySystemSession session; + + // LRM.getPathForLocalArtifact() won't throw an Exception, so translate reflection error to RuntimeException + try + { + aetherArtifact = + (Artifact) Invoker.invoke( RepositoryUtils.class, "toArtifact", + org.apache.maven.artifact.Artifact.class, mavenArtifact ); + + session = (RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" ); + } + catch ( RepositoryManagerException e ) + { + throw new RuntimeException( e.getMessage(), e ); + } + + return session.getLocalRepositoryManager().getPathForLocalArtifact( aetherArtifact ); + } + + /** + * {@inheritDoc} + */ + public ProjectBuildingRequest setLocalRepositoryBasedir( ProjectBuildingRequest buildingRequest, File basedir ) + { + ProjectBuildingRequest newRequest = new DefaultProjectBuildingRequest( buildingRequest ); + + RepositorySystemSession session; + try + { + session = (RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" ); + } + catch ( RepositoryManagerException e ) + { + throw new RuntimeException( e.getMessage(), e ); + } + + // "clone" session and replace localRepository + DefaultRepositorySystemSession newSession = new DefaultRepositorySystemSession( session ); + + // Clear cache, since we're using a new local repository + newSession.setCache( new DefaultRepositoryCache() ); + + // keep same repositoryType + String repositoryType = resolveRepositoryType( session.getLocalRepository() ); + + LocalRepositoryManager localRepositoryManager = + repositorySystem.newLocalRepositoryManager( new LocalRepository( basedir, repositoryType ) ); + + newSession.setLocalRepositoryManager( localRepositoryManager ); + + try + { + Invoker.invoke( newRequest, "setRepositorySession", RepositorySystemSession.class, newSession ); + } + catch ( RepositoryManagerException e ) + { + throw new RuntimeException( e.getMessage(), e ); + } + + return newRequest; + } + + /** + * {@inheritDoc} + */ + public File getLocalRepositoryBasedir( ProjectBuildingRequest buildingRequest ) + { + RepositorySystemSession session; + try + { + session = (RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" ); + } + catch ( RepositoryManagerException e ) + { + throw new RuntimeException( e.getMessage(), e ); + } + + return session.getLocalRepository().getBasedir(); + } + + protected String resolveRepositoryType( LocalRepository localRepository ) + { + return localRepository.getContentType(); + } +} Added: maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/internal/Maven31RepositoryManager.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/internal/Maven31RepositoryManager.java?rev=1678479&view=auto ============================================================================== --- maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/internal/Maven31RepositoryManager.java (added) +++ maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/repository/internal/Maven31RepositoryManager.java Sat May 9 11:48:46 2015 @@ -0,0 +1,138 @@ +package org.apache.maven.shared.artifact.repository.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.io.File; + +import org.apache.maven.RepositoryUtils; +import org.apache.maven.project.DefaultProjectBuildingRequest; +import org.apache.maven.project.ProjectBuildingRequest; +import org.apache.maven.shared.artifact.repository.RepositoryManager; +import org.apache.maven.shared.artifact.repository.RepositoryManagerException; +import org.codehaus.plexus.component.annotations.Component; +import org.codehaus.plexus.component.annotations.Requirement; +import org.eclipse.aether.DefaultRepositoryCache; +import org.eclipse.aether.DefaultRepositorySystemSession; +import org.eclipse.aether.RepositorySystem; +import org.eclipse.aether.RepositorySystemSession; +import org.eclipse.aether.artifact.Artifact; +import org.eclipse.aether.repository.LocalRepository; +import org.eclipse.aether.repository.LocalRepositoryManager; + +@Component( role = RepositoryManager.class, hint = "maven31" ) +public class Maven31RepositoryManager + implements RepositoryManager +{ + + @Requirement + private RepositorySystem repositorySystem; + + public String getPathForLocalArtifact( ProjectBuildingRequest buildingRequest, + org.apache.maven.artifact.Artifact mavenArtifact ) + { + Artifact aetherArtifact; + RepositorySystemSession session; + + // LRM.getPathForLocalArtifact() won't throw an Exception, so translate reflection error to RuntimeException + try + { + aetherArtifact = (Artifact) Invoker.invoke( RepositoryUtils.class, "toArtifact", org.apache.maven.artifact.Artifact.class, + mavenArtifact ); + + session = (RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" ); + } + catch ( RepositoryManagerException e ) + { + throw new RuntimeException( e.getMessage(), e ); + } + + return session.getLocalRepositoryManager().getPathForLocalArtifact( aetherArtifact ); + } + + public ProjectBuildingRequest setLocalRepositoryBasedir( ProjectBuildingRequest buildingRequest, File basedir ) + { + ProjectBuildingRequest newRequest = new DefaultProjectBuildingRequest( buildingRequest ); + + RepositorySystemSession session; + try + { + session = (RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" ); + } + catch ( RepositoryManagerException e ) + { + throw new RuntimeException( e.getMessage(), e ); + } + + // "clone" session and replace localRepository + DefaultRepositorySystemSession newSession = new DefaultRepositorySystemSession( session ); + + // Clear cache, since we're using a new local repository + newSession.setCache( new DefaultRepositoryCache() ); + + // keep same repositoryType + String repositoryType = resolveRepositoryType( session.getLocalRepository() ); + + LocalRepositoryManager localRepositoryManager = + repositorySystem.newLocalRepositoryManager( newSession, new LocalRepository( basedir, repositoryType ) ); + + newSession.setLocalRepositoryManager( localRepositoryManager ); + + try + { + Invoker.invoke( newRequest, "setRepositorySession", RepositorySystemSession.class, newSession ); + } + catch ( RepositoryManagerException e ) + { + throw new RuntimeException( e.getMessage(), e ); + } + + return newRequest; + } + + public File getLocalRepositoryBasedir( ProjectBuildingRequest buildingRequest ) + { + RepositorySystemSession session; + try + { + session = (RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" ); + } + catch ( RepositoryManagerException e ) + { + throw new RuntimeException( e.getMessage(), e ); + } + + return session.getLocalRepository().getBasedir(); + } + + protected String resolveRepositoryType( LocalRepository localRepository ) + { + String repositoryType; + if ( "enhanced".equals( localRepository.getContentType() ) ) + { + repositoryType = "default"; + } + else + { + // this should be "simple" + repositoryType = localRepository.getContentType(); + } + return repositoryType; + } +} Modified: maven/shared/trunk/maven-artifact-transfer/src/test/java/org/apache/maven/shared/artifact/install/internal/Maven30ArtifactInstallerTest.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-artifact-transfer/src/test/java/org/apache/maven/shared/artifact/install/internal/Maven30ArtifactInstallerTest.java?rev=1678479&r1=1678478&r2=1678479&view=diff ============================================================================== --- maven/shared/trunk/maven-artifact-transfer/src/test/java/org/apache/maven/shared/artifact/install/internal/Maven30ArtifactInstallerTest.java (original) +++ maven/shared/trunk/maven-artifact-transfer/src/test/java/org/apache/maven/shared/artifact/install/internal/Maven30ArtifactInstallerTest.java Sat May 9 11:48:46 2015 @@ -8,11 +8,9 @@ import org.apache.maven.artifact.Artifac import org.apache.maven.artifact.DefaultArtifact; import org.apache.maven.artifact.handler.DefaultArtifactHandler; import org.apache.maven.project.DefaultProjectBuildingRequest; -import org.apache.maven.project.ProjectBuildingRequest; import org.apache.maven.repository.internal.MavenRepositorySystemSession; import org.apache.maven.shared.artifact.install.ArtifactInstaller; import org.codehaus.plexus.PlexusTestCase; -import org.sonatype.aether.impl.internal.EnhancedLocalRepositoryManager; import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager; public class Maven30ArtifactInstallerTest extends PlexusTestCase @@ -55,35 +53,4 @@ public class Maven30ArtifactInstallerTes assertTrue( new File( localRepo, "GROUPID/ARTIFACTID/VERSION/ARTIFACTID-VERSION-CLASSIFIER.EXTENSION" ).exists() ); assertTrue( new File( localRepo, "GROUPID/ARTIFACTID/maven-metadata-local.xml" ).exists() ); //?? } - - public void testSetLocalRepositoryBasedirSimple() throws Exception - { - DefaultProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession(); - repositorySession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( localRepo ) ); - buildingRequest.setRepositorySession( repositorySession ); - - File basedir = new File( "NEW/LOCAL/REPO" ); - - ProjectBuildingRequest newBuildingRequest = installer.setLocalRepositoryBasedir( buildingRequest, basedir ); - - assertEquals( basedir.getAbsoluteFile(), newBuildingRequest.getRepositorySession().getLocalRepository().getBasedir() ); - - } - - public void testSetLocalRepositoryBasedirEnhanced() throws Exception - { - DefaultProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession(); - repositorySession.setLocalRepositoryManager( new EnhancedLocalRepositoryManager( localRepo ) ); - buildingRequest.setRepositorySession( repositorySession ); - - File basedir = new File( "NEW/LOCAL/REPO" ); - - ProjectBuildingRequest newBuildingRequest = installer.setLocalRepositoryBasedir( buildingRequest, basedir ); - - assertEquals( basedir.getAbsoluteFile(), newBuildingRequest.getRepositorySession().getLocalRepository().getBasedir() ); - - } - } Added: maven/shared/trunk/maven-artifact-transfer/src/test/java/org/apache/maven/shared/artifact/repository/internal/Maven30RepositoryManagerTest.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-artifact-transfer/src/test/java/org/apache/maven/shared/artifact/repository/internal/Maven30RepositoryManagerTest.java?rev=1678479&view=auto ============================================================================== --- maven/shared/trunk/maven-artifact-transfer/src/test/java/org/apache/maven/shared/artifact/repository/internal/Maven30RepositoryManagerTest.java (added) +++ maven/shared/trunk/maven-artifact-transfer/src/test/java/org/apache/maven/shared/artifact/repository/internal/Maven30RepositoryManagerTest.java Sat May 9 11:48:46 2015 @@ -0,0 +1,57 @@ +package org.apache.maven.shared.artifact.repository.internal; + +import java.io.File; + +import org.apache.maven.project.DefaultProjectBuildingRequest; +import org.apache.maven.project.ProjectBuildingRequest; +import org.apache.maven.repository.internal.MavenRepositorySystemSession; +import org.apache.maven.shared.artifact.repository.RepositoryManager; +import org.codehaus.plexus.PlexusTestCase; +import org.sonatype.aether.impl.internal.EnhancedLocalRepositoryManager; +import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager; + +public class Maven30RepositoryManagerTest extends PlexusTestCase +{ + + private final File localRepo = new File( "target/tests/local-repo" ); + + private Maven30RepositoryManager repositoryManager; + + @Override + public void setUp() throws Exception + { + super.setUp(); + repositoryManager = (Maven30RepositoryManager) super.lookup( RepositoryManager.class, "maven3" ); + } + + public void testSetLocalRepositoryBasedirSimple() throws Exception + { + DefaultProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession(); + repositorySession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( localRepo ) ); + buildingRequest.setRepositorySession( repositorySession ); + + File basedir = new File( "NEW/LOCAL/REPO" ); + + ProjectBuildingRequest newBuildingRequest = repositoryManager.setLocalRepositoryBasedir( buildingRequest, basedir ); + + assertEquals( basedir.getAbsoluteFile(), newBuildingRequest.getRepositorySession().getLocalRepository().getBasedir() ); + + } + + public void testSetLocalRepositoryBasedirEnhanced() throws Exception + { + DefaultProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession(); + repositorySession.setLocalRepositoryManager( new EnhancedLocalRepositoryManager( localRepo ) ); + buildingRequest.setRepositorySession( repositorySession ); + + File basedir = new File( "NEW/LOCAL/REPO" ); + + ProjectBuildingRequest newBuildingRequest = repositoryManager.setLocalRepositoryBasedir( buildingRequest, basedir ); + + assertEquals( basedir.getAbsoluteFile(), newBuildingRequest.getRepositorySession().getLocalRepository().getBasedir() ); + + } + +}