1 package org.apache.maven.resolver.internal.ant.types;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.util.Collections;
24 import java.util.List;
25
26 import org.apache.maven.resolver.internal.ant.ProjectWorkspaceReader;
27 import org.apache.maven.resolver.internal.ant.tasks.RefTask;
28 import org.apache.tools.ant.BuildException;
29 import org.apache.tools.ant.Task;
30 import org.apache.tools.ant.types.Reference;
31
32
33
34 public class Artifact
35 extends RefTask
36 implements ArtifactContainer
37 {
38
39 private File file;
40
41 private String type;
42
43 private String classifier;
44
45 private Pom pom;
46
47 protected Artifact getRef()
48 {
49 return (Artifact) getCheckedRef();
50 }
51
52 public void validate( Task task )
53 {
54 if ( isReference() )
55 {
56 getRef().validate( task );
57 }
58 else
59 {
60 if ( file == null )
61 {
62 throw new BuildException( "You must specify the 'file' for the artifact" );
63 }
64 else if ( !file.isFile() )
65 {
66 throw new BuildException( "The artifact file " + file + " does not exist" );
67 }
68 if ( type == null || type.length() <= 0 )
69 {
70 throw new BuildException( "You must specify the 'type' for the artifact" );
71 }
72 }
73 }
74
75 public void setRefid( Reference ref )
76 {
77 if ( file != null || type != null || classifier != null )
78 {
79 throw tooManyAttributes();
80 }
81 super.setRefid( ref );
82 }
83
84 public File getFile()
85 {
86 if ( isReference() )
87 {
88 return getRef().getFile();
89 }
90 return file;
91 }
92
93 public void setFile( File file )
94 {
95 checkAttributesAllowed();
96 this.file = file;
97
98 if ( file != null && type == null )
99 {
100 String name = file.getName();
101 int period = name.lastIndexOf( '.' );
102 if ( period >= 0 )
103 {
104 type = name.substring( period + 1 );
105 }
106 }
107 }
108
109 public String getType()
110 {
111 if ( isReference() )
112 {
113 return getRef().getType();
114 }
115 return ( type != null ) ? type : "jar";
116 }
117
118 public void setType( String type )
119 {
120 checkAttributesAllowed();
121 this.type = type;
122 }
123
124 public String getClassifier()
125 {
126 if ( isReference() )
127 {
128 return getRef().getClassifier();
129 }
130 return ( classifier != null ) ? classifier : "";
131 }
132
133 public void setClassifier( String classifier )
134 {
135 checkAttributesAllowed();
136 this.classifier = classifier;
137 }
138
139 public void setPomRef( Reference ref )
140 {
141 checkAttributesAllowed();
142 Pom pom = new Pom();
143 pom.setProject( getProject() );
144 pom.setRefid( ref );
145 this.pom = pom;
146 }
147
148 public void addPom( Pom pom )
149 {
150 checkChildrenAllowed();
151 this.pom = pom;
152 }
153
154 public Pom getPom()
155 {
156 if ( isReference() )
157 {
158 return getRef().getPom();
159 }
160 return pom;
161 }
162
163 public List<Artifact> getArtifacts()
164 {
165 return Collections.singletonList( this );
166 }
167
168 @Override
169 public void execute()
170 throws BuildException
171 {
172 ProjectWorkspaceReader.getInstance().addArtifact( this );
173 }
174
175 public String toString()
176 {
177 String pomRepr = getPom() != null ? "(" + getPom().toString() + ":)" : "";
178 return String.format( pomRepr + "%s:%s", getType(), getClassifier() );
179 }
180
181 }