KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > maven > MavenUtil


1 package hudson.maven;
2
3 import hudson.model.TaskListener;
4 import org.apache.maven.embedder.MavenEmbedderException;
5 import org.apache.maven.project.MavenProject;
6 import org.apache.maven.project.ProjectBuildingException;
7
8 import java.io.File JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.util.ArrayList JavaDoc;
11 import java.util.List JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.util.Enumeration JavaDoc;
14 import java.net.URL JavaDoc;
15
16 /**
17  * @author Kohsuke Kawaguchi
18  */

19 class MavenUtil {
20     /**
21      * Creates a fresh {@link MavenEmbedder} instance.
22      *
23      * @param listener
24      * This is where the log messages from Maven will be recorded.
25      */

26     public static MavenEmbedder createEmbedder(TaskListener listener) throws MavenEmbedderException {
27         MavenEmbedder maven = new MavenEmbedder();
28
29         ClassLoader JavaDoc cl = MavenUtil.class.getClassLoader();
30         maven.setClassLoader(new MaskingClassLoader(cl));
31         maven.setLogger( new EmbedderLoggerImpl(listener) );
32
33         maven.start();
34
35         return maven;
36     }
37
38     /**
39      * Recursively resolves module POMs that are referenced from
40      * the given {@link MavenProject} and parses them into
41      * {@link MavenProject}s.
42      *
43      * @param rel
44      * Used to compute the relative path. Pass in "" to begin.
45      * @param relativePathInfo
46      * Upon the completion of this method, this variable stores the relative path
47      * from the root directory of the given {@link MavenProject} to the root directory
48      * of each of the newly parsed {@link MavenProject}.
49      */

50     public static void resolveModules(MavenEmbedder embedder, MavenProject project, String JavaDoc rel, Map JavaDoc<MavenProject,String JavaDoc> relativePathInfo) throws ProjectBuildingException {
51
52         File JavaDoc basedir = project.getFile().getParentFile();
53         relativePathInfo.put(project,rel);
54
55         List JavaDoc<MavenProject> modules = new ArrayList JavaDoc<MavenProject>();
56
57         for (String JavaDoc modulePath : (List JavaDoc<String JavaDoc>) project.getModules()) {
58             File JavaDoc moduleFile = new File JavaDoc(new File JavaDoc(basedir, modulePath),"pom.xml");
59
60             String JavaDoc relativePath = rel;
61             if(relativePath.length()>0) relativePath+='/';
62             relativePath+=modulePath;
63
64             MavenProject child = embedder.readProject(moduleFile);
65             resolveModules(embedder,child,relativePath,relativePathInfo);
66             modules.add(child);
67         }
68
69         project.setCollectedProjects(modules);
70     }
71
72     /**
73      * When we run in Jetty during development, embedded Maven will end up
74      * seeing some of the Maven class visibel through Jetty, and this confuses it.
75      *
76      * <p>
77      * Specifically, embedded Maven will find all the component descriptors
78      * visible through Jetty, yet when it comes to loading classes, classworlds
79      * still load classes from local realms created inside embedder.
80      *
81      * <p>
82      * This classloader prevents this issue by hiding the component descriptor
83      * visible through Jetty.
84      */

85     private static final class MaskingClassLoader extends ClassLoader JavaDoc {
86
87         public MaskingClassLoader(ClassLoader JavaDoc parent) {
88             super(parent);
89         }
90
91         public Enumeration JavaDoc<URL JavaDoc> getResources(String JavaDoc name) throws IOException JavaDoc {
92             final Enumeration JavaDoc<URL JavaDoc> e = super.getResources(name);
93             return new Enumeration JavaDoc<URL JavaDoc>() {
94                 URL JavaDoc next;
95
96                 public boolean hasMoreElements() {
97                     fetch();
98                     return next!=null;
99                 }
100
101                 public URL JavaDoc nextElement() {
102                     fetch();
103                     URL JavaDoc r = next;
104                     next = null;
105                     return r;
106                 }
107
108                 private void fetch() {
109                     while(next==null && e.hasMoreElements()) {
110                         next = e.nextElement();
111                         if(next.toExternalForm().contains("maven-plugin-tools-api"))
112                             next = null;
113                     }
114                 }
115             };
116         }
117     }
118 }
119
Popular Tags