1 package org.apache.maven.artifact.installer;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23
24 import org.apache.maven.RepositoryUtils;
25 import org.apache.maven.artifact.Artifact;
26 import org.apache.maven.artifact.metadata.ArtifactMetadata;
27 import org.apache.maven.artifact.repository.ArtifactRepository;
28 import org.apache.maven.artifact.repository.LegacyLocalRepositoryManager;
29 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
30 import org.apache.maven.artifact.repository.metadata.MetadataBridge;
31 import org.apache.maven.artifact.repository.metadata.Snapshot;
32 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
33 import org.apache.maven.artifact.repository.metadata.Versioning;
34 import org.apache.maven.plugin.LegacySupport;
35 import org.apache.maven.project.artifact.ProjectArtifactMetadata;
36 import org.codehaus.plexus.component.annotations.Component;
37 import org.codehaus.plexus.component.annotations.Requirement;
38 import org.codehaus.plexus.logging.AbstractLogEnabled;
39 import org.sonatype.aether.RepositorySystem;
40 import org.sonatype.aether.RepositorySystemSession;
41 import org.sonatype.aether.installation.InstallRequest;
42 import org.sonatype.aether.installation.InstallationException;
43 import org.sonatype.aether.util.DefaultRequestTrace;
44 import org.sonatype.aether.util.artifact.SubArtifact;
45
46
47
48
49 @Component( role = ArtifactInstaller.class )
50 public class DefaultArtifactInstaller
51 extends AbstractLogEnabled
52 implements ArtifactInstaller
53 {
54
55 @Requirement
56 private RepositorySystem repoSystem;
57
58 @Requirement
59 private LegacySupport legacySupport;
60
61
62 @Deprecated
63 public void install( String basedir, String finalName, Artifact artifact, ArtifactRepository localRepository )
64 throws ArtifactInstallationException
65 {
66 String extension = artifact.getArtifactHandler().getExtension();
67 File source = new File( basedir, finalName + "." + extension );
68
69 install( source, artifact, localRepository );
70 }
71
72 public void install( File source, Artifact artifact, ArtifactRepository localRepository )
73 throws ArtifactInstallationException
74 {
75 RepositorySystemSession session =
76 LegacyLocalRepositoryManager.overlay( localRepository, legacySupport.getRepositorySession(), repoSystem );
77
78 InstallRequest request = new InstallRequest();
79
80 request.setTrace( DefaultRequestTrace.newChild( null, legacySupport.getSession().getCurrentProject() ) );
81
82 org.sonatype.aether.artifact.Artifact mainArtifact = RepositoryUtils.toArtifact( artifact );
83 mainArtifact = mainArtifact.setFile( source );
84 request.addArtifact( mainArtifact );
85
86 for ( ArtifactMetadata metadata : artifact.getMetadataList() )
87 {
88 if ( metadata instanceof ProjectArtifactMetadata )
89 {
90 org.sonatype.aether.artifact.Artifact pomArtifact = new SubArtifact( mainArtifact, "", "pom" );
91 pomArtifact = pomArtifact.setFile( ( (ProjectArtifactMetadata) metadata ).getFile() );
92 request.addArtifact( pomArtifact );
93 }
94 else if ( metadata instanceof SnapshotArtifactRepositoryMetadata
95 || metadata instanceof ArtifactRepositoryMetadata )
96 {
97
98 }
99 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
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 }