1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.artifact.installer;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.io.File;
26
27 import org.apache.maven.RepositoryUtils;
28 import org.apache.maven.artifact.Artifact;
29 import org.apache.maven.artifact.metadata.ArtifactMetadata;
30 import org.apache.maven.artifact.repository.ArtifactRepository;
31 import org.apache.maven.artifact.repository.LegacyLocalRepositoryManager;
32 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
33 import org.apache.maven.artifact.repository.metadata.MetadataBridge;
34 import org.apache.maven.artifact.repository.metadata.Snapshot;
35 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
36 import org.apache.maven.artifact.repository.metadata.Versioning;
37 import org.apache.maven.plugin.LegacySupport;
38 import org.apache.maven.project.artifact.ProjectArtifactMetadata;
39 import org.codehaus.plexus.logging.AbstractLogEnabled;
40 import org.eclipse.aether.RepositorySystem;
41 import org.eclipse.aether.RepositorySystemSession;
42 import org.eclipse.aether.RequestTrace;
43 import org.eclipse.aether.installation.InstallRequest;
44 import org.eclipse.aether.installation.InstallationException;
45 import org.eclipse.aether.util.artifact.SubArtifact;
46
47
48
49
50 @Named
51 @Singleton
52 public class DefaultArtifactInstaller extends AbstractLogEnabled implements ArtifactInstaller {
53
54 @Inject
55 private RepositorySystem repoSystem;
56
57 @Inject
58 private LegacySupport legacySupport;
59
60
61 @Deprecated
62 public void install(String basedir, String finalName, Artifact artifact, ArtifactRepository localRepository)
63 throws ArtifactInstallationException {
64 String extension = artifact.getArtifactHandler().getExtension();
65 File source = new File(basedir, finalName + "." + extension);
66
67 install(source, artifact, localRepository);
68 }
69
70 public void install(File source, Artifact artifact, ArtifactRepository localRepository)
71 throws ArtifactInstallationException {
72 RepositorySystemSession session =
73 LegacyLocalRepositoryManager.overlay(localRepository, legacySupport.getRepositorySession(), repoSystem);
74
75 InstallRequest request = new InstallRequest();
76
77 request.setTrace(RequestTrace.newChild(null, legacySupport.getSession().getCurrentProject()));
78
79 org.eclipse.aether.artifact.Artifact mainArtifact = RepositoryUtils.toArtifact(artifact);
80 mainArtifact = mainArtifact.setFile(source);
81 request.addArtifact(mainArtifact);
82
83 for (ArtifactMetadata metadata : artifact.getMetadataList()) {
84 if (metadata instanceof ProjectArtifactMetadata) {
85 org.eclipse.aether.artifact.Artifact pomArtifact = new SubArtifact(mainArtifact, "", "pom");
86 pomArtifact = pomArtifact.setFile(((ProjectArtifactMetadata) metadata).getFile());
87 request.addArtifact(pomArtifact);
88 } else if (metadata instanceof SnapshotArtifactRepositoryMetadata
89 || metadata instanceof ArtifactRepositoryMetadata) {
90
91 } else {
92 request.addMetadata(new MetadataBridge(metadata));
93 }
94 }
95
96 try {
97 repoSystem.install(session, request);
98 } catch (InstallationException e) {
99 throw new ArtifactInstallationException(e.getMessage(), e);
100 }
101
102
103
104
105
106 if (artifact.isSnapshot()) {
107 Snapshot snapshot = new Snapshot();
108 snapshot.setLocalCopy(true);
109 artifact.addMetadata(new SnapshotArtifactRepositoryMetadata(artifact, snapshot));
110 }
111
112 Versioning versioning = new Versioning();
113
114 versioning.updateTimestamp();
115 versioning.addVersion(artifact.getBaseVersion());
116 if (artifact.isRelease()) {
117 versioning.setRelease(artifact.getBaseVersion());
118 }
119 artifact.addMetadata(new ArtifactRepositoryMetadata(artifact, versioning));
120 }
121 }