View Javadoc

1   package org.apache.maven.artifact.ant;
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.ArtifactDeployer;
24  import org.apache.maven.artifact.deployer.ArtifactDeploymentException;
25  import org.apache.maven.artifact.metadata.ArtifactMetadata;
26  import org.apache.maven.artifact.repository.ArtifactRepository;
27  import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
28  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
29  import org.apache.maven.model.DistributionManagement;
30  import org.apache.maven.project.artifact.ProjectArtifactMetadata;
31  import org.apache.tools.ant.BuildException;
32  import org.apache.tools.ant.Project;
33  
34  /**
35   * Deploy task, using maven-artifact.
36   *
37   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
38   * @version $Id: DeployTask.html 806929 2012-03-01 18:57:40Z hboutemy $
39   */
40  public class DeployTask
41      extends InstallDeployTaskSupport
42  {
43      private RemoteRepository remoteRepository;
44  
45      private RemoteRepository remoteSnapshotRepository;
46  
47      private boolean uniqueVersion = true;
48  
49      /**
50       * Create a core-Maven deployment ArtifactRepository from a Maven Ant Tasks's RemoteRepository definition.
51       * @param repository the remote repository as defined in Ant
52       * @return the corresponding ArtifactRepository
53       */
54      protected ArtifactRepository createDeploymentArtifactRepository( RemoteRepository repository )
55      {
56          if ( repository.getId().equals( repository.getUrl() ) )
57          {
58              // MANTTASKS-103: avoid default id set to the url, since it is used for maven-metadata-<id>.xml
59              repository.setId( "remote" );
60          }
61  
62          updateRepositoryWithSettings( repository );
63  
64          ArtifactRepositoryLayout repositoryLayout =
65              (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, repository.getLayout() );
66  
67          ArtifactRepositoryFactory repositoryFactory = null;
68  
69          ArtifactRepository artifactRepository;
70  
71          try
72          {
73              repositoryFactory = getArtifactRepositoryFactory( repository );
74  
75              artifactRepository =
76                  repositoryFactory.createDeploymentArtifactRepository( repository.getId(), repository.getUrl(),
77                                                                        repositoryLayout, uniqueVersion );
78          }
79          finally
80          {
81              releaseArtifactRepositoryFactory( repositoryFactory );
82          }
83  
84          return artifactRepository;
85      }
86  
87  
88      protected void doExecute()
89      {
90          if ( file == null && ( attachedArtifacts.size() == 0 ) )
91          {
92              throw new BuildException( "You must specify a file and/or an attached artifact "
93                  + "to deploy to the repository." );
94          }
95  
96          ArtifactRepository localRepo = createLocalArtifactRepository();
97  
98          Pom pom = initializePom( localRepo );
99  
100         if ( pom == null )
101         {
102             throw new BuildException( "A POM element is required to deploy to the repository" );
103         }
104 
105         Artifact artifact = pom.getArtifact();
106 
107         // Deploy the POM
108         boolean isPomArtifact = "pom".equals( pom.getPackaging() );
109         if ( !isPomArtifact )
110         {
111             ArtifactMetadata metadata = new ProjectArtifactMetadata( artifact, pom.getFile() );
112             artifact.addMetadata( metadata );
113         }
114 
115         ArtifactRepository deploymentRepository = getDeploymentRepository( pom, artifact );
116 
117         log( "Deploying to " + deploymentRepository.getUrl(), Project.MSG_INFO );
118         ArtifactDeployer deployer = (ArtifactDeployer) lookup( ArtifactDeployer.ROLE );
119         try
120         {
121             if ( file != null )
122             {
123                 if ( !isPomArtifact )
124                 {
125                     deployer.deploy( file, artifact, deploymentRepository, localRepo );
126                 }
127                 else
128                 {
129                     deployer.deploy( pom.getFile(), artifact, deploymentRepository, localRepo );
130                 }
131             }
132 
133             // Deploy any attached artifacts
134             if ( attachedArtifacts != null )
135             {
136                 for ( Artifact attachedArtifact : pom.getAttachedArtifacts() )
137                 {
138                     deployer.deploy( attachedArtifact.getFile(), attachedArtifact, deploymentRepository, localRepo );
139                 }
140             }
141         }
142         catch ( ArtifactDeploymentException e )
143         {
144             throw new BuildException(
145                 "Error deploying artifact '" + artifact.getDependencyConflictId() + "': " + e.getMessage(), e );
146         }
147     }
148 
149     private ArtifactRepository getDeploymentRepository( Pom pom, Artifact artifact )
150     {
151         DistributionManagement distributionManagement = pom.getDistributionManagement();
152 
153         if ( remoteSnapshotRepository == null && remoteRepository == null )
154         {
155             if ( distributionManagement != null )
156             {
157                 if ( distributionManagement.getSnapshotRepository() != null )
158                 {
159                     remoteSnapshotRepository = createAntRemoteRepositoryBase( distributionManagement
160                         .getSnapshotRepository() );
161                     uniqueVersion = distributionManagement.getSnapshotRepository().isUniqueVersion();
162                 }
163                 if ( distributionManagement.getRepository() != null )
164                 {
165                     remoteRepository = createAntRemoteRepositoryBase( distributionManagement.getRepository() );
166                 }
167             }
168         }
169 
170         if ( remoteSnapshotRepository == null )
171         {
172             remoteSnapshotRepository = remoteRepository;
173         }
174 
175         ArtifactRepository deploymentRepository;
176         if ( artifact.isSnapshot() && remoteSnapshotRepository != null )
177         {
178             deploymentRepository = createDeploymentArtifactRepository( remoteSnapshotRepository );
179         }
180         else if ( remoteRepository != null )
181         {
182             deploymentRepository = createDeploymentArtifactRepository( remoteRepository );
183         }
184         else
185         {
186             throw new BuildException(
187                 "A distributionManagement element or remoteRepository element is required to deploy" );
188         }
189 
190         return deploymentRepository;
191     }
192 
193     public RemoteRepository getRemoteRepository()
194     {
195         return remoteRepository;
196     }
197 
198     public void addRemoteSnapshotRepository( RemoteRepository remoteSnapshotRepository )
199     {
200         this.remoteSnapshotRepository = remoteSnapshotRepository;
201     }
202 
203     public void addRemoteRepository( RemoteRepository remoteRepository )
204     {
205         this.remoteRepository = remoteRepository;
206     }
207 
208     public void setUniqueVersion( boolean uniqueVersion )
209     {
210         this.uniqueVersion = uniqueVersion;
211     }
212 
213     public boolean getUniqueVersion()
214     {
215         return uniqueVersion;
216     }
217 }