- Externalize archiver methods

- Update JarMojo
- Implements WarMojo (Not tested yet)


git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@163458 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Emmanuel Venisse 2005-03-05 14:52:12 +00:00
parent dfbae27c44
commit 323eecfc57
9 changed files with 524 additions and 52 deletions

12
maven-archiver/.cvsignore Normal file
View File

@ -0,0 +1,12 @@
*~
*.log
target
dist
*.ipr
*.iws
*.iml
dist
target
.classpath
.project
build.xml

48
maven-archiver/pom.xml Normal file
View File

@ -0,0 +1,48 @@
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>maven</groupId>
<artifactId>maven-component</artifactId>
<version>2.0-SNAPSHOT</version>
</parent>
<groupId>maven</groupId>
<artifactId>maven-archiver</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>maven</groupId>
<artifactId>maven-monitor</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>plexus</groupId>
<artifactId>plexus-container-default</artifactId>
<version>1.0-alpha-2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>plexus</groupId>
<artifactId>plexus-archiver</artifactId>
<version>1.0-alpha-1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>maven</groupId>
<artifactId>maven-core</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>maven</groupId>
<artifactId>maven-model</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>maven</groupId>
<artifactId>maven-plugin</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,222 @@
package org.apache.maven.archiver;
/* ====================================================================
* Copyright 2001-2005 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.artifact.Artifact;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.PluginExecutionRequest;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.archiver.jar.JarArchiver;
import org.codehaus.plexus.archiver.jar.Manifest;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Set;
/**
* @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
* @version $Revision$ $Date$
*/
public class MavenArchiver
{
JarArchiver archiver = new JarArchiver();
File archiveFile;
/**
* Return a pre-configured manifest
* @todo Add user attributes list and user groups list
*/
public Manifest getManifest( PluginExecutionRequest request )
throws Exception
{
MavenProject project = (MavenProject)request.getParameter("project");
String mainClass = (String) request.getParameter( "mainClass" );
boolean addClasspath = new Boolean( (String) request.getParameter( "addClasspath" ) ).booleanValue();
boolean addExtensions = new Boolean( (String) request.getParameter( "addExtensions" ) ).booleanValue();
// Added basic entries
Manifest m = new Manifest();
Manifest.Attribute buildAttr = new Manifest.Attribute( "Built-By", System.getProperty( "user.name" ) );
m.addConfiguredAttribute( buildAttr );
Manifest.Attribute createdAttr = new Manifest.Attribute( "Created-By", "Apache Maven" );
m.addConfiguredAttribute( createdAttr );
Manifest.Attribute packageAttr = new Manifest.Attribute( "Package", project.getPackage() );
m.addConfiguredAttribute( packageAttr );
Manifest.Attribute buildJdkAttr = new Manifest.Attribute( "Build-Jdk", System.getProperty( "java.version" ) );
m.addConfiguredAttribute( buildJdkAttr );
if ( addClasspath )
{
StringBuffer classpath = new StringBuffer();
List dependencies = project.getDependencies();
for ( Iterator iter = dependencies.iterator(); iter.hasNext(); )
{
Dependency dependency = (Dependency) iter.next();
Properties properties = dependency.getProperties();
if ( Boolean.valueOf(properties.getProperty("manifest.classpath")).booleanValue())
{
if (classpath.length() > 0 )
{
classpath.append( " " );
}
// TODO replace dependency by artifact
classpath.append( dependency.getArtifactId() + "-" + dependency.getVersion() + ".jar");
}
}
Manifest.Attribute classpathAttr = new Manifest.Attribute( "Class-Path", classpath.toString() );
m.addConfiguredAttribute( classpathAttr );
}
// Added supplementary entries
Manifest.Attribute extensionNameAttr = new Manifest.Attribute( "Extension-Name", project.getArtifactId() );
m.addConfiguredAttribute( extensionNameAttr );
if ( project.getShortDescription() != null )
{
Manifest.Attribute specificationTitleAttr = new Manifest.Attribute( "Specification-Title", project.getShortDescription() );
m.addConfiguredAttribute( specificationTitleAttr );
}
if ( project.getOrganization() != null )
{
Manifest.Attribute specificationVendor = new Manifest.Attribute( "Specification-Vendor", project.getOrganization().getName() );
m.addConfiguredAttribute( specificationVendor );
Manifest.Attribute implementationVendorAttr = new Manifest.Attribute( "Implementation-Vendor", project.getOrganization().getName() );
m.addConfiguredAttribute( implementationVendorAttr );
}
Manifest.Attribute implementationTitleAttr = new Manifest.Attribute( "Implementation-Title", project.getArtifactId() );
m.addConfiguredAttribute( implementationTitleAttr );
Manifest.Attribute implementationVersionAttr = new Manifest.Attribute( "Implementation-Version", project.getVersion() );
m.addConfiguredAttribute( implementationVersionAttr );
if ( mainClass != null && ! "".equals( mainClass ) )
{
Manifest.Attribute mainClassAttr = new Manifest.Attribute( "Main-Class", mainClass );
m.addConfiguredAttribute( mainClassAttr );
}
// Added extensions
if ( addExtensions )
{
StringBuffer extensionsList = new StringBuffer();
Set artifacts = project.getArtifacts();
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
{
Artifact artifact = (Artifact) iter.next();
if ( "jar".equals( artifact.getType() ) )
{
if (extensionsList.length() > 0 )
{
extensionsList.append( " " );
}
extensionsList.append( artifact.getArtifactId() );
}
}
if (extensionsList.length() > 0 )
{
Manifest.Attribute extensionsListAttr = new Manifest.Attribute( "Extension-List", extensionsList.toString() );
m.addConfiguredAttribute( extensionsListAttr );
}
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
{
Artifact artifact = (Artifact) iter.next();
if ( "jar".equals( artifact.getType() ) )
{
Manifest.Attribute archExtNameAttr = new Manifest.Attribute( artifact.getArtifactId()
+ "-Extension-Name", artifact.getArtifactId() );
m.addConfiguredAttribute( archExtNameAttr );
Manifest.Attribute archImplVersionAttr = new Manifest.Attribute( artifact.getArtifactId()
+ "-Implementation-Version", artifact.getVersion() );
m.addConfiguredAttribute( archImplVersionAttr );
Manifest.Attribute archImplUrlAttr = new Manifest.Attribute( artifact.getArtifactId()
+ "-Implementation-URL", "http://www.ibiblio.org/maven/" + artifact.toString() );
m.addConfiguredAttribute( archImplUrlAttr );
}
}
}
return m;
}
public JarArchiver getArchiver()
{
return archiver;
}
public void setArchiver( JarArchiver archiver )
{
this.archiver = archiver;
}
public void setOutputFile( File outputFile )
{
archiveFile = outputFile;
}
public void createArchive( PluginExecutionRequest request )
throws Exception
{
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
MavenProject project = (MavenProject)request.getParameter("project");
String manifest = (String) request.getParameter( "manifest" );
boolean compress = new Boolean( (String) request.getParameter( "compress" ) ).booleanValue();
boolean index = new Boolean( (String) request.getParameter( "index" ) ).booleanValue();
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
archiver.addFile( project.getFile(), "META-INF/maven/pom.xml" );
if (manifest != null && ! "".equals( manifest ) )
{
File manifestFile = new File( manifest );
archiver.setManifest( manifestFile );
}
// Configure the jar
archiver.addConfiguredManifest( getManifest( request ) );
archiver.setCompress( compress );
archiver.setIndex( index );
archiver.setDestFile( archiveFile );
// create archive
archiver.createArchive();
}
}

