On 10/8/07, Yan Huang <[EMAIL PROTECTED]> wrote:
> Hello,
>
> I have these requirements to package a module:
>
> - it's group ID is different from what I have at the top level
> - no compilation or test is required
> - it just needs to be packaged in "zip" format
> - it needs to be deployed to a maven2 repository as part of deploy
> phase
>
> Do you think I need to separate this module out from the overall package
> because of different group ID? If I need to build it separately, what's
> going to be the "packaging" type? Maven does not support "zip" packaging
> type though, and I don't want an empty jar being created in order to attach
> "zip" file in the "package" phase by using assembly plugin.
>
> Any thoughts or suggestions?
>
> Thanks
> Yan
>
Creating a plugin that provides a ZIP packaging is not that hard:
Use this mojo:
================
import java.io.File;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;
import org.codehaus.plexus.archiver.zip.ZipArchiver;
/**
* Creates a zip from the output directory, and sets it as the project artifact.
*
* @goal zip
* @phase package
* @requiresProject true
*/
public class ZipMojo extends AbstractMojo {
public static final String[] DEFAULT_EXCLUDES = {
// Miscellaneous typical temporary files
"**/*~",
"**/#*#",
"**/.#*",
"**/%*%",
"**/._*",
// CVS
"**/CVS",
"**/CVS/**",
"**/.cvsignore",
// SCCS
"**/SCCS",
"**/SCCS/**",
// Visual SourceSafe
"**/vssver.scc",
// Subversion
"**/.svn",
"**/.svn/**",
// Arch/Bazaar
"**/.arch-ids", "**/.arch-ids/**",
// Mac
"**/.DS_Store"
};
private static final String[] DEFAULT_INCLUDES = new String[]{"**/**"};
/**
* Directory containing the generated ZIP.
*
* @parameter expression="${project.build.directory}"
* @required
*/
private File outputDirectory;
/**
* Name of the generated ZIP.
*
* @parameter alias="zipName" expression="${project.build.finalName}"
* @required
*/
private String finalName;
/**
* The Jar archiver.
*
* @parameter
expression="${component.org.codehaus.plexus.archiver.Archiver#zip}"
* @required
*/
private ZipArchiver zipArchiver;
/**
* The maven project.
*
* @parameter expression="${project}"
* @required
* @readonly
*/
private MavenProject project;
/**
* @component
*/
private MavenProjectHelper projectHelper;
/**
* Whether creating the archive should be forced.
* @parameter expression="${jar.forceCreation}" default-value="false"
*/
private boolean forceCreation;
/**
* Directory containing the classes.
*
* @parameter expression="${project.build.outputDirectory}"
* @required
*/
private File classesDirectory;
/**
* Classifier to add to the artifact generated. If given, the
artifact will be an attachment instead.
*
* @parameter
*/
private String classifier;
protected String getClassifier()
{
return classifier;
}
/**
* Return the main classes directory, so it's used as the root of the jar.
*/
protected File getClassesDirectory()
{
return classesDirectory;
}
protected final MavenProject getProject()
{
return project;
}
protected static File getZipFile( File basedir, String finalName,
String classifier )
{
if ( classifier == null )
{
classifier = "";
}
else if ( classifier.trim().length() > 0 &&
!classifier.startsWith( "-" ) )
{
classifier = "-" + classifier;
}
return new File( basedir, finalName + classifier + ".zip" );
}
/**
* Generates the ZIP.
*
* @todo Add license files in META-INF directory.
*/
public File createArchive()
throws MojoExecutionException
{
File zipFile = getZipFile( outputDirectory, finalName,
getClassifier() );
try
{
File contentDirectory = getClassesDirectory();
if ( !contentDirectory.exists() )
{
getLog().warn( "ZIP will be empty - no content was
marked for inclusion!" );
}
else
{
zipArchiver.addDirectory( contentDirectory,
DEFAULT_INCLUDES, DEFAULT_EXCLUDES );
}
zipArchiver.setForced(forceCreation);
zipArchiver.setDestFile(zipFile);
zipArchiver.createArchive();
return zipFile;
}
catch ( Exception e )
{
throw new MojoExecutionException( "Error assembling ZIP", e );
}
}
/**
* Generates the ZIP.
*
* @todo Add license files in META-INF directory.
*/
public void execute()
throws MojoExecutionException
{
File zipFile = createArchive();
String classifier = getClassifier();
if ( classifier != null )
{
projectHelper.attachArtifact( getProject(), "zip",
classifier, zipFile );
}
else
{
getProject().getArtifact().setFile( zipFile );
}
}
}
==================
and put this in META-INF\plexus\components.xml:
<component-set>
<components>
<component>
<role>org.apache.maven.lifecycle.mapping.LifecycleMapping</role>
<role-hint>zip</role-hint>
<implementation>
org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping
</implementation>
<configuration>
<phases>
<process-resources>
org.apache.maven.plugins:maven-resources-plugin:resources
</process-resources>
<package>groupId:maven-zip-plugin:zip</package>
<install>org.apache.maven.plugins:maven-install-plugin:install</install>
<deploy>org.apache.maven.plugins:maven-deploy-plugin:deploy</deploy>
</phases>
</configuration>
</component>
<component>
<role>org.apache.maven.artifact.handler.ArtifactHandler</role>
<role-hint>zip</role-hint>
<implementation>
org.apache.maven.artifact.handler.DefaultArtifactHandler
</implementation>
<configuration>
<extension>zip</extension>
<type>zip</type>
<packaging>zip</packaging>
<addedToClasspath>false</addedToClasspath>
<includesDependencies>true</includesDependencies>
</configuration>
</component>
</components>
</component-set>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]