1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 @Override
48 public void validate(final Task task) {
49 if (isReference()) {
50 getRef().validate(task);
51 } else {
52 if (file == null) {
53 throw new BuildException("You must specify the 'file' for the artifact");
54 } else if (!file.isFile()) {
55 throw new BuildException("The artifact file " + file + " does not exist");
56 }
57 if (type == null || type.length() <= 0) {
58 throw new BuildException("You must specify the 'type' for the artifact");
59 }
60 }
61 }
62
63 @Override
64 public void setRefid(final Reference ref) {
65 if (file != null || type != null || classifier != null) {
66 throw tooManyAttributes();
67 }
68 super.setRefid(ref);
69 }
70
71 public File getFile() {
72 if (isReference()) {
73 return getRef().getFile();
74 }
75 return file;
76 }
77
78 public void setFile(final File file) {
79 checkAttributesAllowed();
80 this.file = file;
81
82 if (file != null && type == null) {
83 final String name = file.getName();
84 final int period = name.lastIndexOf('.');
85 if (period >= 0) {
86 type = name.substring(period + 1);
87 }
88 }
89 }
90
91 public String getType() {
92 if (isReference()) {
93 return getRef().getType();
94 }
95 return (type != null) ? type : "jar";
96 }
97
98 public void setType(final String type) {
99 checkAttributesAllowed();
100 this.type = type;
101 }
102
103 public String getClassifier() {
104 if (isReference()) {
105 return getRef().getClassifier();
106 }
107 return (classifier != null) ? classifier : "";
108 }
109
110 public void setClassifier(final String classifier) {
111 checkAttributesAllowed();
112 this.classifier = classifier;
113 }
114
115 public void setPomRef(final Reference ref) {
116 checkAttributesAllowed();
117 final Pom pom = new Pom();
118 pom.setProject(getProject());
119 pom.setRefid(ref);
120 this.pom = pom;
121 }
122
123 public void addPom(final Pom pom) {
124 checkChildrenAllowed();
125 this.pom = pom;
126 }
127
128 public Pom getPom() {
129 if (isReference()) {
130 return getRef().getPom();
131 }
132 return pom;
133 }
134
135 @Override
136 public List<Artifact> getArtifacts() {
137 return Collections.singletonList(this);
138 }
139
140 @Override
141 public void execute() throws BuildException {
142 ProjectWorkspaceReader.getInstance().addArtifact(this);
143 }
144
145 @Override
146 public String toString() {
147 final String pomRepr = getPom() != null ? "(" + getPom().toString() + ":)" : "";
148 return String.format(pomRepr + "%s:%s", getType(), getClassifier());
149 }
150 }