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.DefaultArtifactRepository;
27  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
28  import org.apache.maven.plugin.MojoExecutionException;
29  import org.apache.maven.plugin.MojoFailureException;
30  import org.apache.maven.project.artifact.ProjectArtifactMetadata;
31  import org.codehaus.plexus.PlexusConstants;
32  import org.codehaus.plexus.PlexusContainer;
33  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
34  import org.codehaus.plexus.context.Context;
35  import org.codehaus.plexus.context.ContextException;
36  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
37  
38  import java.io.File;
39  import java.util.Iterator;
40  import java.util.List;
41  import java.util.regex.Matcher;
42  import java.util.regex.Pattern;
43  
44  /**
45   * Deploys an artifact to remote repository.
46   * 
47   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
48   * @author <a href="mailto:jdcasey@apache.org">John Casey (refactoring only)</a>
49   * @version $Id: DeployMojo.java 680848 2008-07-29 21:33:34Z olamy $
50   * @goal deploy
51   * @phase deploy
52   */
53  public class DeployMojo
54      extends AbstractDeployMojo
55      implements Contextualizable
56  {
57  
58      private static final Pattern ALT_REPO_SYNTAX_PATTERN = Pattern.compile( "(.+)::(.+)::(.+)" );
59  
60      /**
61       * @parameter expression="${project.artifact}"
62       * @required
63       * @readonly
64       */
65      private Artifact artifact;
66  
67      /**
68       * @parameter expression="${project.packaging}"
69       * @required
70       * @readonly
71       */
72      private String packaging;
73  
74      /**
75       * @parameter expression="${project.file}"
76       * @required
77       * @readonly
78       */
79      private File pomFile;
80  
81      /**
82       * @parameter expression="${project.distributionManagementArtifactRepository}"
83       * @readonly
84       */
85      private ArtifactRepository deploymentRepository;
86  
87      /**
88       * Specifies an alternative repository to which the project artifacts should be deployed ( other
89       * than those specified in &lt;distributionManagement&gt; ).
90       * <br/>
91       * Format: id::layout::url
92       * 
93       * @parameter expression="${altDeploymentRepository}"
94       */
95      private String altDeploymentRepository;
96      
97      /**
98       * @parameter expression="${project.attachedArtifacts}
99       * @required
100      * @readonly
101      */
102     private List attachedArtifacts;
103 
104     /**
105      * Parameter used to update the metadata to make the artifact as release.
106      * 
107      * @parameter expression="${updateReleaseInfo}" default-value="false"
108      */
109     private boolean updateReleaseInfo;
110 
111     /**
112      * Contextualized.
113      */
114     private PlexusContainer container;
115     
116     /**
117      * Set this to 'true' to bypass artifact deploy
118      *       
119      * @parameter expression="${maven.deploy.skip}" default-value="false"
120      * @since 2.4
121      */
122     private boolean skip;     
123 
124     public void execute()
125         throws MojoExecutionException, MojoFailureException
126     {
127         
128         if ( skip )
129         {
130             getLog().info( " skipping artifact deployement " );
131             return;
132         }
133         
134         ArtifactRepository repo = getDeploymentRepository();
135 
136         String protocol = repo.getProtocol();
137 
138         if ( protocol.equalsIgnoreCase( "scp" ) )
139         {
140             File sshFile = new File( System.getProperty( "user.home" ), ".ssh" );
141 
142             if ( !sshFile.exists() )
143             {
144                 sshFile.mkdirs();
145             }
146         }
147 
148         // Deploy the POM
149         boolean isPomArtifact = "pom".equals( packaging );
150         if ( !isPomArtifact )
151         {
152             ArtifactMetadata metadata = new ProjectArtifactMetadata( artifact, pomFile );
153             artifact.addMetadata( metadata );
154         }
155 
156         if ( updateReleaseInfo )
157         {
158             artifact.setRelease( true );
159         }
160 
161         try
162         {
163             if ( isPomArtifact )
164             {
165                 getDeployer().deploy( pomFile, artifact, repo, getLocalRepository() );
166             }
167             else
168             {
169                 File file = artifact.getFile();
170 
171                 if ( file != null && !file.isDirectory() )
172                 {
173                     getDeployer().deploy( file, artifact, repo, getLocalRepository() );
174                 }
175                 else if ( !attachedArtifacts.isEmpty() )
176                 {
177                     getLog().info( "No primary artifact to deploy, deploy attached artifacts instead." );
178                 }
179                 else
180                 {
181                     String message = "The packaging for this project did not assign a file to the build artifact";
182                     throw new MojoExecutionException( message );
183                 }
184             }
185 
186             for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); )
187             {
188                 Artifact attached = ( Artifact ) i.next();
189 
190                 getDeployer().deploy( attached.getFile(), attached, repo, getLocalRepository() );
191             }
192         }
193         catch ( ArtifactDeploymentException e )
194         {
195             throw new MojoExecutionException( e.getMessage(), e );
196         }
197     }
198 
199     private ArtifactRepository getDeploymentRepository()
200         throws MojoExecutionException, MojoFailureException
201     {
202         if ( deploymentRepository == null && altDeploymentRepository == null )
203         {
204             String msg = "Deployment failed: repository element was not specified in the pom inside"
205                 + " distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter";
206 
207             throw new MojoExecutionException( msg );
208         }
209 
210         ArtifactRepository repo = null;
211 
212         if ( altDeploymentRepository != null )
213         {
214             getLog().info( "Using alternate deployment repository " + altDeploymentRepository );
215 
216             Matcher matcher = ALT_REPO_SYNTAX_PATTERN.matcher( altDeploymentRepository );
217 
218             if ( !matcher.matches() )
219             {
220                 throw new MojoFailureException( altDeploymentRepository, "Invalid syntax for repository.",
221                                                 "Invalid syntax for alternative repository. Use \"id::layout::url\"." );
222             }
223             else
224             {
225                 String id = matcher.group( 1 ).trim();
226                 String layout = matcher.group( 2 ).trim();
227                 String url = matcher.group( 3 ).trim();
228                 
229                 ArtifactRepositoryLayout repoLayout;
230                 try
231                 {
232                     repoLayout = ( ArtifactRepositoryLayout ) container.lookup( ArtifactRepositoryLayout.ROLE, layout );
233                 }
234                 catch ( ComponentLookupException e )
235                 {
236                     throw new MojoExecutionException( "Cannot find repository layout: " + layout, e );
237                 }
238                 
239                 repo = new DefaultArtifactRepository( id, url, repoLayout );
240             }
241         }
242         
243         if ( repo == null )
244         {
245             repo = deploymentRepository;
246         }
247 
248         return repo;
249     }
250 
251     public void contextualize( Context context )
252         throws ContextException
253     {
254         this.container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
255     }
256 }