diff --git a/maven-plugins/maven-source-plugin/pom.xml b/maven-plugins/maven-source-plugin/pom.xml new file mode 100644 index 0000000000..6230737fab --- /dev/null +++ b/maven-plugins/maven-source-plugin/pom.xml @@ -0,0 +1,25 @@ + + 4.0.0 + + maven-plugin-parent + org.apache.maven.plugins + 2.0-SNAPSHOT + + maven-source-plugin + 2.0-alpha-1-SNAPSHOT + maven-plugin + Maven Source Plug-In + + + plexus + plexus-archiver + 1.0-alpha-1 + + + plexus + plexus-container-default + 1.0-alpha-2 + test + + + diff --git a/maven-plugins/maven-source-plugin/src/main/java/org/apache/maven/plugin/source/JarSourceMojo.java b/maven-plugins/maven-source-plugin/src/main/java/org/apache/maven/plugin/source/JarSourceMojo.java new file mode 100644 index 0000000000..dcc874b4c0 --- /dev/null +++ b/maven-plugins/maven-source-plugin/src/main/java/org/apache/maven/plugin/source/JarSourceMojo.java @@ -0,0 +1,81 @@ +package org.apache.maven.plugin.source; + +/* + * Copyright 2001-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.codehaus.plexus.archiver.jar.JarArchiver; + +import java.io.File; +import java.util.Iterator; +import java.util.List; + +/** + * This plugin bundles all the generated sources into a jar archive. + * + * @author Trygve Laugstøl + * @version $Id$ + * @goal jar + */ +public class JarSourceMojo + extends AbstractMojo +{ + /** + * @parameter expression="${project.build.finalName}" + * @required + */ + private String finalName; + + /** + * @parameter expression="${project.compileSourceRoots}" + * @required + */ + private List compileSourceRoots; + + /** + * @parameter expression="${project.build.output}" + * @required + */ + private File outputDirectory; + + public void execute() + throws MojoExecutionException + { + // TODO: use a component lookup? + JarArchiver archiver = new JarArchiver(); + + SourceBundler sourceBundler = new SourceBundler(); + + File outputFile = new File( outputDirectory, finalName + "-sources.jar" ); + + File[] sourceDirectories = new File[compileSourceRoots.size()]; + int count = 0; + for ( Iterator i = compileSourceRoots.iterator(); i.hasNext(); count++ ) + { + sourceDirectories[count] = new File( (String) i.next() ); + } + + try + { + sourceBundler.makeSourceBundle( outputFile, sourceDirectories, archiver ); + } + catch ( Exception e ) + { + throw new MojoExecutionException( "Error building source JAR", e ); + } + } +} diff --git a/maven-plugins/maven-source-plugin/src/main/java/org/apache/maven/plugin/source/SourceBundler.java b/maven-plugins/maven-source-plugin/src/main/java/org/apache/maven/plugin/source/SourceBundler.java new file mode 100644 index 0000000000..dd9b1fd5e7 --- /dev/null +++ b/maven-plugins/maven-source-plugin/src/main/java/org/apache/maven/plugin/source/SourceBundler.java @@ -0,0 +1,38 @@ +package org.apache.maven.plugin.source; + +import java.io.File; + +import org.codehaus.plexus.archiver.Archiver; + +/** + * @author Trygve Laugstøl + * @version $Id$ + */ +public class SourceBundler +{ + private final static String[] DEFAULT_INCLUDES = new String[]{ + "**/*", + }; + + private final static String[] DEFAULT_EXCLUDES = new String[]{ + "**/CVS/**", + "**/.svn/**", + }; + + public void makeSourceBundle( File outputFile, File[] sourceDirectories, Archiver archiver ) + throws Exception + { + String[] includes = DEFAULT_INCLUDES; + + String[] excludes = DEFAULT_EXCLUDES; + + for ( int i = 0; i < sourceDirectories.length; i++ ) + { + archiver.addDirectory( sourceDirectories[ i ], includes, excludes ); + } + + archiver.setDestFile( outputFile ); + + archiver.createArchive(); + } +} diff --git a/maven-plugins/maven-source-plugin/src/main/resources/META-INF/plexus/components.xml b/maven-plugins/maven-source-plugin/src/main/resources/META-INF/plexus/components.xml new file mode 100644 index 0000000000..41798138f9 --- /dev/null +++ b/maven-plugins/maven-source-plugin/src/main/resources/META-INF/plexus/components.xml @@ -0,0 +1,4 @@ + + + + diff --git a/maven-plugins/maven-source-plugin/src/test/java/org/apache/maven/plugin/source/SourceBundlerTest.java b/maven-plugins/maven-source-plugin/src/test/java/org/apache/maven/plugin/source/SourceBundlerTest.java new file mode 100644 index 0000000000..8da5c26feb --- /dev/null +++ b/maven-plugins/maven-source-plugin/src/test/java/org/apache/maven/plugin/source/SourceBundlerTest.java @@ -0,0 +1,40 @@ +package org.apache.maven.plugin.source; + +import org.codehaus.plexus.PlexusTestCase; +import org.codehaus.plexus.archiver.Archiver; + +import java.io.File; + +/** + * @author Trygve Laugstøl + * @version $Id$ + */ +public class SourceBundlerTest + extends PlexusTestCase +{ + public void testNormalProject() + throws Exception + { + SourceBundler sourceBundler = new SourceBundler(); + + Archiver archiver = (Archiver) lookup( Archiver.ROLE, "jar" ); + + File outputFile = getTestFile( "target/source-bundler-test/normal.jar" ); + + File sourceDirectories[] = { + getTestFile( "src/test/projects/normal/src/main/java" ), + getTestFile( "src/test/projects/normal/src/main/resources" ), + getTestFile( "src/test/projects/normal/src/test/java" ), + getTestFile( "src/test/projects/normal/src/test/resources" ), + }; + + if ( outputFile.exists() ) + { + assertTrue( "Could not delete output file: " + outputFile.getAbsolutePath(), outputFile.delete() ); + } + + sourceBundler.makeSourceBundle( outputFile, sourceDirectories, archiver ); + + assertTrue( "Missing output file: " + outputFile.getAbsolutePath(), outputFile.isFile() ); + } +} diff --git a/maven-plugins/maven-source-plugin/src/test/projects/normal/pom.xml b/maven-plugins/maven-source-plugin/src/test/projects/normal/pom.xml new file mode 100644 index 0000000000..c9e2398618 --- /dev/null +++ b/maven-plugins/maven-source-plugin/src/test/projects/normal/pom.xml @@ -0,0 +1,7 @@ + + 4.0.0 + mojo-test + maven-sources-plugin-test-zip + jar + 1.0 + \ No newline at end of file diff --git a/maven-plugins/maven-source-plugin/src/test/projects/normal/src/main/java/App.java b/maven-plugins/maven-source-plugin/src/test/projects/normal/src/main/java/App.java new file mode 100644 index 0000000000..ad74427757 --- /dev/null +++ b/maven-plugins/maven-source-plugin/src/test/projects/normal/src/main/java/App.java @@ -0,0 +1,3 @@ +public class App +{ +} diff --git a/maven-plugins/maven-source-plugin/src/test/projects/normal/src/main/resources/sub-directory/file.resource b/maven-plugins/maven-source-plugin/src/test/projects/normal/src/main/resources/sub-directory/file.resource new file mode 100644 index 0000000000..e69de29bb2 diff --git a/maven-plugins/maven-source-plugin/src/test/projects/normal/src/test/java/Test.java b/maven-plugins/maven-source-plugin/src/test/projects/normal/src/test/java/Test.java new file mode 100644 index 0000000000..604e3a7ab8 --- /dev/null +++ b/maven-plugins/maven-source-plugin/src/test/projects/normal/src/test/java/Test.java @@ -0,0 +1,3 @@ +public class Test +{ +} diff --git a/maven-plugins/maven-source-plugin/src/test/projects/normal/src/test/resources/sub-directory2/file2.resource b/maven-plugins/maven-source-plugin/src/test/projects/normal/src/test/resources/sub-directory2/file2.resource new file mode 100644 index 0000000000..e69de29bb2