]> source.dussan.org Git - archiva.git/blob
19bc0c85a21e63f39a295c1227e383d9e5ac8d06
[archiva.git] /
1 package org.apache.maven.archiva.plugins.dev.testgen;
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.factory.ArtifactFactory;
24 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
25 import org.apache.maven.artifact.versioning.VersionRange;
26 import org.apache.maven.model.Dependency;
27 import org.apache.maven.model.DependencyManagement;
28 import org.apache.maven.project.MavenProject;
29 import org.apache.maven.project.ProjectBuildingException;
30
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.Iterator;
34 import java.util.Map;
35
36 /**
37  * DependencyUtils - common utilities for dependencies. 
38  *
39  * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
40  * @version $Id$
41  */
42 public class DependencyUtils
43 {
44     public static Map getManagedVersionMap( MavenProject project, ArtifactFactory factory ) throws ProjectBuildingException
45     {
46         DependencyManagement dependencyManagement = project.getDependencyManagement();
47         Map managedVersionMap;
48
49         if ( dependencyManagement != null && dependencyManagement.getDependencies() != null )
50         {
51             managedVersionMap = new HashMap();
52
53             for ( Iterator iterator = dependencyManagement.getDependencies().iterator(); iterator.hasNext(); )
54             {
55                 Dependency dependency = (Dependency) iterator.next();
56
57                 try
58                 {
59                     VersionRange versionRange = VersionRange.createFromVersionSpec( dependency.getVersion() );
60
61                     Artifact artifact =
62                         factory.createDependencyArtifact( dependency.getGroupId(), dependency.getArtifactId(),
63                                                           versionRange, dependency.getType(),
64                                                           dependency.getClassifier(), dependency.getScope() );
65
66                     managedVersionMap.put( dependency.getManagementKey(), artifact );
67                 }
68                 catch ( InvalidVersionSpecificationException exception )
69                 {
70                     throw new ProjectBuildingException( project.getId(), "Unable to parse version '"
71                                     + dependency.getVersion() + "' for dependency '" + dependency.getManagementKey()
72                                     + "': " + exception.getMessage(), exception );
73                 }
74             }
75         }
76         else
77         {
78             managedVersionMap = Collections.EMPTY_MAP;
79         }
80
81         return managedVersionMap;
82     }
83 }