1 package org.apache.maven.shared.transfer.artifact.deploy.internal;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Collection;
23
24 import org.apache.maven.artifact.Artifact;
25 import org.apache.maven.artifact.repository.ArtifactRepository;
26 import org.apache.maven.project.ProjectBuildingRequest;
27 import org.apache.maven.shared.transfer.artifact.deploy.ArtifactDeployer;
28 import org.apache.maven.shared.transfer.artifact.deploy.ArtifactDeployerException;
29 import org.codehaus.plexus.PlexusConstants;
30 import org.codehaus.plexus.PlexusContainer;
31 import org.codehaus.plexus.component.annotations.Component;
32 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
33 import org.codehaus.plexus.context.Context;
34 import org.codehaus.plexus.context.ContextException;
35 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
36
37
38
39
40 @Component( role = ArtifactDeployer.class )
41 class DefaultArtifactDeployer implements ArtifactDeployer, Contextualizable
42 {
43
44 private PlexusContainer container;
45
46 @Override
47 public void deploy( ProjectBuildingRequest request, Collection<Artifact> mavenArtifacts )
48 throws ArtifactDeployerException
49 {
50 validateParameters( request, mavenArtifacts );
51
52 try
53 {
54 getMavenArtifactDeployer( request ).deploy( mavenArtifacts );
55 }
56 catch ( ComponentLookupException e )
57 {
58 throw new ArtifactDeployerException( e.getMessage(), e );
59 }
60 }
61
62 @Override
63 public void deploy( ProjectBuildingRequest request, ArtifactRepository remoteRepository,
64 Collection<Artifact> mavenArtifacts ) throws ArtifactDeployerException
65 {
66 validateParameters( request, mavenArtifacts );
67 try
68 {
69 getMavenArtifactDeployer( request ).deploy( remoteRepository, mavenArtifacts );
70 }
71 catch ( ComponentLookupException e )
72 {
73 throw new ArtifactDeployerException( e.getMessage(), e );
74 }
75 }
76
77 private void validateParameters( ProjectBuildingRequest request, Collection<Artifact> mavenArtifacts )
78 {
79 if ( request == null )
80 {
81 throw new IllegalArgumentException( "The parameter request is not allowed to be null." );
82 }
83 if ( mavenArtifacts == null )
84 {
85 throw new IllegalArgumentException( "The parameter mavenArtifacts is not allowed to be null." );
86 }
87 if ( mavenArtifacts.isEmpty() )
88 {
89 throw new IllegalArgumentException( "The collection mavenArtifacts is not allowed to be empty." );
90 }
91 }
92
93
94
95
96 private boolean isMaven31()
97 {
98 try
99 {
100
101 Thread.currentThread().getContextClassLoader().loadClass( "org.eclipse.aether.artifact.Artifact" );
102 return true;
103 }
104 catch ( ClassNotFoundException e )
105 {
106 return false;
107 }
108 }
109
110
111
112
113
114
115
116 public void contextualize( Context context ) throws ContextException
117 {
118 container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
119 }
120
121 private MavenArtifactDeployer getMavenArtifactDeployer( ProjectBuildingRequest buildingRequest )
122 throws ComponentLookupException, ArtifactDeployerException
123 {
124 if ( isMaven31() )
125 {
126 org.eclipse.aether.RepositorySystem repositorySystem = container.lookup(
127 org.eclipse.aether.RepositorySystem.class );
128
129 org.eclipse.aether.RepositorySystemSession session = Invoker.invoke( buildingRequest,
130 "getRepositorySession" );
131
132 return new Maven31ArtifactDeployer( repositorySystem, session );
133 }
134 else
135 {
136 org.sonatype.aether.RepositorySystem repositorySystem = container.lookup(
137 org.sonatype.aether.RepositorySystem.class );
138
139 org.sonatype.aether.RepositorySystemSession session = Invoker.invoke( buildingRequest,
140 "getRepositorySession" );
141
142 return new Maven30ArtifactDeployer( repositorySystem, session );
143 }
144 }
145 }