View Javadoc

1   package org.apache.maven;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  
23  import org.apache.maven.project.Dependency;
24  import org.apache.maven.project.Project;
25  
26  import java.io.File;
27  
28  import java.util.ArrayList;
29  import java.util.Collections;
30  import java.util.Iterator;
31  import java.util.List;
32  
33  
34  /**
35   * Retreive descriptions for project's dependencies.
36   *
37   * @author <a href="mailto:michal.maczka@dimatics.com">Michal Maczka</a>
38   * @version $Id: DependencyDescriberBean.java 532339 2007-04-25 12:28:56Z ltheussl $
39   */
40  public class DependencyDescriberBean
41  {
42    //~ Instance fields ------------------------------------------------
43  
44    /** Dependencies list. */
45    private List dependencies = new ArrayList();
46  
47    /** For debug output. */
48    private Log log = LogFactory.getLog(DependencyDescriberBean.class);
49  
50    /** Project reference. */
51    private Project project;
52  
53    //~ Methods --------------------------------------------------------
54  
55    /**
56     * The list of described dependencies.
57     *
58     * @return list of described dependencies
59     *
60     * @see DescribedDependency
61     */
62    public List getDependencies()
63    {
64      return dependencies;
65    }
66  
67    /**
68     * Project provides context/session related information, e.g where
69     * maven repo local is.
70     *
71     * @param projectValue project to scan
72     */
73    public void build(Project projectValue)
74    {
75      this.project = projectValue;
76  
77      for (Iterator iter = project.getDependencies().iterator();
78          iter.hasNext();)
79      {
80        Dependency          dependency = (Dependency) iter.next();
81        DescribedDependency describedDependency =
82          new DescribedDependency(dependency);
83        dependencies.add(describedDependency);
84  
85        Project resolvedProject = resolveProject(dependency);
86  
87        if (resolvedProject != null)
88        {
89          log.debug("POM URL:" + resolvedProject.getUrl());
90          describedDependency.setUrl(resolvedProject.getUrl());
91          describedDependency.setDescription(
92            resolvedProject.getDescription());
93        }
94      }
95  
96      Collections.sort(dependencies);
97    }
98  
99    /**
100    * Returns the project for a given dependency.
101    *
102    * @param dependency The dependency for which the project is
103    *        searched.
104    *
105    * @return The project associated to this dependency.
106    */
107   private Project resolveProject(Dependency dependency)
108   {
109     File projectFile = null;
110 
111     if ("pom".equals(dependency.getType()))
112     {
113       projectFile =
114         new File(
115           project.getContext().getMavenRepoLocal(),
116           dependency.getArtifact());
117     }
118     else
119     {
120       projectFile =
121         new File(
122           project.getContext().getMavenRepoLocal(),
123           dependency.getArtifactDirectory() + "/poms/"
124           + dependency.getArtifactId() + "-"
125           + dependency.getVersion() + ".pom");
126     }
127 
128     try
129     {
130       if (projectFile.exists() && projectFile.canRead())
131       {
132         return MavenUtils.getProject(
133           projectFile,
134           null,
135           false);
136       }
137       else
138       {
139         log.debug("Failed to read POM:" + projectFile);
140 
141         return null;
142       }
143     }
144     catch (Exception e)
145     {
146       log.debug("Failed to read POM:" + projectFile);
147 
148       return null;
149     }
150   }
151 }