Author: aramirez
Date: Mon Mar 27 00:19:25 2006
New Revision: 389062

URL: http://svn.apache.org/viewcvs?rev=389062&view=rev
Log:
PR:MSOURCES-2
Submitted By: Maria Odea Ching

new parameter "includeTestSources" was added. It indicates whether a jar file 
that contains the test sources will be created or not. The jar file is named 
${finalName}-test-sources.jar.

Modified:
    
maven/plugins/trunk/maven-source-plugin/src/main/java/org/apache/maven/plugin/source/JarSourceMojo.java

Modified: 
maven/plugins/trunk/maven-source-plugin/src/main/java/org/apache/maven/plugin/source/JarSourceMojo.java
URL: 
http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-source-plugin/src/main/java/org/apache/maven/plugin/source/JarSourceMojo.java?rev=389062&r1=389061&r2=389062&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-source-plugin/src/main/java/org/apache/maven/plugin/source/JarSourceMojo.java
 (original)
+++ 
maven/plugins/trunk/maven-source-plugin/src/main/java/org/apache/maven/plugin/source/JarSourceMojo.java
 Mon Mar 27 00:19:25 2006
@@ -21,6 +21,7 @@
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.project.MavenProject;
 import org.apache.maven.project.MavenProjectHelper;
+import org.codehaus.plexus.archiver.Archiver;
 import org.codehaus.plexus.archiver.jar.JarArchiver;
 
 import java.io.File;
@@ -96,6 +97,15 @@
      */
     private File outputDirectory;
 
+    /**
+     * @parameter expression="${includeTestSources}" default="false"
+     */
+    private boolean includeTestSources;
+
+    private Archiver archiver;
+
+    private static SourceBundler sourceBundler;
+
     public void execute()
         throws MojoExecutionException
     {
@@ -105,42 +115,41 @@
 
             return;
         }
-
         // TODO: use a component lookup?
-        JarArchiver archiver = new JarArchiver();
-
-        SourceBundler sourceBundler = new SourceBundler();
+        archiver = new JarArchiver();
+        sourceBundler = new SourceBundler();
 
         File outputFile = new File( outputDirectory, finalName + 
"-sources.jar" );
-
         List compileSourceRoots = executedProject.getCompileSourceRoots();
         List resources = executedProject.getResources();
-
         File[] sourceDirectories = new File[compileSourceRoots.size() + 
resources.size()];
-        int count = 0;
-        for ( Iterator i = compileSourceRoots.iterator(); i.hasNext(); count++ 
)
-        {
-            sourceDirectories[count] = new File( (String) i.next() );
-        }
-        for ( Iterator i = resources.iterator(); i.hasNext(); count++ )
-        {
-            Resource resource = (Resource) i.next();
-            sourceDirectories[count] = new File( resource.getDirectory() );
-        }
+
+        sourceDirectories = addDirectories( compileSourceRoots, resources, 
sourceDirectories );
 
         try
         {
-            sourceBundler.makeSourceBundle( outputFile, sourceDirectories, 
archiver );
+            createJar( outputFile, sourceDirectories );
         }
         catch ( Exception e )
         {
             throw new MojoExecutionException( "Error building source JAR", e );
         }
 
+        if ( includeTestSources )
+        {
+            try
+            {
+                createTestSourcesJar();
+            }
+            catch ( Exception e )
+            {
+                throw new MojoExecutionException( "Error building source JAR", 
e );
+            }
+        }
+
         if ( !attach )
         {
             getLog().info( "NOT adding java-sources to attached artifacts 
list." );
-
         }
         else
         {
@@ -149,4 +158,62 @@
             projectHelper.attachArtifact( project, "java-source", "sources", 
outputFile );
         }
     }
+
+    /**
+     * Add the compile source directories and resource directories that will 
be included in the jar file
+     *
+     * @param compileSourceRoots
+     * @param resources
+     * @param sourceDirectories
+     * @return an array of File objects that contains the directories that 
will be included in the jar file
+     */
+    private File[] addDirectories( List compileSourceRoots, List resources, 
File[] sourceDirectories )
+    {
+        int count = 0;
+        for ( Iterator i = compileSourceRoots.iterator(); i.hasNext(); count++ 
)
+        {
+            sourceDirectories[count] = new File( (String) i.next() );
+        }
+
+        for ( Iterator i = resources.iterator(); i.hasNext(); count++ )
+        {
+            Resource resource = (Resource) i.next();
+            sourceDirectories[count] = new File( resource.getDirectory() );
+        }
+
+        return sourceDirectories;
+    }
+
+    /**
+     * Create jar file that will contain the test sources
+     *
+     * @throws Exception
+     */
+    private void createTestSourcesJar()
+        throws Exception
+    {
+        File outputFile = new File( outputDirectory, finalName + 
"-test-sources.jar" );
+        List testCompileSourceRoots = 
executedProject.getTestCompileSourceRoots();
+        List testResources = executedProject.getTestResources();
+
+        File[] testSourceDirectories = new File[testCompileSourceRoots.size() 
+ testResources.size()];
+        testSourceDirectories = addDirectories( testCompileSourceRoots, 
testResources, testSourceDirectories );
+        archiver = new JarArchiver();
+
+        createJar( outputFile, testSourceDirectories );
+    }
+
+    /**
+     * Create jar file that contains the specified source directories
+     *
+     * @param outputFile        the file name of the jar
+     * @param sourceDirectories the source directories that will be included 
in the jar file
+     * @throws Exception
+     */
+    private void createJar( File outputFile, File[] sourceDirectories )
+        throws Exception
+    {
+        sourceBundler.makeSourceBundle( outputFile, sourceDirectories, 
archiver );
+    }
+
 }


Reply via email to