View File

@ -40,7 +40,7 @@ public class MBoot
"modello/jars/modello-xdoc-plugin-1.0-SNAPSHOT.jar",
"modello/jars/modello-xml-plugin-1.0-SNAPSHOT.jar",
"modello/jars/modello-xpp3-plugin-1.0-SNAPSHOT.jar",
"plexus/jars/plexus-utils-1.0-alpha-1-SNAPSHOT.jar",
"plexus/jars/plexus-utils-1.0-alpha-2-SNAPSHOT.jar",
"surefire/jars/surefire-booter-1.2-SNAPSHOT.jar",
"surefire/jars/surefire-1.2-SNAPSHOT.jar",
"qdox/jars/qdox-1.2.jar" };
@ -83,6 +83,7 @@ public class MBoot
"maven-artifact",
"maven-script/maven-script-marmalade",
"maven-core",
"maven-archiver",
"maven-plugin-tools/maven-plugin-tools-api",
"maven-plugin-tools/maven-plugin-tools-java",
"maven-plugin-tools/maven-plugin-tools-pluggy",

View File

@ -24,6 +24,11 @@
<artifactId>plexus-archiver</artifactId>
<version>1.0-alpha-1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>maven</groupId>
<artifactId>maven-archiver</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>maven</groupId>
<artifactId>maven-artifact</artifactId>

View File

