1 package org.apache.maven.plugin.deploy;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.artifact.Artifact;
23 import org.apache.maven.artifact.deployer.ArtifactDeploymentException;
24 import org.apache.maven.artifact.metadata.ArtifactMetadata;
25 import org.apache.maven.artifact.repository.ArtifactRepository;
26 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
27 import org.apache.maven.plugin.MojoExecutionException;
28 import org.apache.maven.plugin.MojoFailureException;
29 import org.apache.maven.project.MavenProject;
30 import org.apache.maven.project.artifact.ProjectArtifactMetadata;
31
32 import java.io.File;
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.regex.Matcher;
36 import java.util.regex.Pattern;
37
38
39
40
41
42
43
44
45
46
47 public class DeployMojo
48 extends AbstractDeployMojo
49 {
50
51 private static final Pattern ALT_REPO_SYNTAX_PATTERN = Pattern.compile( "(.+)::(.+)::(.+)" );
52
53
54
55
56
57
58 private MavenProject project;
59
60
61
62
63
64
65 private Artifact artifact;
66
67
68
69
70
71
72 private String packaging;
73
74
75
76
77
78
79 private File pomFile;
80
81
82
83
84
85
86
87
88
89 private String altDeploymentRepository;
90
91
92
93
94
95
96 private List attachedArtifacts;
97
98
99
100
101
102
103
104 private boolean skip;
105
106 public void execute()
107 throws MojoExecutionException, MojoFailureException
108 {
109 if ( skip )
110 {
111 getLog().info( "Skipping artifact deployment" );
112 return;
113 }
114
115 failIfOffline();
116
117 ArtifactRepository repo = getDeploymentRepository();
118
119 String protocol = repo.getProtocol();
120
121 if ( protocol.equalsIgnoreCase( "scp" ) )
122 {
123 File sshFile = new File( System.getProperty( "user.home" ), ".ssh" );
124
125 if ( !sshFile.exists() )
126 {
127 sshFile.mkdirs();
128 }
129 }
130
131
132 boolean isPomArtifact = "pom".equals( packaging );
133 if ( !isPomArtifact )
134 {
135 ArtifactMetadata metadata = new ProjectArtifactMetadata( artifact, pomFile );
136 artifact.addMetadata( metadata );
137 }
138
139 if ( updateReleaseInfo )
140 {
141 artifact.setRelease( true );
142 }
143
144 try
145 {
146 if ( isPomArtifact )
147 {
148 getDeployer().deploy( pomFile, artifact, repo, getLocalRepository() );
149 }
150 else
151 {
152 File file = artifact.getFile();
153
154 if ( file != null && file.isFile() )
155 {
156 getDeployer().deploy( file, artifact, repo, getLocalRepository() );
157 }
158 else if ( !attachedArtifacts.isEmpty() )
159 {
160 getLog().info( "No primary artifact to deploy, deploying attached artifacts instead." );
161
162 Artifact pomArtifact =
163 artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(),
164 artifact.getBaseVersion() );
165 pomArtifact.setFile( pomFile );
166 if ( updateReleaseInfo )
167 {
168 pomArtifact.setRelease( true );
169 }
170
171 getDeployer().deploy( pomFile, pomArtifact, repo, getLocalRepository() );
172 }
173 else
174 {
175 String message = "The packaging for this project did not assign a file to the build artifact";
176 throw new MojoExecutionException( message );
177 }
178 }
179
180 for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); )
181 {
182 Artifact attached = ( Artifact ) i.next();
183
184 getDeployer().deploy( attached.getFile(), attached, repo, getLocalRepository() );
185 }
186 }
187 catch ( ArtifactDeploymentException e )
188 {
189 throw new MojoExecutionException( e.getMessage(), e );
190 }
191 }
192
193 private ArtifactRepository getDeploymentRepository()
194 throws MojoExecutionException, MojoFailureException
195 {
196 ArtifactRepository repo = null;
197
198 if ( altDeploymentRepository != null )
199 {
200 getLog().info( "Using alternate deployment repository " + altDeploymentRepository );
201
202 Matcher matcher = ALT_REPO_SYNTAX_PATTERN.matcher( altDeploymentRepository );
203
204 if ( !matcher.matches() )
205 {
206 throw new MojoFailureException( altDeploymentRepository, "Invalid syntax for repository.",
207 "Invalid syntax for alternative repository. Use \"id::layout::url\"." );
208 }
209 else
210 {
211 String id = matcher.group( 1 ).trim();
212 String layout = matcher.group( 2 ).trim();
213 String url = matcher.group( 3 ).trim();
214
215 ArtifactRepositoryLayout repoLayout = getLayout( layout );
216
217 repo = repositoryFactory.createDeploymentArtifactRepository( id, url, repoLayout, true );
218 }
219 }
220
221 if ( repo == null )
222 {
223 repo = project.getDistributionManagementArtifactRepository();
224 }
225
226 if ( repo == null )
227 {
228 String msg = "Deployment failed: repository element was not specified in the POM inside"
229 + " distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter";
230
231 throw new MojoExecutionException( msg );
232 }
233
234 return repo;
235 }
236
237 }