KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > maven > plugin > jbi > AbstractJbiMojo


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.maven.plugin.jbi;
18
19 import java.io.File JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import org.apache.maven.artifact.Artifact;
29 import org.apache.maven.artifact.factory.ArtifactFactory;
30 import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
31 import org.apache.maven.artifact.repository.ArtifactRepository;
32 import org.apache.maven.artifact.resolver.ArtifactCollector;
33 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
34 import org.apache.maven.artifact.resolver.ArtifactResolver;
35 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
36 import org.apache.maven.artifact.versioning.VersionRange;
37 import org.apache.maven.model.Dependency;
38 import org.apache.maven.model.DependencyManagement;
39 import org.apache.maven.plugin.AbstractMojo;
40 import org.apache.maven.project.MavenProject;
41 import org.apache.maven.project.MavenProjectBuilder;
42 import org.apache.maven.project.MavenProjectHelper;
43 import org.apache.maven.project.ProjectBuildingException;
44 import org.apache.servicemix.maven.plugin.jbi.JbiResolutionListener.Node;
45
46 public abstract class AbstractJbiMojo extends AbstractMojo {
47
48     public static final String JavaDoc META_INF = "META-INF";
49
50     public static final String JavaDoc JBI_DESCRIPTOR = "jbi.xml";
51
52     public static final String JavaDoc LIB_DIRECTORY = "lib";
53
54     /**
55      * Maven ProjectHelper
56      *
57      * @component
58      */

59     protected MavenProjectHelper projectHelper;
60
61     /**
62      * The maven project.
63      *
64      * @parameter expression="${project}"
65      * @required
66      * @readonly
67      */

68     protected MavenProject project;
69
70     /**
71      * Directory that resources are copied to during the build.
72      *
73      * @parameter expression="${project.build.directory}/${project.artifactId}-${project.version}-installer"
74      * @required
75      */

76     protected File JavaDoc workDirectory;
77
78     /**
79      * @component
80      */

81     protected MavenProjectBuilder projectBuilder;
82
83     /**
84      * @parameter default-value="${localRepository}"
85      */

86     protected ArtifactRepository localRepo;
87
88     /**
89      * @parameter default-value="${project.remoteArtifactRepositories}"
90      */

91     protected List JavaDoc remoteRepos;
92
93     /**
94      * @component
95      */

96     protected ArtifactMetadataSource artifactMetadataSource;
97
98     /**
99      * @component
100      */

101     protected ArtifactResolver resolver;
102
103     /**
104      * @component
105      */

106     protected ArtifactCollector collector;
107
108     /**
109      * @component
110      */

111     protected ArtifactFactory factory;
112
113     protected MavenProject getProject() {
114         return project;
115     }
116
117     protected File JavaDoc getWorkDirectory() {
118         return workDirectory;
119     }
120
121     public MavenProjectHelper getProjectHelper() {
122         return projectHelper;
123     }
124
125     protected void removeBranch(JbiResolutionListener listener,
126             Artifact artifact) {
127         Node n = listener.getNode(artifact);
128         if (n != null && n.getParent() != null) {
129             n.getParent().getChildren().remove(n);
130         }
131     }
132
133     protected void removeChildren(JbiResolutionListener listener,
134             Artifact artifact) {
135         Node n = listener.getNode(artifact);
136         n.getChildren().clear();
137     }
138
139     protected Set JavaDoc getArtifacts(Node n, Set JavaDoc s) {
140         s.add(n.getArtifact());
141         for (Iterator JavaDoc iter = n.getChildren().iterator(); iter.hasNext();) {
142             Node c = (Node) iter.next();
143             getArtifacts(c, s);
144         }
145         return s;
146     }
147
148     protected void excludeBranch(Node n, Set JavaDoc excludes) {
149         excludes.add(n);
150         for (Iterator JavaDoc iter = n.getChildren().iterator(); iter.hasNext();) {
151             Node c = (Node) iter.next();
152             excludeBranch(c, excludes);
153         }
154     }
155
156     protected void print(Node rootNode, String JavaDoc string) {
157         getLog().info(string + rootNode.getArtifact());
158         for (Iterator JavaDoc iter = rootNode.getChildren().iterator(); iter.hasNext();) {
159             Node n = (Node) iter.next();
160             print(n, string + " ");
161         }
162     }
163
164     protected Set JavaDoc retainArtifacts(Set JavaDoc includes, JbiResolutionListener listener) {
165         HashSet JavaDoc finalIncludes = new HashSet JavaDoc();
166         Set JavaDoc filteredArtifacts = getArtifacts(listener.getRootNode(),
167                 new HashSet JavaDoc());
168         for (Iterator JavaDoc iter = includes.iterator(); iter.hasNext();) {
169             Artifact artifact = (Artifact) iter.next();
170             for (Iterator JavaDoc iter2 = filteredArtifacts.iterator(); iter2.hasNext();) {
171                 Artifact filteredArtifact = (Artifact) iter2.next();
172                 if (filteredArtifact.getArtifactId().equals(
173                         artifact.getArtifactId())
174                         && filteredArtifact.getType()
175                                 .equals(artifact.getType())
176                         && filteredArtifact.getGroupId().equals(
177                                 artifact.getGroupId())) {
178                     if (!filteredArtifact.getVersion().equals(
179                             artifact.getVersion())) {
180                         getLog()
181                                 .warn(
182                                         "Resolved artifact "
183                                                 + artifact
184                                                 + " has a different version from that in dependency management "
185                                                 + filteredArtifact
186                                                 + ", overriding dependency management");
187                     }
188                     finalIncludes.add(artifact);
189                 }
190             }
191
192         }
193
194         return finalIncludes;
195     }
196
197     protected JbiResolutionListener resolveProject() {
198         Map JavaDoc managedVersions = null;
199         try {
200             managedVersions = createManagedVersionMap(project.getId(), project
201                     .getDependencyManagement());
202         } catch (ProjectBuildingException e) {
203             getLog().error(
204                     "An error occurred while resolving project dependencies.",
205                     e);
206         }
207         JbiResolutionListener listener = new JbiResolutionListener();
208         try {
209             collector.collect(project.getDependencyArtifacts(), project
210                     .getArtifact(), managedVersions, localRepo, remoteRepos,
211                     artifactMetadataSource, null, Collections
212                             .singletonList(listener));
213         } catch (ArtifactResolutionException e) {
214             getLog().error(
215                     "An error occurred while resolving project dependencies.",
216                     e);
217         }
218         return listener;
219     }
220
221     protected Map JavaDoc createManagedVersionMap(String JavaDoc projectId,
222             DependencyManagement dependencyManagement)
223             throws ProjectBuildingException {
224         Map JavaDoc map;
225         if (dependencyManagement != null
226                 && dependencyManagement.getDependencies() != null) {
227             map = new HashMap JavaDoc();
228             for (Iterator JavaDoc i = dependencyManagement.getDependencies().iterator(); i
229                     .hasNext();) {
230                 Dependency d = (Dependency) i.next();
231
232                 try {
233                     VersionRange versionRange = VersionRange
234                             .createFromVersionSpec(d.getVersion());
235                     Artifact artifact = factory.createDependencyArtifact(d
236                             .getGroupId(), d.getArtifactId(), versionRange, d
237                             .getType(), d.getClassifier(), d.getScope());
238                     map.put(d.getManagementKey(), artifact);
239                 } catch (InvalidVersionSpecificationException e) {
240                     throw new ProjectBuildingException(projectId,
241                             "Unable to parse version '" + d.getVersion()
242                                     + "' for dependency '"
243                                     + d.getManagementKey() + "': "
244                                     + e.getMessage(), e);
245                 }
246             }
247         } else {
248             map = Collections.EMPTY_MAP;
249         }
250         return map;
251     }
252
253 }
254
Popular Tags