View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.resolver.internal.ant.types;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.apache.tools.ant.BuildException;
28  import org.apache.tools.ant.Task;
29  import org.apache.tools.ant.types.DataType;
30  import org.apache.tools.ant.types.Reference;
31  
32  /**
33   */
34  public class Dependencies extends DataType implements DependencyContainer {
35  
36      private File file;
37  
38      private Pom pom;
39  
40      private List<DependencyContainer> containers = new ArrayList<>();
41  
42      private List<Exclusion> exclusions = new ArrayList<>();
43  
44      private boolean nestedDependencies;
45  
46      protected Dependencies getRef() {
47          return (Dependencies) getCheckedRef();
48      }
49  
50      public void validate(Task task) {
51          if (isReference()) {
52              getRef().validate(task);
53          } else {
54              if (getPom() != null && getPom().getFile() == null) {
55                  throw new BuildException("A <pom> used for dependency resolution has to be backed by a pom.xml file");
56              }
57              Map<String, String> ids = new HashMap<>();
58              for (DependencyContainer container : containers) {
59                  container.validate(task);
60                  if (container instanceof Dependency) {
61                      Dependency dependency = (Dependency) container;
62                      String id = dependency.getVersionlessKey();
63                      String collision = ids.put(id, dependency.getVersion());
64                      if (collision != null) {
65                          throw new BuildException("You must not declare multiple <dependency> elements"
66                                  + " with the same coordinates but got " + id + " -> " + collision + " vs "
67                                  + dependency.getVersion());
68                      }
69                  }
70              }
71          }
72      }
73  
74      public void setRefid(Reference ref) {
75          if (pom != null || !exclusions.isEmpty() || !containers.isEmpty()) {
76              throw noChildrenAllowed();
77          }
78          super.setRefid(ref);
79      }
80  
81      public void setFile(File file) {
82          checkAttributesAllowed();
83          this.file = file;
84          checkExternalSources();
85      }
86  
87      public File getFile() {
88          if (isReference()) {
89              return getRef().getFile();
90          }
91          return file;
92      }
93  
94      public void addPom(Pom pom) {
95          checkChildrenAllowed();
96          if (this.pom != null) {
97              throw new BuildException("You must not specify multiple <pom> elements");
98          }
99          this.pom = pom;
100         checkExternalSources();
101     }
102 
103     public Pom getPom() {
104         if (isReference()) {
105             return getRef().getPom();
106         }
107         return pom;
108     }
109 
110     public void setPomRef(Reference ref) {
111         if (pom == null) {
112             pom = new Pom();
113             pom.setProject(getProject());
114         }
115         pom.setRefid(ref);
116         checkExternalSources();
117     }
118 
119     private void checkExternalSources() {
120         if (file != null && pom != null) {
121             throw new BuildException("You must not specify both a text file and a POM to list dependencies");
122         }
123         if ((file != null || pom != null) && nestedDependencies) {
124             throw new BuildException("You must not specify both a file/POM and nested dependency collections");
125         }
126     }
127 
128     public void addDependency(Dependency dependency) {
129         checkChildrenAllowed();
130         containers.add(dependency);
131     }
132 
133     public void addDependencies(Dependencies dependencies) {
134         checkChildrenAllowed();
135         if (dependencies == this) {
136             throw circularReference();
137         }
138         containers.add(dependencies);
139         nestedDependencies = true;
140         checkExternalSources();
141     }
142 
143     public List<DependencyContainer> getDependencyContainers() {
144         if (isReference()) {
145             return getRef().getDependencyContainers();
146         }
147         return containers;
148     }
149 
150     public void addExclusion(Exclusion exclusion) {
151         checkChildrenAllowed();
152         this.exclusions.add(exclusion);
153     }
154 
155     public List<Exclusion> getExclusions() {
156         if (isReference()) {
157             return getRef().getExclusions();
158         }
159         return exclusions;
160     }
161 }