Subversion Repositories DevTools

Rev

Blame | Last modification | View Log | RSS feed

package com.erggroup.mass.ant;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.FileScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Jar;
import org.apache.tools.ant.taskdefs.Manifest;
import org.apache.tools.ant.taskdefs.ManifestException;
import org.apache.tools.ant.types.FileSet;


public class ManifestElement implements JatsElement
{
        private static final String MANIFEST_NAME = "META-INF/MANIFEST.MF";
        
        static final String TASK_NAME = "manifest";
        
        private String implementationVersion;
        
        private ArrayList filesets = new ArrayList();

        public FileSet createFileset()
        {
                 FileSet fileset = new FileSet();
                filesets.add( fileset );
                 return fileset;
        }
        
        public void setImplementationVersion(String value)
        {
                this.implementationVersion = value;
        }       
        
        public void execute(Jats parent)
        {
                if ( filesets.size() == 0 ) 
                {
                        throw new BuildException("At least one fileset must be provided");      
                }
                
                for ( Iterator i=filesets.iterator(); i.hasNext(); )
                {
                        FileSet fileset = (FileSet)i.next();
                        FileScanner scanner = fileset.getDirectoryScanner( parent.getProject() );
                        String[] files = scanner.getIncludedFiles();
                        File basedir = scanner.getBasedir();
                        for ( int j=0; j<files.length; j++ )
                        {
                                //log( "Updating manifest for file " + files[j], Project.MSG_VERBOSE );
                                File file = new File( basedir, files[j] );
                                updateManifest( file, parent.getProject() );
                        }
                }
        }
        
        protected void updateManifest( File jarFile, Project p )
        {
                Jar jar = new Jar();    
                jar.setDestFile(jarFile);
                jar.setUpdate(true);            
                
                try
                {
                        Manifest newManifest = new Manifest();
                        if ( implementationVersion != null )
                                newManifest.addConfiguredAttribute( new Manifest.Attribute( "Implementation-Version", implementationVersion ) );
                        newManifest.addConfiguredAttribute( new Manifest.Attribute( "Implementation-Vendor", "ERG Transit Systems" ) );                         
                        jar.addConfiguredManifest(newManifest);
                }
                catch ( ManifestException x )
                {
                        throw new BuildException("Manifest creation error.",x);                 
                }
                jar.setProject(p);
                jar.execute();
        }

        public String getElementType()
        {
                return TASK_NAME;
        }

}