View Javadoc
1   package org.apache.maven.resolver.internal.ant.tasks;
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.HashMap;
24  import java.util.Map;
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.types.Artifact;
29  import org.apache.maven.resolver.internal.ant.types.Artifacts;
30  import org.apache.maven.resolver.internal.ant.types.Pom;
31  import org.apache.tools.ant.BuildException;
32  import org.apache.tools.ant.Project;
33  import org.apache.tools.ant.Task;
34  import org.apache.tools.ant.types.Reference;
35  
36  /**
37   */
38  public abstract class AbstractDistTask
39      extends Task
40  {
41  
42      private Pom pom;
43  
44      private Artifacts artifacts;
45  
46      protected void validate()
47      {
48          getArtifacts().validate( this );
49  
50          Map<String, File> duplicates = new HashMap<String, File>();
51          for ( Artifact artifact : getArtifacts().getArtifacts() )
52          {
53              String key = artifact.getType() + ':' + artifact.getClassifier();
54              if ( "pom:".equals( key ) )
55              {
56                  throw new BuildException( "You must not specify an <artifact> with type=pom"
57                      + ", please use the <pom> element instead." );
58              }
59              else if ( duplicates.containsKey( key ) )
60              {
61                  throw new BuildException( "You must not specify two or more artifacts with the same type ("
62                      + artifact.getType() + ") and classifier (" + artifact.getClassifier() + ")" );
63              }
64              else
65              {
66                  duplicates.put( key, artifact.getFile() );
67              }
68  
69              validateArtifactGav( artifact );
70          }
71  
72          Pom defaultPom = AntRepoSys.getInstance( getProject() ).getDefaultPom();
73          if ( pom == null && defaultPom != null )
74          {
75              log( "Using default POM (" + defaultPom.getCoords() + ")", Project.MSG_INFO );
76              pom = defaultPom;
77          }
78  
79          if ( pom == null )
80          {
81              throw new BuildException( "You must specify the <pom file=\"...\"> element"
82                  + " to denote the descriptor for the artifacts" );
83          }
84          if ( pom.getFile() == null )
85          {
86              throw new BuildException( "You must specify a <pom> element that has the 'file' attribute set" );
87          }
88      }
89  
90      private void validateArtifactGav( Artifact artifact )
91      {
92          Pom artifactPom = artifact.getPom();
93          if ( artifactPom != null )
94          {
95              String gid;
96              String aid;
97              String version;
98              if ( artifactPom.getFile() != null )
99              {
100                 Model model = artifactPom.getModel( this );
101                 gid = model.getGroupId();
102                 aid = model.getArtifactId();
103                 version = model.getVersion();
104             }
105             else
106             {
107                 gid = artifactPom.getGroupId();
108                 aid = artifactPom.getArtifactId();
109                 version = artifactPom.getVersion();
110             }
111             
112             Model model = getPom().getModel( this );
113             
114             if ( ! ( model.getGroupId().equals( gid ) && model.getArtifactId().equals( aid ) && model.getVersion().equals( version ) ) )
115             {
116                 throw new BuildException( "Artifact references different pom than it would be installed with: "
117                     + artifact.toString() );
118             }
119         }
120     }
121 
122     protected Artifacts getArtifacts()
123     {
124         if ( artifacts == null )
125         {
126             artifacts = new Artifacts();
127             artifacts.setProject( getProject() );
128         }
129         return artifacts;
130     }
131 
132     public void addArtifact( Artifact artifact )
133     {
134         getArtifacts().addArtifact( artifact );
135     }
136 
137     public void addArtifacts( Artifacts artifacts )
138     {
139         getArtifacts().addArtifacts( artifacts );
140     }
141 
142     public void setArtifactsRef( Reference ref )
143     {
144         Artifacts artifacts = new Artifacts();
145         artifacts.setProject( getProject() );
146         artifacts.setRefid( ref );
147         getArtifacts().addArtifacts( artifacts );
148     }
149 
150     protected Pom getPom()
151     {
152         if ( pom == null )
153         {
154             return AntRepoSys.getInstance( getProject() ).getDefaultPom();
155         }
156 
157         return pom;
158     }
159 
160     public void addPom( Pom pom )
161     {
162         if ( this.pom != null )
163         {
164             throw new BuildException( "You must not specify multiple <pom> elements" );
165         }
166         this.pom = pom;
167     }
168 
169     public void setPomRef( Reference ref )
170     {
171         if ( this.pom != null )
172         {
173             throw new BuildException( "You must not specify multiple <pom> elements" );
174         }
175         pom = new Pom();
176         pom.setProject( getProject() );
177         pom.setRefid( ref );
178     }
179 
180 }