@ -16,10 +16,10 @@ package org.apache.maven.plugin.jar;
* limitations under the License.
*/
import org.apache.maven.archiver.MavenArchiver;
import org.apache.maven.plugin.PluginExecutionRequest;
import org.apache.maven.plugin.PluginExecutionResponse;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.archiver.jar.JarArchiver;
import java.io.File;
@ -120,45 +120,25 @@ public class JarMojo
//
// ----------------------------------------------------------------------
MavenProject project = (MavenProject)request.getParameter("project");
String manifest = (String) request.getParameter( "manifest" );
File basedir = new File( (String) request.getParameter( "basedir" ) );
String outputDirectory = (String) request.getParameter( "outputDirectory" );
String jarName = (String) request.getParameter( "jarName" );
boolean compress = new Boolean( (String) request.getParameter( "compress" ) ).booleanValue();
boolean index = new Boolean( (String) request.getParameter( "index" ) ).booleanValue();
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
File jarFile = new File( basedir, jarName + ".jar" );
JarArchiver archiver = new JarArchiver();
archiver.addDirectory( new File( outputDirectory ), new String[] { "**/**" }, new String[] { "**/package.html" } );
archiver.addFile( project.getFile(), "META-INF/maven/pom.xml" );
MavenArchiver archiver = new MavenArchiver();
if (manifest != null && ! "".equals( manifest ) )
{
File manifestFile = new File( manifest );
archiver.setManifest( manifestFile );
}
archiver.setOutputFile( jarFile );
// Configure the jar
archiver.addConfiguredManifest( getManifest( request ) );
archiver.setCompress( compress );
archiver.setIndex( index );
archiver.setDestFile( jarFile );
archiver.getArchiver().addDirectory( new File( outputDirectory ), new String[] { "**/**" }, new String[] { "**/package.html" } );
// create archive
archiver.createArchive();
archiver.createArchive( request );
}
}

View File

@ -19,12 +19,6 @@
<artifactId>maven-monitor</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>maven</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<type>plugin</type>
</dependency>
<dependency>
<groupId>plexus</groupId>
<artifactId>plexus-container-default</artifactId>
@ -35,6 +29,11 @@
<artifactId>plexus-archiver</artifactId>
<version>1.0-alpha-1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>maven</groupId>
<artifactId>maven-archiver</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>maven</groupId>
<artifactId>maven-artifact</artifactId>
@ -50,5 +49,10 @@
<artifactId>maven-model</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>maven</groupId>
<artifactId>wagon-api</artifactId>
<version>1.0-alpha-1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

View File

