Author: sshafroi
Date: 2008-04-03 14:09:41 +0200 (Thu, 03 Apr 2008)
New Revision: 6337

Modified:
   
branches/2.16/mojo/src/main/java/no/sesat/mojo/SearchModesSchemaGenerator.java
   branches/2.16/mojo/src/main/java/no/sesat/mojo/modes/Builder.java
Log:
Make it possible to use specify 'sourceArtifact' for the 
SearchModeSchemaGenerator. This will now fetch the artifact from some 
repository and unpack it into the target/source directory. Then append this to 
the classpath.



Modified: 
branches/2.16/mojo/src/main/java/no/sesat/mojo/SearchModesSchemaGenerator.java
===================================================================
--- 
branches/2.16/mojo/src/main/java/no/sesat/mojo/SearchModesSchemaGenerator.java  
    2008-04-03 11:56:10 UTC (rev 6336)
+++ 
branches/2.16/mojo/src/main/java/no/sesat/mojo/SearchModesSchemaGenerator.java  
    2008-04-03 12:09:41 UTC (rev 6337)
@@ -2,11 +2,22 @@
 
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintStream;
+import java.util.Enumeration;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
 
 import no.sesat.mojo.modes.Builder;
 
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.factory.ArtifactFactory;
+import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
+import org.apache.maven.artifact.resolver.ArtifactResolutionException;
+import org.apache.maven.artifact.resolver.ArtifactResolver;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.project.MavenProject;
@@ -18,7 +29,7 @@
 
     /**
      * The Maven project.
-     *
+     * 
      * @parameter expression="${project}"
      * @required
      * @readonly
@@ -28,50 +39,137 @@
 
     /**
      * Classpath
-     *
+     * 
      * @parameter
      */
     private List<String> classpaths;
 
     /**
+     * sourceArtifacts
+     * 
+     * @parameter
+     */
+    private List<String> sourceArtifacts;
+
+    /**
      * Output directory
-     *
+     * 
      * @parameter
      */
     private String outputDir;
 
     /**
+     * Used to look up Artifacts in the remote serverDeployLocation.
+     * 
+     * @parameter 
expression="${component.org.apache.maven.artifact.factory.ArtifactFactory}"
+     * @required
+     * @readonly
+     */
+    private ArtifactFactory factory;
+
+    /**
+     * Used to look up Artifacts in the remote serverDeployLocation.
+     * 
+     * @parameter 
expression="${component.org.apache.maven.artifact.resolver.ArtifactResolver}"
+     * @required
+     * @readonly
+     */
+    private ArtifactResolver resolver;
+
+    /**
+     * Location of the local serverDeployLocation.
+     * 
+     * @parameter expression="${localRepository}"
+     * @readonly
+     * @required
+     */
+    private org.apache.maven.artifact.repository.ArtifactRepository local;
+
+    /**
+     * List of Remote Repositories used by the resolver
+     * 
+     * @parameter expression="${project.remoteArtifactRepositories}"
+     * @readonly
+     * @required
+     */
+    private java.util.List remoteRepos;
+
+    /**
      * @see org.apache.maven.plugin.Mojo#execute()
      */
     public void execute() throws MojoExecutionException {
         getLog().info(this.getClass().getName());
 
-        if (classpaths == null) {
-            getLog().error("classpaths variable must be specified");
-        }
-
         if (outputDir == null) {
             getLog().error("outputDir variable must be specified");
         }
 
         String classpath = "";
-        for (final Iterator<String> iterator = classpaths.iterator(); 
iterator.hasNext();) {
-            final String name = iterator.next();
-            File file = new File(name);
-            if (!file.isAbsolute()) {
-                file = new File(project.getBasedir(), name);
+        if (classpaths != null) {
+            for (final Iterator<String> iterator = classpaths.iterator(); 
iterator.hasNext();) {
+                final String name = iterator.next();
+                File file = new File(name);
+                if (!file.isAbsolute()) {
+                    file = new File(project.getBasedir(), name);
+                }
+                if (file.exists()) {
+                    try {
+                        classpath += file.getCanonicalPath() + File.separator;
+                    } catch (IOException e) {
+                        getLog().warn(e);
+                    }
+                    if (iterator.hasNext()) {
+                        classpath += File.pathSeparator;
+                    }
+                } else {
+                    getLog().warn("Classpath not found : " + 
file.getAbsolutePath());
+                }
             }
-            if (file.exists()) {
+        }
+
+        if (sourceArtifacts != null) {
+            Map<String, Artifact> artifactMap = project.getArtifactMap();
+            for (String artifactName : sourceArtifacts) {
+                String[] ap = artifactName.split(":");
+
+                Artifact a = factory.createArtifactWithClassifier(ap[0], 
ap[1], ap[2], "jar", "sources");
+
                 try {
-                    classpath += file.getCanonicalPath() + File.separator;
-                } catch (IOException e) {
-                    getLog().warn(e);
+                    resolver.resolve(a, remoteRepos, local);
+                } catch (ArtifactResolutionException e) {
+                    e.printStackTrace();
+                } catch (ArtifactNotFoundException e) {
+                    e.printStackTrace();
                 }
-                if (iterator.hasNext()) {
+
+                File outFolder = new File("target/source/");
+                outFolder.mkdirs();
+                if (!classpath.isEmpty()) {
                     classpath += File.pathSeparator;
                 }
-            } else {
-                getLog().warn("Classpath not found : " + 
file.getAbsolutePath());
+                classpath += outFolder.getAbsolutePath();
+
+                try {
+                    JarFile jarFile = new JarFile(a.getFile());
+
+                    for (Enumeration<JarEntry> e = jarFile.entries(); 
e.hasMoreElements();) {
+                        JarEntry entry = (JarEntry) e.nextElement();
+                        File file = new File(outFolder, entry.getName());
+                        if (entry.isDirectory()) {
+                            file.mkdir();
+                        } else {
+                            InputStream in = jarFile.getInputStream(entry);
+                            PrintStream out = new PrintStream(file);
+                            byte[] buf = new byte[1024];
+                            int len;
+                            while ((len = in.read(buf)) > 0) {
+                                out.write(buf, 0, len);
+                            }
+                        }
+                    }
+                } catch (IOException e1) {
+                    e1.printStackTrace();
+                }
             }
         }
 

Modified: branches/2.16/mojo/src/main/java/no/sesat/mojo/modes/Builder.java
===================================================================
--- branches/2.16/mojo/src/main/java/no/sesat/mojo/modes/Builder.java   
2008-04-03 11:56:10 UTC (rev 6336)
+++ branches/2.16/mojo/src/main/java/no/sesat/mojo/modes/Builder.java   
2008-04-03 12:09:41 UTC (rev 6337)
@@ -44,7 +44,8 @@
     public static void build(final String classpath, final String dir, final 
String idString) {
         outputDir = new File(dir).getAbsolutePath() + File.separator;
         id = idString;
-
+        RootDocImpl root = null;
+        
         // hack to suppress javadoc's warnings.
         final PrintStream out = System.out;
         final PrintStream err = System.err;
@@ -64,7 +65,7 @@
 
             final Options compOpts = Options.instance(context);
             compOpts.put("-classpath", classpath);
-            RootDocImpl root = null;
+            
             try {
                 root = comp.getRootDocImpl("", "", new ModifierFilter(PUBLIC | 
PROTECTED), javaNames.toList(), options
                         .toList(), false, subPackages.toList(), 
xcludePackages.toList(), false, false, false);
@@ -72,9 +73,6 @@
                 e.printStackTrace(err);
                 return;
             }
-            if (root != null) {
-                start(root);
-            }
         } catch (Throwable e) {
             // e.printStackTrace(out);
             out.print("Generating schema files failed due to error: " + 
e.getMessage());
@@ -82,6 +80,9 @@
             System.setOut(out);
             System.setErr(err);
         }
+        if (root != null) {
+            start(root);
+        }
     }
 
     /**

_______________________________________________
Kernel-commits mailing list
[email protected]
http://sesat.no/mailman/listinfo/kernel-commits

Reply via email to