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