@ -16,19 +16,30 @@ package org.apache.maven.plugin.war;
* limitations under the License.
*/
import java.io.IOException;
import java.io.File;
import java.util.Iterator;
import java.util.Set;
import org.apache.maven.archiver.MavenArchiver;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.plugin.AbstractPlugin;
import org.apache.maven.plugin.PluginExecutionRequest;
import org.apache.maven.plugin.PluginExecutionResponse;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.archiver.war.WarArchiver;
import org.codehaus.plexus.util.FileUtils;
/**
* @goal war
* @phase package
*
* @description build a jar
* @description build a war/webapp
*
* @parameter
* name="jarName"
* name="warName"
* type="String"
* required="true"
* validator=""
@ -58,21 +69,6 @@ import org.apache.maven.project.MavenProject;
* expression="#maven.jar.manifest"
* description=""
* @parameter
* name="mainClass"
* type="String"
* required="false"
* validator=""
* expression="#maven.jar.mainClass"
* description=""
* @parameter
* name="addClasspath"
* type="String"
* required="false"
* validator=""
* expression="#maven.jar.addClasspath"
* default="false"
* description=""
* @parameter
* name="addExtensions"
* type="String"
* required="false"
@ -81,11 +77,58 @@ import org.apache.maven.project.MavenProject;
* default="false"
* description=""
* @parameter
* name="warSourceDirectory"
* type="String"
* required="true"
* validator=""
* expression="#maven.war.src"
* default="#basedir/src/webapp"
* description=""
* @parameter
* name="warSourceIncludes"
* type="String"
* required="false"
* validator=""
* expression="#maven.war.src.includes"
* default="**"
* description=""
* @parameter
* name="warSourceIncludes"
* type="String"
* required="false"
* validator=""
* expression="#maven.war.src.excludes"
* description=""
* @parameter
* name="webXml"
* type="String"
* required="false"
* validator=""
* expression="#maven.war.webxml"
* description=""
* @parameter
* name="webappDirectory"
* type="String"
* required="true"
* validator=""
* expression="#maven.war.webapp.dir"
* default="#project.build.output/#project.build.finalName"
* description=""
* @parameter
* name="mode"
* type="String"
* required="true"
* validator=""
* expression="#maven.war.mode"
* default="war"
* description=""
* @parameter
* name="outputDirectory"
* type="String"
* required="true"
* validator=""
* expression="#project.build.output"
* expression="#maven.war.build.dir"
* default="#project.build.output"
* description=""
* @parameter
* name="basedir"
@ -94,6 +137,12 @@ import org.apache.maven.project.MavenProject;
* validator=""
* expression="#project.build.directory"
* description=""
* @parameter name="localRepository"
* type="org.apache.maven.artifact.repository.ArtifactRepository"
* required="true"
* validator=""
* expression="#localRepository"
* description=""
* @parameter
* name="project"
* type="org.apache.maven.project.MavenProject"
@ -108,13 +157,164 @@ import org.apache.maven.project.MavenProject;
public class WarMojo
extends AbstractPlugin
{
public static final String WEB_INF = "WEB_INF";
private PluginExecutionRequest request;
private String mode;
private MavenProject project;
private ArtifactRepository localRepository;
private String outputDirectory;
private File webappDirectory;
private File warSourceDirectory;
private String warSourceIncludes;
private String warSourceExcludes;
private String webXml;
private File warFile;
public void copyResources( File sourceDirectory, File webappDirectory, String includes, String excludes, String webXml )
throws IOException
{
if ( sourceDirectory != webappDirectory )
{
request.getLog().info( "Copy webapp resources to " + webappDirectory.getAbsolutePath() );
if ( warSourceDirectory.exists() )
{
FileUtils.copyDirectory( sourceDirectory, webappDirectory, includes, excludes );
}
if ( webXml != null && ! "".equals( webXml ) )
{
FileUtils.copyFileToDirectory( new File( webXml ), new File( webappDirectory, WEB_INF ) );
}
}
}
/**
* @todo properties 'war.bundle' and 'war.target.path'
* @todo copy classes to classes webapp directory
*/
public void buildWebapp( MavenProject project )
throws IOException
{
request.getLog().info( "Assembling webapp " + project.getArtifactId() );
File libDirectory = new File( webappDirectory, WEB_INF + "/lib" );
File tldDirectory = new File( webappDirectory, WEB_INF + "/tld" );
File classesDirectory = new File( webappDirectory, WEB_INF + "/classes" );
Set artifacts = project.getArtifacts();
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
{
Artifact artifact = (Artifact) iter.next();
if ( "jar".equals( artifact.getType() ) )
{
FileUtils.copyFileToDirectory( new File( localRepository.getBasedir(), artifact.toString() ) , libDirectory );
}
if ( "tld".equals( artifact.getType() ) )
{
FileUtils.copyFileToDirectory( new File( localRepository.getBasedir(), artifact.toString() ) , tldDirectory );
}
}
}
public void generateExplodedWebapp()
throws IOException
{
webappDirectory.mkdirs();
File webinfDir = new File( webappDirectory, WEB_INF );
webinfDir.mkdirs();
copyResources( warSourceDirectory, webappDirectory, warSourceIncludes, warSourceExcludes, webXml );
//buildWebapp( project );
}
public void generateInPlaceWebapp()
throws IOException
{
webappDirectory = warSourceDirectory;
generateExplodedWebapp();
}
public void execute( PluginExecutionRequest request, PluginExecutionResponse response )
throws Exception
{
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
request.getLog().info("war");
parseRequest( request );
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
if ( "inplace".equals( mode ) )
{
generateInPlaceWebapp();
}
else
{
generateExplodedWebapp();
if ( ! "exploded".equals( mode ) )
{
//generate war file
request.getLog().info( "Generating war " + warFile.getAbsolutePath() );
MavenArchiver archiver = new MavenArchiver();
//archiver.setArchiver( new WarArchiver() );
archiver.setOutputFile( warFile );
archiver.getArchiver().addDirectory( webappDirectory, new String[] { "**/**" }, null );
// create archive
archiver.createArchive( request );
}
}
}
public void parseRequest( PluginExecutionRequest request )
{
this.request = request;
project = (MavenProject) request.getParameter( "project" );
localRepository = (ArtifactRepository) request.getParameter( "localRepository" );
outputDirectory = (String) request.getParameter( "outputDirectory" );
webappDirectory = new File( (String) request.getParameter( "webappDirectory" ) );
warSourceDirectory = new File( (String) request.getParameter( "warSourceDirectory" ) );
warSourceIncludes = (String) request.getParameter( "warSourceIncludes" );
warSourceExcludes = (String) request.getParameter( "warSourceExcludes" );
webXml = (String) request.getParameter( "webXml" );
mode = (String) request.getParameter( "mode" );
warFile = new File( outputDirectory, (String) request.getParameter( "warName" ) + ".war" );
}
}

View File

@ -58,7 +58,7 @@
<dependency>
<groupId>plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>1.0-alpha-1-SNAPSHOT</version>
<version>1.0-alpha-2-SNAPSHOT</version>
</dependency>
</dependencies>
</project>