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.Collections;
23  import java.util.List;
24  
25  import org.apache.maven.resolver.internal.ant.ProjectWorkspaceReader;
26  import org.apache.maven.resolver.internal.ant.tasks.RefTask;
27  import org.apache.tools.ant.BuildException;
28  import org.apache.tools.ant.Task;
29  import org.apache.tools.ant.types.Reference;
30  
31  /**
32   */
33  public class Artifact extends RefTask implements ArtifactContainer {
34  
35      private File file;
36  
37      private String type;
38  
39      private String classifier;
40  
41      private Pom pom;
42  
43      protected Artifact getRef() {
44          return (Artifact) getCheckedRef();
45      }
46  
47      public void validate(final Task task) {
48          if (isReference()) {
49              getRef().validate(task);
50          } else {
51              if (file == null) {
52                  throw new BuildException("You must specify the 'file' for the artifact");
53              } else if (!file.isFile()) {
54                  throw new BuildException("The artifact file " + file + " does not exist");
55              }
56              if (type == null || type.length() <= 0) {
57                  throw new BuildException("You must specify the 'type' for the artifact");
58              }
59          }
60      }
61  
62      public void setRefid(final Reference ref) {
63          if (file != null || type != null || classifier != null) {
64              throw tooManyAttributes();
65          }
66          super.setRefid(ref);
67      }
68  
69      public File getFile() {
70          if (isReference()) {
71              return getRef().getFile();
72          }
73          return file;
74      }
75  
76      public void setFile(final File file) {
77          checkAttributesAllowed();
78          this.file = file;
79  
80          if (file != null && type == null) {
81              final String name = file.getName();
82              final int period = name.lastIndexOf('.');
83              if (period >= 0) {
84                  type = name.substring(period + 1);
85              }
86          }
87      }
88  
89      public String getType() {
90          if (isReference()) {
91              return getRef().getType();
92          }
93          return (type != null) ? type : "jar";
94      }
95  
96      public void setType(final String type) {
97          checkAttributesAllowed();
98          this.type = type;
99      }
100 
101     public String getClassifier() {
102         if (isReference()) {
103             return getRef().getClassifier();
104         }
105         return (classifier != null) ? classifier : "";
106     }
107 
108     public void setClassifier(final String classifier) {
109         checkAttributesAllowed();
110         this.classifier = classifier;
111     }
112 
113     public void setPomRef(final Reference ref) {
114         checkAttributesAllowed();
115         final Pom pom = new Pom();
116         pom.setProject(getProject());
117         pom.setRefid(ref);
118         this.pom = pom;
119     }
120 
121     public void addPom(final Pom pom) {
122         checkChildrenAllowed();
123         this.pom = pom;
124     }
125 
126     public Pom getPom() {
127         if (isReference()) {
128             return getRef().getPom();
129         }
130         return pom;
131     }
132 
133     public List<Artifact> getArtifacts() {
134         return Collections.singletonList(this);
135     }
136 
137     @Override
138     public void execute() throws BuildException {
139         ProjectWorkspaceReader.getInstance().addArtifact(this);
140     }
141 
142     public String toString() {
143         final String pomRepr = getPom() != null ? "(" + getPom().toString() + ":)" : "";
144         return String.format(pomRepr + "%s:%s", getType(), getClassifier());
145     }
146 }