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 java.math.BigInteger;
23  import java.security.MessageDigest;
24  import java.util.ArrayList;
25  import java.util.Iterator;
26  import java.util.List;
27  
28  import org.apache.maven.model.Repository;
29  import org.apache.tools.ant.BuildException;
30  import org.apache.tools.ant.Project;
31  
32  /**
33   * Base class for atifact tasks that are able to download artifact from repote repositories. 
34   * @version $Id: AbstractArtifactWithRepositoryTask.java 772278 2009-05-06 14:31:49Z pgier $
35   */
36  public abstract class AbstractArtifactWithRepositoryTask
37      extends AbstractArtifactTask
38  {
39      /**
40       * List of Ant Tasks RemoteRepository-ies
41       */
42      private List remoteRepositories = new ArrayList();
43  
44      /**
45       * Get the default remote repository.
46       * @return central repository 
47       */
48      private static RemoteRepository getDefaultRemoteRepository()
49      {
50          // TODO: could we utilize the super POM for this?
51          RemoteRepository remoteRepository = new RemoteRepository();
52          remoteRepository.setId( "central" );
53          remoteRepository.setUrl( "http://repo1.maven.org/maven2" );
54          RepositoryPolicy snapshots = new RepositoryPolicy();
55          snapshots.setEnabled( false );
56          remoteRepository.addSnapshots( snapshots );
57          return remoteRepository;
58      }
59  
60      private static String statusAsString( RepositoryPolicy policy )
61      {
62          return ( policy == null ) || policy.isEnabled() ? "enabled" : "disabled";
63      }
64  
65      protected List createRemoteArtifactRepositories()
66      {
67          return createRemoteArtifactRepositories( null );
68      }
69  
70      /**
71       * Create the list of ArtifactRepository-ies where artifacts can be downloaded. If
72       * no remote repository has been configured, adds central repository.
73       * @param pomRepositories additionnal repositories defined in pom (or null if none)
74       * @return the list of ArtifactRepository-ies
75       * @see #createRemoteArtifactRepository(RemoteRepository)
76       */
77      protected List createRemoteArtifactRepositories( List pomRepositories )
78      {
79          List remoteRepositories = new ArrayList();
80          remoteRepositories.addAll( getRemoteRepositories() );
81  
82          if ( getRemoteRepositories().isEmpty() )
83          {
84              remoteRepositories.add( getDefaultRemoteRepository() );
85          }
86  
87          if ( pomRepositories != null )
88          {
89              for ( Iterator i = pomRepositories.iterator(); i.hasNext(); )
90              {
91                  Repository pomRepository = (Repository) i.next();
92              
93                  remoteRepositories.add( createAntRemoteRepository( pomRepository ) );
94              }
95          }
96  
97          log( "Using remote repositories:", Project.MSG_VERBOSE );
98          List list = new ArrayList();
99          for ( Iterator i = remoteRepositories.iterator(); i.hasNext(); )
100         {
101             RemoteRepository remoteRepository = (RemoteRepository) i.next();
102             updateRepositoryWithSettings( remoteRepository );
103     
104             StringBuffer msg = new StringBuffer();
105             msg.append( "  - id=" + remoteRepository.getId() );
106             msg.append( ", url=" + remoteRepository.getUrl() );
107             msg.append( ", releases=" + statusAsString( remoteRepository.getReleases() ) );
108             msg.append( ", snapshots=" + statusAsString( remoteRepository.getSnapshots() ) );
109             if ( remoteRepository.getAuthentication() != null )
110             {
111                 msg.append( ", authentication=" + remoteRepository.getAuthentication().getUserName() );
112             }
113             if ( remoteRepository.getProxy() != null )
114             {
115                 msg.append( ", proxy=" + remoteRepository.getProxy().getHost() );
116             }
117             getProject().log( msg.toString(), Project.MSG_VERBOSE );
118     
119             list.add( createRemoteArtifactRepository( remoteRepository ) );
120         }
121         return list;
122     }
123 
124     public List getRemoteRepositories()
125     {
126         return remoteRepositories;
127     }
128 
129     /**
130      * This is called automatically by ant when the task is initialized.
131      * Need to use "addConfigured..." instead of "add..." because the 
132      * repository Id and URL need to be set before the method is called.
133      * 
134      * @param remoteRepository
135      */
136     public void addConfiguredRemoteRepository( RemoteRepository remoteRepository )
137     {
138         // Validate the url and id parameters before adding the repository
139         if ( remoteRepository.getUrl() == null )
140         {
141             throw new BuildException( "Each remote repository must specify a url." );
142         }
143         if ( remoteRepository.getId() == null || remoteRepository.getId().equals( remoteRepository.getUrl() ) )
144         {
145             log( "Each remote repository must specify a unique id. For backward-compatibility, "
146                  + "a default id will be used. In future releases, a missing repository id will raise an error.",
147                   Project.MSG_WARN );
148             remoteRepository.setId( generateDefaultRepositoryId( remoteRepository ) );
149         }
150         remoteRepositories.add( remoteRepository );
151     }
152     
153     public final String MD5_ALGO_NAME = "MD5";
154     
155     public final String UTF_ENC_NAME = "UTF-8";
156     
157     /**
158      * Generates an MD5 digest based on the url of the repository.
159      * This is safer to use for the id than the url.  
160      * MANTTASKS-142
161      * 
162      * @param repository
163      * @return
164      */
165     public String generateDefaultRepositoryId( RemoteRepository repository )
166     {
167         try
168         {
169             MessageDigest md = MessageDigest.getInstance( MD5_ALGO_NAME );
170             md.update( repository.getUrl().getBytes( UTF_ENC_NAME ) );
171             BigInteger digest = new BigInteger( md.digest() );
172             return digest.toString( 16 );
173         }
174         catch ( Exception e )
175         {
176             log( "Unable to generate unique repository Id: " + e, Project.MSG_WARN );
177             return "default";
178         }
179     }
180 }