View Javadoc
1   package org.apache.maven.resolver.internal.ant.types;
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.io.File;
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.apache.tools.ant.BuildException;
29  import org.apache.tools.ant.Task;
30  import org.apache.tools.ant.types.DataType;
31  import org.apache.tools.ant.types.Reference;
32  
33  /**
34   */
35  public class Dependencies
36      extends DataType
37      implements DependencyContainer
38  {
39  
40      private File file;
41  
42      private Pom pom;
43  
44      private List<DependencyContainer> containers = new ArrayList<DependencyContainer>();
45  
46      private List<Exclusion> exclusions = new ArrayList<Exclusion>();
47  
48      private boolean nestedDependencies;
49  
50      protected Dependencies getRef()
51      {
52          return (Dependencies) getCheckedRef();
53      }
54  
55      public void validate( Task task )
56      {
57          if ( isReference() )
58          {
59              getRef().validate( task );
60          }
61          else
62          {
63              if ( getPom() != null && getPom().getFile() == null )
64              {
65                  throw new BuildException( "A <pom> used for dependency resolution has to be backed by a pom.xml file" );
66              }
67              Map<String, String> ids = new HashMap<String, String>();
68              for ( DependencyContainer container : containers )
69              {
70                  container.validate( task );
71                  if ( container instanceof Dependency )
72                  {
73                      Dependency dependency = (Dependency) container;
74                      String id = dependency.getVersionlessKey();
75                      String collision = ids.put( id, dependency.getVersion() );
76                      if ( collision != null )
77                      {
78                          throw new BuildException( "You must not declare multiple <dependency> elements"
79                              + " with the same coordinates but got " + id + " -> " + collision + " vs "
80                              + dependency.getVersion() );
81                      }
82                  }
83              }
84          }
85      }
86  
87      public void setRefid( Reference ref )
88      {
89          if ( pom != null || !exclusions.isEmpty() || !containers.isEmpty() )
90          {
91              throw noChildrenAllowed();
92          }
93          super.setRefid( ref );
94      }
95  
96      public void setFile( File file )
97      {
98          checkAttributesAllowed();
99          this.file = file;
100         checkExternalSources();
101     }
102 
103     public File getFile()
104     {
105         if ( isReference() )
106         {
107             return getRef().getFile();
108         }
109         return file;
110     }
111 
112     public void addPom( Pom pom )
113     {
114         checkChildrenAllowed();
115         if ( this.pom != null )
116         {
117             throw new BuildException( "You must not specify multiple <pom> elements" );
118         }
119         this.pom = pom;
120         checkExternalSources();
121     }
122 
123     public Pom getPom()
124     {
125         if ( isReference() )
126         {
127             return getRef().getPom();
128         }
129         return pom;
130     }
131 
132     public void setPomRef( Reference ref )
133     {
134         if ( pom == null )
135         {
136             pom = new Pom();
137             pom.setProject( getProject() );
138         }
139         pom.setRefid( ref );
140         checkExternalSources();
141     }
142 
143     private void checkExternalSources()
144     {
145         if ( file != null && pom != null )
146         {
147             throw new BuildException( "You must not specify both a text file and a POM to list dependencies" );
148         }
149         if ( ( file != null || pom != null ) && nestedDependencies )
150         {
151             throw new BuildException( "You must not specify both a file/POM and nested dependency collections" );
152         }
153     }
154 
155     public void addDependency( Dependency dependency )
156     {
157         checkChildrenAllowed();
158         containers.add( dependency );
159     }
160 
161     public void addDependencies( Dependencies dependencies )
162     {
163         checkChildrenAllowed();
164         if ( dependencies == this )
165         {
166             throw circularReference();
167         }
168         containers.add( dependencies );
169         nestedDependencies = true;
170         checkExternalSources();
171     }
172 
173     public List<DependencyContainer> getDependencyContainers()
174     {
175         if ( isReference() )
176         {
177             return getRef().getDependencyContainers();
178         }
179         return containers;
180     }
181 
182     public void addExclusion( Exclusion exclusion )
183     {
184         checkChildrenAllowed();
185         this.exclusions.add( exclusion );
186     }
187 
188     public List<Exclusion> getExclusions()
189     {
190         if ( isReference() )
191         {
192             return getRef().getExclusions();
193         }
194         return exclusions;
195     }
196 
197 }