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.regex.Matcher;
23  import java.util.regex.Pattern;
24  
25  import org.apache.maven.model.Model;
26  import org.apache.maven.resolver.internal.ant.AntRepoSys;
27  import org.apache.maven.resolver.internal.ant.ProjectWorkspaceReader;
28  import org.apache.maven.resolver.internal.ant.tasks.RefTask;
29  import org.apache.tools.ant.BuildException;
30  import org.apache.tools.ant.PropertyHelper;
31  import org.apache.tools.ant.Task;
32  import org.apache.tools.ant.types.Reference;
33  
34  /**
35   */
36  public class Pom extends RefTask {
37  
38      private Model model;
39  
40      private String id;
41  
42      private File file;
43  
44      private String groupId;
45  
46      private String artifactId;
47  
48      private String version;
49  
50      private String packaging = "jar";
51  
52      private RemoteRepositories remoteRepositories;
53  
54      private String coords;
55  
56      protected Pom getRef() {
57          return (Pom) getCheckedRef();
58      }
59  
60      public void validate() {
61          if (isReference()) {
62              getRef().validate();
63          } else {
64              if (file == null) {
65                  if (groupId == null) {
66                      throw new BuildException("You must specify the 'groupId' for the POM");
67                  }
68                  if (artifactId == null) {
69                      throw new BuildException("You must specify the 'artifactId' for the POM");
70                  }
71                  if (version == null) {
72                      throw new BuildException("You must specify the 'version' for the POM");
73                  }
74              }
75          }
76      }
77  
78      @Override
79      public void setRefid(Reference ref) {
80          if (id != null || file != null || groupId != null || artifactId != null || version != null) {
81              throw tooManyAttributes();
82          }
83          if (remoteRepositories != null) {
84              throw noChildrenAllowed();
85          }
86          super.setRefid(ref);
87      }
88  
89      public void setId(String id) {
90          checkAttributesAllowed();
91          this.id = id;
92      }
93  
94      public File getFile() {
95          if (isReference()) {
96              return getRef().getFile();
97          }
98          return file;
99      }
100 
101     public void setFile(File file) {
102         checkAttributesAllowed();
103         if (groupId != null || artifactId != null || version != null) {
104             throw ambiguousSource();
105         }
106 
107         this.file = file;
108     }
109 
110     public String getGroupId() {
111         if (isReference()) {
112             return getRef().getGroupId();
113         }
114         return groupId;
115     }
116 
117     public void setGroupId(String groupId) {
118         checkAttributesAllowed();
119         if (this.groupId != null) {
120             throw ambiguousCoords();
121         }
122         if (file != null) {
123             throw ambiguousSource();
124         }
125         this.groupId = groupId;
126     }
127 
128     public String getArtifactId() {
129         if (isReference()) {
130             return getRef().getArtifactId();
131         }
132         return artifactId;
133     }
134 
135     public void setArtifactId(String artifactId) {
136         checkAttributesAllowed();
137         if (this.artifactId != null) {
138             throw ambiguousCoords();
139         }
140         if (file != null) {
141             throw ambiguousSource();
142         }
143         this.artifactId = artifactId;
144     }
145 
146     public String getVersion() {
147         if (isReference()) {
148             return getRef().getVersion();
149         }
150         return version;
151     }
152 
153     public void setVersion(String version) {
154         checkAttributesAllowed();
155         if (this.version != null) {
156             throw ambiguousCoords();
157         }
158         if (file != null) {
159             throw ambiguousSource();
160         }
161         this.version = version;
162     }
163 
164     public String getCoords() {
165         if (isReference()) {
166             return getRef().getCoords();
167         }
168         return coords;
169     }
170 
171     public void setCoords(String coords) {
172         checkAttributesAllowed();
173         if (file != null) {
174             throw ambiguousSource();
175         }
176         if (groupId != null || artifactId != null || version != null) {
177             throw ambiguousCoords();
178         }
179         Pattern p = Pattern.compile("([^: ]+):([^: ]+):([^: ]+)");
180         Matcher m = p.matcher(coords);
181         if (!m.matches()) {
182             throw new BuildException("Bad POM coordinates, expected format is <groupId>:<artifactId>:<version>");
183         }
184         groupId = m.group(1);
185         artifactId = m.group(2);
186         version = m.group(3);
187     }
188 
189     private BuildException ambiguousCoords() {
190         return new BuildException("You must not specify both 'coords' and ('groupId', 'artifactId', 'version')");
191     }
192 
193     private BuildException ambiguousSource() {
194         return new BuildException(
195                 "You must not specify both 'file' and " + "('coords', 'groupId', 'artifactId', 'version')");
196     }
197 
198     public String getPackaging() {
199         if (isReference()) {
200             return getRef().getPackaging();
201         }
202         return packaging;
203     }
204 
205     public void setPackaging(String packaging) {
206         checkAttributesAllowed();
207         if (file != null) {
208             throw ambiguousSource();
209         }
210         this.packaging = packaging;
211     }
212 
213     private RemoteRepositories getRemoteRepos() {
214         if (remoteRepositories == null) {
215             remoteRepositories = new RemoteRepositories();
216             remoteRepositories.setProject(getProject());
217         }
218         return remoteRepositories;
219     }
220 
221     public void addRemoteRepo(RemoteRepository repository) {
222         getRemoteRepos().addRemoterepo(repository);
223     }
224 
225     public void addRemoteRepos(RemoteRepositories repositories) {
226         getRemoteRepos().addRemoterepos(repositories);
227     }
228 
229     public void setRemoteReposRef(Reference ref) {
230         RemoteRepositories repos = new RemoteRepositories();
231         repos.setProject(getProject());
232         repos.setRefid(ref);
233         getRemoteRepos().addRemoterepos(repos);
234     }
235 
236     public Model getModel(Task task) {
237         if (isReference()) {
238             return getRef().getModel(task);
239         }
240         synchronized (this) {
241             if (model == null) {
242                 if (file != null) {
243                     model = AntRepoSys.getInstance(getProject()).loadModel(task, file, true, remoteRepositories);
244                 }
245             }
246             return model;
247         }
248     }
249 
250     @Override
251     public void execute() {
252         validate();
253 
254         if (file != null && (id == null || AntRepoSys.getInstance(getProject()).getDefaultPom() == null)) {
255             AntRepoSys.getInstance(getProject()).setDefaultPom(this);
256         }
257 
258         ProjectWorkspaceReader.getInstance().addPom(this);
259 
260         Model model = getModel(this);
261 
262         if (model == null) {
263             coords = getGroupId() + ":" + getArtifactId() + ":" + getVersion();
264             return;
265         }
266 
267         coords = model.getGroupId() + ":" + model.getArtifactId() + ":" + model.getVersion();
268 
269         ModelValueExtractor extractor = new ModelValueExtractor(id, model, getProject());
270 
271         PropertyHelper propHelper = PropertyHelper.getPropertyHelper(getProject());
272 
273         try {
274             // Ant 1.8.0 delegate
275             PomPropertyEvaluator.register(extractor, propHelper);
276         } catch (LinkageError e) {
277             // Ant 1.6 - 1.7.1 interceptor chaining
278             PomPropertyHelper.register(extractor, propHelper);
279         }
280     }
281 
282     @Override
283     public String toString() {
284         return coords + " (" + super.toString() + ")";
285     }
286 }