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