standard assemly - bin

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@163721 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Leslie Porter 2005-03-30 15:41:23 +00:00
parent 6f8b171aaf
commit 0f861327fd
3 changed files with 153 additions and 74 deletions

View File

@ -5,4 +5,5 @@
#
m2 plugin:descriptor
m2 modello:xpp3-reader modello:xpp3-writer modello:java resources:resources compiler:compile resources:testResources compiler:testCompile surefire:test jar:jar install:install
m2 modello:xpp3-reader modello:xpp3-writer modello:java resources:resources compiler:compile resources:testResources compiler:testCompile surefire:test jar:jar
cp target/maven-assemble-plugin-1.0-SNAPSHOT.jar $HOME/repository-m2/org.apache.maven.plugins/maven-plugins

View File

@ -25,9 +25,13 @@
import org.codehaus.plexus.archiver.jar.JarArchiver;
import org.codehaus.plexus.archiver.tar.TarArchiver;
import org.codehaus.plexus.archiver.zip.ZipArchiver;
import org.codehaus.plexus.util.IOUtil;
import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Iterator;
/**
@ -35,19 +39,28 @@
* @version $Id$
* @goal assemble
* @description assemble an application bundle or distribution
* @parameter name="basedir" type="String" required="true" validator="" expression="#basedir" description=""
* @parameter name="outputDirectory" type="java.io.File" required="true" validator="" expression="#project.build.directory" description=""
* @parameter name="descriptor" type="java.io.File" required="true" validator="" expression="#maven.assemble.descriptor" description=""
* @parameter name="descriptor" type="java.io.File" required="false" validator="" expression="#maven.assemble.descriptor" description=""
* @parameter name="finalName" type="String" required="true" validator="" expression="#project.build.finalName" description=""
* @parameter name="descriptorId" type="String" required="false" validator="" expression="#maven.assemble.descriptorId" description=""
*/
public class AssembleMojo
extends AbstractPlugin
{
private static final String[] EMPTY_STRING_ARRAY = {};
private File outputDirectory;
private String basedir;
/**
* @todo use java.io.File
*/
private String outputDirectory;
private File descriptor;
private String descriptorId;
private String finalName;
public void execute()
@ -67,92 +80,131 @@ public void execute()
private void doExecute()
throws Exception
{
AssemblyXpp3Reader reader = new AssemblyXpp3Reader();
Assembly assembly = reader.read( new FileReader( descriptor ) );
Reader r = null;
// TODO: include dependencies marked for distribution under certain formats
// TODO: have a default set of descriptors that can be used instead of the file
// TODO: how, might we plugin this into an installer, such as NSIS?
// TODO: allow file mode specifications?
String fullName = finalName + "-" + assembly.getId();
for ( Iterator i = assembly.getFormats().iterator(); i.hasNext(); )
if ( descriptor != null )
{
String format = (String) i.next();
String filename = fullName + "." + format;
// TODO: use component roles? Can we do that in a mojo?
Archiver archiver;
if ( format.startsWith( "tar" ) )
r = new FileReader( descriptor );
}
else if ( descriptorId != null )
{
InputStream resourceAsStream = getClass().getResourceAsStream( "/assemblies/" + descriptorId + ".xml" );
if ( resourceAsStream == null )
{
TarArchiver tarArchiver = new TarArchiver();
archiver = tarArchiver;
int index = format.indexOf( '.' );
if ( index >= 0 )
// TODO: better exception
throw new Exception( "Descriptor with ID '" + descriptorId + "' not found" );
}
r = new InputStreamReader( resourceAsStream );
}
else
{
// TODO: better exception
throw new Exception( "You must specify descriptor or descriptorId" );
}
try
{
AssemblyXpp3Reader reader = new AssemblyXpp3Reader();
Assembly assembly = reader.read( r );
// TODO: include dependencies marked for distribution under certain formats
// TODO: how, might we plugin this into an installer, such as NSIS?
// TODO: allow file mode specifications?
String fullName = finalName + "-" + assembly.getId();
for ( Iterator i = assembly.getFormats().iterator(); i.hasNext(); )
{
String format = (String) i.next();
String filename = fullName + "." + format;
// TODO: use component roles? Can we do that in a mojo?
Archiver archiver;
if ( format.startsWith( "tar" ) )
{
// TODO: this needs a cleanup in plexus archiver - use a real typesafe enum
TarArchiver.TarCompressionMethod tarCompressionMethod = new TarArchiver.TarCompressionMethod();
// TODO: this should accept gz and bz2 as well so we can skip over the switch
String compression = format.substring( index + 1 );
if ( compression.equals( "gz" ) )
TarArchiver tarArchiver = new TarArchiver();
archiver = tarArchiver;
int index = format.indexOf( '.' );
if ( index >= 0 )
{
tarCompressionMethod.setValue( "gzip" );
// TODO: this needs a cleanup in plexus archiver - use a real typesafe enum
TarArchiver.TarCompressionMethod tarCompressionMethod = new TarArchiver.TarCompressionMethod();
// TODO: this should accept gz and bz2 as well so we can skip over the switch
String compression = format.substring( index + 1 );
if ( compression.equals( "gz" ) )
{
tarCompressionMethod.setValue( "gzip" );
}
else if ( compression.equals( "bz2" ) )
{
tarCompressionMethod.setValue( "bzip2" );
}
else
{
// TODO: better handling
throw new IllegalArgumentException( "Unknown compression format: " + compression );
}
tarArchiver.setCompression( tarCompressionMethod );
}
else if ( compression.equals( "bz2" ) )
}
else if ( format.startsWith( "zip" ) )
{
archiver = new ZipArchiver();
}
else if ( format.startsWith( "jar" ) )
{
// TODO: use MavenArchiver for manifest?
archiver = new JarArchiver();
}
else
{
// TODO: better handling
throw new IllegalArgumentException( "Unknown format: " + format );
}
for ( Iterator j = assembly.getFilesets().iterator(); j.hasNext(); )
{
FileSet fileset = (FileSet) j.next();
String directory = fileset.getDirectory();
String output = fileset.getOutputDirectory();
if ( directory == null )
{
tarCompressionMethod.setValue( "bzip2" );
directory = basedir;
if ( output == null )
{
output = "/";
}
}
else
{
// TODO: better handling
throw new IllegalArgumentException( "Unknown compression format: " + compression );
if ( output == null )
{
output = directory;
}
}
if ( !output.endsWith( "/" ) && !output.endsWith( "\\" ) )
{
// TODO: shouldn't archiver do this?
output += '/';
}
tarArchiver.setCompression( tarCompressionMethod );
}
}
else if ( format.startsWith( "zip" ) )
{
archiver = new ZipArchiver();
}
else if ( format.startsWith( "jar" ) )
{
// TODO: use MavenArchiver for manifest?
archiver = new JarArchiver();
}
else
{
// TODO: better handling
throw new IllegalArgumentException( "Unknown format: " + format );
}
for ( Iterator j = assembly.getFilesets().iterator(); j.hasNext(); )
{
FileSet fileset = (FileSet) j.next();
String directory = fileset.getDirectory();
String output = fileset.getOutputDirectory();
if ( output == null )
{
output = directory;
}
if ( !output.endsWith( "/" ) && !output.endsWith( "\\" ) )
{
// TODO: shouldn't archiver do this?
output += '/';
String[] includes = (String[]) fileset.getIncludes().toArray( EMPTY_STRING_ARRAY );
if ( includes.length == 0 )
{
includes = null;
}
String[] excludes = (String[]) fileset.getExcludes().toArray( EMPTY_STRING_ARRAY );
archiver.addDirectory( new File( directory ), output, includes, excludes );
}
String[] includes = (String[]) fileset.getIncludes().toArray( EMPTY_STRING_ARRAY );
if ( includes.length == 0 )
{
includes = null;
}
String[] excludes = (String[]) fileset.getExcludes().toArray( EMPTY_STRING_ARRAY );
archiver.addDirectory( new File( directory ), output, includes, excludes );
archiver.setDestFile( new File( outputDirectory, filename ) );
archiver.createArchive();
}
archiver.setDestFile( new File( outputDirectory, filename ) );
archiver.createArchive();
}
finally
{
IOUtil.close( r );
}
}
}

View File

@ -0,0 +1,26 @@
<assembly>
<id>bin</id>
<formats>
<format>tar.gz</format>
<format>tar.bz2</format>
<format>zip</format>
</formats>
<filesets>
<fileset>
<includes>
<include>README*</include>
<include>LICENSE*</include>
<include>NOTICE*</include>
</includes>
</fileset>
<!-- TODO: docs? -->
<fileset>
<!-- TODO: use expresssions instead: ${project.build.directory}, ${project.build.finalName} -->
<directory>target</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileset>
</filesets>
</assembly>