1 | |
package org.apache.maven.ant.tasks; |
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.LinkedHashSet; |
24 | |
import java.util.Iterator; |
25 | |
import java.util.Set; |
26 | |
|
27 | |
import org.apache.maven.ant.tasks.support.SpecificScopesArtifactFilter; |
28 | |
import org.apache.maven.ant.tasks.support.TypesArtifactFilter; |
29 | |
import org.apache.maven.artifact.Artifact; |
30 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
31 | |
import org.apache.maven.artifact.resolver.filter.AndArtifactFilter; |
32 | |
import org.apache.maven.plugin.antrun.AntRunMojo; |
33 | |
import org.apache.maven.project.MavenProject; |
34 | |
import org.apache.tools.ant.BuildException; |
35 | |
import org.apache.tools.ant.Task; |
36 | |
import org.apache.tools.ant.types.FileSet; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
public class DependencyFilesetsTask |
45 | |
extends Task |
46 | |
{ |
47 | |
|
48 | |
public final static String DEFAULT_PROJECT_DEPENDENCIES_ID = "maven.project.dependencies"; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | 0 | private String mavenProjectId = AntRunMojo.DEFAULT_MAVEN_PROJECT_REFID; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | 0 | private String projectDependenciesId = DEFAULT_PROJECT_DEPENDENCIES_ID; |
59 | |
|
60 | |
public String getProjectDependenciesId() |
61 | |
{ |
62 | 0 | return projectDependenciesId; |
63 | |
} |
64 | |
|
65 | |
public void setProjectDependenciesId( String projectDependenciesId ) |
66 | |
{ |
67 | 0 | this.projectDependenciesId = projectDependenciesId; |
68 | 0 | } |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | 0 | private String prefix = ""; |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | 0 | private String types = ""; |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | 0 | private String scopes = ""; |
84 | |
|
85 | |
public DependencyFilesetsTask() |
86 | 0 | { |
87 | |
|
88 | 0 | } |
89 | |
|
90 | |
public void execute() |
91 | |
{ |
92 | 0 | if ( this.getProject().getReference( mavenProjectId ) == null ) |
93 | |
{ |
94 | 0 | throw new BuildException( "Maven project reference not found: " + mavenProjectId ); |
95 | |
} |
96 | |
|
97 | 0 | MavenProject mavenProject = (MavenProject) this.getProject().getReference( "maven.project" ); |
98 | |
|
99 | |
|
100 | 0 | Set depArtifacts = filterArtifacts( mavenProject.getArtifacts() ); |
101 | |
|
102 | 0 | FileSet dependenciesFileSet = new FileSet(); |
103 | 0 | dependenciesFileSet.setProject( getProject() ); |
104 | 0 | ArtifactRepository localRepository = (ArtifactRepository) getProject().getReference( "maven.local.repository" ); |
105 | 0 | dependenciesFileSet.setDir( new File( localRepository.getBasedir() ) ); |
106 | |
|
107 | 0 | for ( Iterator it = depArtifacts.iterator(); it.hasNext(); ) |
108 | |
{ |
109 | 0 | Artifact artifact = (Artifact) it.next(); |
110 | |
|
111 | 0 | String relativeArtifactPath = localRepository.pathOf( artifact ); |
112 | 0 | dependenciesFileSet.createInclude().setName( relativeArtifactPath ); |
113 | |
|
114 | 0 | String fileSetName = getPrefix() + artifact.getDependencyConflictId(); |
115 | |
|
116 | 0 | FileSet singleArtifactFileSet = new FileSet(); |
117 | 0 | singleArtifactFileSet.setProject( getProject() ); |
118 | 0 | singleArtifactFileSet.setFile( artifact.getFile() ); |
119 | 0 | getProject().addReference( fileSetName, singleArtifactFileSet ); |
120 | 0 | } |
121 | |
|
122 | 0 | getProject().addReference( ( getPrefix() + projectDependenciesId ), dependenciesFileSet ); |
123 | 0 | } |
124 | |
|
125 | |
public String getMavenProjectId() |
126 | |
{ |
127 | 0 | return mavenProjectId; |
128 | |
} |
129 | |
|
130 | |
public void setMavenProjectId( String mavenProjectId ) |
131 | |
{ |
132 | 0 | this.mavenProjectId = mavenProjectId; |
133 | 0 | } |
134 | |
|
135 | |
public String getPrefix() |
136 | |
{ |
137 | 0 | if ( prefix == null ) |
138 | |
{ |
139 | 0 | prefix = ""; |
140 | |
} |
141 | 0 | return prefix; |
142 | |
} |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
public void setPrefix( String prefix ) |
148 | |
{ |
149 | 0 | this.prefix = prefix; |
150 | 0 | } |
151 | |
|
152 | |
public String getTypes() |
153 | |
{ |
154 | 0 | return types; |
155 | |
} |
156 | |
|
157 | |
public void setTypes( String types ) |
158 | |
{ |
159 | 0 | this.types = types; |
160 | 0 | } |
161 | |
|
162 | |
public String getScopes() |
163 | |
{ |
164 | 0 | return scopes; |
165 | |
} |
166 | |
|
167 | |
public void setScopes( String scopes ) |
168 | |
{ |
169 | 0 | this.scopes = scopes; |
170 | 0 | } |
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
public Set filterArtifacts( Set artifacts ) |
179 | |
{ |
180 | 0 | if ( scopes == null ) |
181 | |
{ |
182 | 0 | scopes = ""; |
183 | |
} |
184 | 0 | if ( types == null ) |
185 | |
{ |
186 | 0 | types = ""; |
187 | |
} |
188 | |
|
189 | 0 | if ( scopes.equals( "" ) && types.equals( "" ) ) |
190 | |
{ |
191 | 0 | return artifacts; |
192 | |
} |
193 | |
|
194 | 0 | AndArtifactFilter filter = new AndArtifactFilter(); |
195 | 0 | if ( !scopes.equals( "" ) ) |
196 | |
{ |
197 | 0 | filter.add( new SpecificScopesArtifactFilter( getScopes() ) ); |
198 | |
} |
199 | 0 | if ( !types.equals( "" ) ) |
200 | |
{ |
201 | 0 | filter.add( new TypesArtifactFilter( getTypes() ) ); |
202 | |
} |
203 | |
|
204 | 0 | Set artifactsResult = new LinkedHashSet(); |
205 | 0 | for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) |
206 | |
{ |
207 | 0 | Artifact artifact = (Artifact) iter.next(); |
208 | 0 | if ( filter.include( artifact ) ) |
209 | |
{ |
210 | 0 | artifactsResult.add( artifact ); |
211 | |
} |
212 | 0 | } |
213 | 0 | return artifactsResult; |
214 | |
} |
215 | |
} |