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 final List<DependencyContainer> containers = new ArrayList<>();
41  
42      private final List<Exclusion> exclusions = new ArrayList<>();
43  
44      private boolean nestedDependencies;
45  
46      protected Dependencies getRef() {
47          return getCheckedRef(Dependencies.class);
48      }
49  
50      @Override
51      public void validate(Task task) {
52          if (isReference()) {
53              getRef().validate(task);
54          } else {
55              if (getPom() != null && getPom().getFile() == null) {
56                  throw new BuildException("A <pom> used for dependency resolution has to be backed by a pom.xml file");
57              }
58              Map<String, String> ids = new HashMap<>();
59              for (DependencyContainer container : containers) {
60                  container.validate(task);
61                  if (container instanceof Dependency) {
62                      Dependency dependency = (Dependency) container;
63                      String id = dependency.getVersionlessKey();
64                      String collision = ids.put(id, dependency.getVersion());
65                      if (collision != null) {
66                          throw new BuildException("You must not declare multiple <dependency> elements"
67                                  + " with the same coordinates but got " + id + " -> " + collision + " vs "
68                                  + dependency.getVersion());
69                      }
70                  }
71              }
72          }
73      }
74  
75      @Override
76      public void setRefid(Reference ref) {
77          if (pom != null || !exclusions.isEmpty() || !containers.isEmpty()) {
78              throw noChildrenAllowed();
79          }
80          super.setRefid(ref);
81      }
82  
83      public void setFile(File file) {
84          checkAttributesAllowed();
85          this.file = file;
86          checkExternalSources();
87      }
88  
89      public File getFile() {
90          if (isReference()) {
91              return getRef().getFile();
92          }
93          return file;
94      }
95  
96      public void addPom(Pom pom) {
97          checkChildrenAllowed();
98          if (this.pom != null) {
99              throw new BuildException("You must not specify multiple <pom> elements");
100         }
101         this.pom = pom;
102         checkExternalSources();
103     }
104 
105     public Pom getPom() {
106         if (isReference()) {
107             return getRef().getPom();
108         }
109         return pom;
110     }
111 
112     public void setPomRef(Reference ref) {
113         if (pom == null) {
114             pom = new Pom();
115             pom.setProject(getProject());
116         }
117         pom.setRefid(ref);
118         checkExternalSources();
119     }
120 
121     private void checkExternalSources() {
122         if (file != null && pom != null) {
123             throw new BuildException("You must not specify both a text file and a POM to list dependencies");
124         }
125         if ((file != null || pom != null) && nestedDependencies) {
126             throw new BuildException("You must not specify both a file/POM and nested dependency collections");
127         }
128     }
129 
130     public void addDependency(Dependency dependency) {
131         checkChildrenAllowed();
132         containers.add(dependency);
133     }
134 
135     public void addDependencies(Dependencies dependencies) {
136         checkChildrenAllowed();
137         if (dependencies == this) {
138             throw circularReference();
139         }
140         containers.add(dependencies);
141         nestedDependencies = true;
142         checkExternalSources();
143     }
144 
145     public List<DependencyContainer> getDependencyContainers() {
146         if (isReference()) {
147             return getRef().getDependencyContainers();
148         }
149         return containers;
150     }
151 
152     public void addExclusion(Exclusion exclusion) {
153         checkChildrenAllowed();
154         this.exclusions.add(exclusion);
155     }
156 
157     public List<Exclusion> getExclusions() {
158         if (isReference()) {
159             return getRef().getExclusions();
160         }
161         return exclusions;
162     }
163 }