001    /*
002     * Copyright 2005 Stephen J. McConnell
003     *
004     * Licensed  under the  Apache License,  Version 2.0  (the "License");
005     * you may not use  this file  except in  compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *   http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed  under the  License is distributed on an "AS IS" BASIS,
012     * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
013     * implied.
014     *
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package net.dpml.tools.tasks;
020    
021    import java.io.File;
022    
023    import net.dpml.library.Resource;
024    
025    import net.dpml.tools.Context;
026    import net.dpml.tools.BuildError;
027    
028    import org.apache.tools.ant.BuildException;
029    import org.apache.tools.ant.Project;
030    
031    /**
032     * Cleanup of generated target directory.
033     *
034     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
035     * @version 1.1.3
036     */
037    public class PackageTask extends GenericTask
038    {
039       /**
040        * Compiles main and test classes resulting in the creation of the 
041        * target/classes/main and target/classes/test directories.
042        */
043        public void execute()
044        {
045            Context context = getContext();
046            Project project = context.getProject();
047            Resource resource = context.getResource();
048            
049            // handle jar file packaging
050            
051            if( resource.isa( "jar" ) )
052            {
053                File base = context.getTargetClassesMainDirectory();
054                final File jar = getJarFile( context );
055                if( base.exists() )
056                {
057                    final JarTask task = new JarTask();
058                    task.setProject( project );
059                    task.setTaskName( "jar" );
060                    task.setSrc( base );
061                    task.setDest( jar );
062                    task.init();
063                    task.execute();
064                }
065            }
066            
067            // handle part production
068            
069            if( resource.isa( "part" ) )
070            {
071                try
072                {
073                    PartTask task = new PartTask();
074                    task.setProject( project );
075                    task.setTaskName( "part" );
076                    task.init();
077                    task.execute();
078                }
079                catch( BuildException e )
080                {
081                    throw e;
082                }
083                catch( Throwable e )
084                {
085                    e.printStackTrace();
086                    final String error = 
087                      "Unexpected failure during part externalization in ["
088                        + resource.getName()
089                        + "]";
090                    throw new BuildError( error, e, getLocation() );
091                }
092            }
093            
094            // handle module export
095            
096            if( resource.isa( "module" ) )
097            {
098                final ModuleTask task = new ModuleTask();
099                task.setProject( project );
100                task.setTaskName( "module" );
101                task.init();
102                task.execute();
103            }
104        }
105        
106        private File getJarFile( Context context )
107        {
108            Project project = context.getProject();
109            File deliverables = context.getTargetDeliverablesDirectory();
110            File jars = new File( deliverables, "jars" );
111            String filename = context.getLayoutFilename( "jar" );
112            return new File( jars, filename );
113        }
114    }