001package org.apache.maven.artifact.installer;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023
024import org.apache.maven.RepositoryUtils;
025import org.apache.maven.artifact.Artifact;
026import org.apache.maven.artifact.metadata.ArtifactMetadata;
027import org.apache.maven.artifact.repository.ArtifactRepository;
028import org.apache.maven.artifact.repository.LegacyLocalRepositoryManager;
029import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
030import org.apache.maven.artifact.repository.metadata.MetadataBridge;
031import org.apache.maven.artifact.repository.metadata.Snapshot;
032import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
033import org.apache.maven.artifact.repository.metadata.Versioning;
034import org.apache.maven.plugin.LegacySupport;
035import org.apache.maven.project.artifact.ProjectArtifactMetadata;
036import org.codehaus.plexus.component.annotations.Component;
037import org.codehaus.plexus.component.annotations.Requirement;
038import org.codehaus.plexus.logging.AbstractLogEnabled;
039import org.eclipse.aether.RepositorySystem;
040import org.eclipse.aether.RepositorySystemSession;
041import org.eclipse.aether.RequestTrace;
042import org.eclipse.aether.installation.InstallRequest;
043import org.eclipse.aether.installation.InstallationException;
044import org.eclipse.aether.util.artifact.SubArtifact;
045
046/**
047 * @author Jason van Zyl
048 */
049@Component( role = ArtifactInstaller.class )
050public class DefaultArtifactInstaller
051    extends AbstractLogEnabled
052    implements ArtifactInstaller
053{
054
055    @Requirement
056    private RepositorySystem repoSystem;
057    
058    @Requirement
059    private LegacySupport legacySupport;
060    
061    /** @deprecated we want to use the artifact method only, and ensure artifact.file is set correctly. */
062    @Deprecated
063    public void install( String basedir, String finalName, Artifact artifact, ArtifactRepository localRepository )
064        throws ArtifactInstallationException
065    {
066        String extension = artifact.getArtifactHandler().getExtension();
067        File source = new File( basedir, finalName + "." + extension );
068
069        install( source, artifact, localRepository );
070    }
071
072    public void install( File source, Artifact artifact, ArtifactRepository localRepository )
073        throws ArtifactInstallationException
074    {
075        RepositorySystemSession session =
076            LegacyLocalRepositoryManager.overlay( localRepository, legacySupport.getRepositorySession(), repoSystem );
077
078        InstallRequest request = new InstallRequest();
079
080        request.setTrace( RequestTrace.newChild( null, legacySupport.getSession().getCurrentProject() ) );
081
082        org.eclipse.aether.artifact.Artifact mainArtifact = RepositoryUtils.toArtifact( artifact );
083        mainArtifact = mainArtifact.setFile( source );
084        request.addArtifact( mainArtifact );
085
086        for ( ArtifactMetadata metadata : artifact.getMetadataList() )
087        {
088            if ( metadata instanceof ProjectArtifactMetadata )
089            {
090                org.eclipse.aether.artifact.Artifact pomArtifact = new SubArtifact( mainArtifact, "", "pom" );
091                pomArtifact = pomArtifact.setFile( ( (ProjectArtifactMetadata) metadata ).getFile() );
092                request.addArtifact( pomArtifact );
093            }
094            else if ( metadata instanceof SnapshotArtifactRepositoryMetadata
095                || metadata instanceof ArtifactRepositoryMetadata )
096            {
097                // eaten, handled by repo system
098            }
099            else
100            {
101                request.addMetadata( new MetadataBridge( metadata ) );
102            }
103        }
104
105        try
106        {
107            repoSystem.install( session, request );
108        }
109        catch ( InstallationException e )
110        {
111            throw new ArtifactInstallationException( e.getMessage(), e );
112        }
113
114        /*
115         * NOTE: Not used by Maven core, only here to provide backward-compat with plugins like the Install Plugin.
116         */
117
118        if ( artifact.isSnapshot() )
119        {
120            Snapshot snapshot = new Snapshot();
121            snapshot.setLocalCopy( true );
122            artifact.addMetadata( new SnapshotArtifactRepositoryMetadata( artifact, snapshot ) );
123        }
124
125        Versioning versioning = new Versioning();
126        versioning.updateTimestamp();
127        versioning.addVersion( artifact.getBaseVersion() );
128        if ( artifact.isRelease() )
129        {
130            versioning.setRelease( artifact.getBaseVersion() );
131        }
132        artifact.addMetadata( new ArtifactRepositoryMetadata( artifact, versioning ) );
133    }
134
135}