001    package 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    
022    import java.io.File;
023    
024    import org.apache.maven.RepositoryUtils;
025    import org.apache.maven.artifact.Artifact;
026    import org.apache.maven.artifact.metadata.ArtifactMetadata;
027    import org.apache.maven.artifact.repository.ArtifactRepository;
028    import org.apache.maven.artifact.repository.LegacyLocalRepositoryManager;
029    import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
030    import org.apache.maven.artifact.repository.metadata.MetadataBridge;
031    import org.apache.maven.artifact.repository.metadata.Snapshot;
032    import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
033    import org.apache.maven.artifact.repository.metadata.Versioning;
034    import org.apache.maven.plugin.LegacySupport;
035    import org.apache.maven.project.artifact.ProjectArtifactMetadata;
036    import org.codehaus.plexus.component.annotations.Component;
037    import org.codehaus.plexus.component.annotations.Requirement;
038    import org.codehaus.plexus.logging.AbstractLogEnabled;
039    import org.sonatype.aether.RepositorySystem;
040    import org.sonatype.aether.RepositorySystemSession;
041    import org.sonatype.aether.installation.InstallRequest;
042    import org.sonatype.aether.installation.InstallationException;
043    import org.sonatype.aether.util.DefaultRequestTrace;
044    import org.sonatype.aether.util.artifact.SubArtifact;
045    
046    /**
047     * @author Jason van Zyl
048     */
049    @Component( role = ArtifactInstaller.class )
050    public 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( DefaultRequestTrace.newChild( null, legacySupport.getSession().getCurrentProject() ) );
081    
082            org.sonatype.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.sonatype.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    }