View Javadoc

1   package org.apache.maven.plugin.deploy;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Deploys an artifact to remote repository.
40   * 
41   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
42   * @author <a href="mailto:jdcasey@apache.org">John Casey (refactoring only)</a>
43   * @version $Id: DeployMojo.java 1160164 2011-08-22 09:38:32Z stephenc $
44   * @goal deploy
45   * @phase deploy
46   * @threadSafe
47   */
48  public class DeployMojo
49      extends AbstractDeployMojo
50  {
51  
52      private static final Pattern ALT_REPO_SYNTAX_PATTERN = Pattern.compile( "(.+)::(.+)::(.+)" );
53  
54      /**
55       * @parameter default-value="${project}"
56       * @required
57       * @readonly
58       */
59      private MavenProject project;
60  
61      /**
62       * @parameter default-value="${project.artifact}"
63       * @required
64       * @readonly
65       */
66      private Artifact artifact;
67  
68      /**
69       * @parameter default-value="${project.packaging}"
70       * @required
71       * @readonly
72       */
73      private String packaging;
74  
75      /**
76       * @parameter default-value="${project.file}"
77       * @required
78       * @readonly
79       */
80      private File pomFile;
81  
82      /**
83       * Specifies an alternative repository to which the project artifacts should be deployed ( other
84       * than those specified in &lt;distributionManagement&gt; ).
85       * <br/>
86       * Format: id::layout::url
87       * 
88       * @parameter expression="${altDeploymentRepository}"
89       */
90      private String altDeploymentRepository;
91      
92      /**
93       * @parameter default-value="${project.attachedArtifacts}
94       * @required
95       * @readonly
96       */
97      private List attachedArtifacts;
98      
99      /**
100      * Set this to 'true' to bypass artifact deploy
101      *       
102      * @parameter expression="${maven.deploy.skip}" default-value="false"
103      * @since 2.4
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         // Deploy the POM
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                     // propagate the timestamped version to the main artifact for the attached artifacts to pick it up
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 }