public void execute()

in installers-maven-plugin/src/main/java/org/apache/directory/server/installers/macosxpkg/MacOsXPkgInstallerCommand.java [199:507]


    public void execute() throws MojoExecutionException, MojoFailureException
    {
        // Verifying the target
        if ( !verifyTarget() )
        {
            return;
        }

        log.info( "  Creating Mac OS X PKG installer..." );

        // Creating the target directory
        File targetDirectory = getTargetDirectory();

        if ( !targetDirectory.mkdirs() )
        {
            Exception e = new IOException( I18n.err( I18n.ERR_00004_COULD_NOT_CREATE_DIRECTORY, targetDirectory ) );
            log.error( e.getLocalizedMessage() );
            throw new MojoFailureException( e.getMessage() );
        }

        log.info( "    Copying PKG installer files" );

        // Creating the root directories hierarchy
        File pkgRootDirectory = new File( targetDirectory, ROOT );

        if ( !pkgRootDirectory.mkdirs() )
        {
            Exception e = new IOException( I18n.err( I18n.ERR_00004_COULD_NOT_CREATE_DIRECTORY, pkgRootDirectory ) );
            log.error( e.getLocalizedMessage() );
            throw new MojoFailureException( e.getMessage() );
        }

        File pkgRootUsrBinDirectory = new File( pkgRootDirectory, USR_BIN );

        if ( !pkgRootUsrBinDirectory.mkdirs() )
        {
            Exception e = new IOException( I18n.err( I18n.ERR_00004_COULD_NOT_CREATE_DIRECTORY, pkgRootUsrBinDirectory ) );
            log.error( e.getLocalizedMessage() );
            throw new MojoFailureException( e.getMessage() );
        }

        File pkgRootUsrLocalApachedsDirectory = new File( pkgRootDirectory, USR_LOCAL_APACHEDS
            + mojo.getProject().getVersion() );

        if ( !pkgRootUsrLocalApachedsDirectory.mkdirs() )
        {
            Exception e = new IOException( I18n.err( I18n.ERR_00004_COULD_NOT_CREATE_DIRECTORY,
                pkgRootUsrLocalApachedsDirectory ) );
            log.error( e.getLocalizedMessage() );
            throw new MojoFailureException( e.getMessage() );
        }

        File pkgRootInstancesDirectory = new File( pkgRootUsrLocalApachedsDirectory, INSTANCES );

        if ( !pkgRootInstancesDirectory.mkdirs() )
        {
            Exception e = new IOException( I18n.err( I18n.ERR_00004_COULD_NOT_CREATE_DIRECTORY, pkgRootInstancesDirectory ) );
            log.error( e.getLocalizedMessage() );
            throw new MojoFailureException( e.getMessage() );
        }

        File pkgRootInstancesDefaultDirectory = new File( pkgRootInstancesDirectory, DEFAULT );

        if ( !pkgRootInstancesDefaultDirectory.mkdirs() )
        {
            Exception e = new IOException( I18n.err( I18n.ERR_00004_COULD_NOT_CREATE_DIRECTORY,
                pkgRootInstancesDefaultDirectory ) );
            log.error( e.getLocalizedMessage() );
            throw new MojoFailureException( e.getMessage() );
        }

        File pkgRootInstancesDefaultConfDirectory = new File( pkgRootInstancesDefaultDirectory,
            InstanceLayout.CONF_NAME );

        if ( !pkgRootInstancesDefaultConfDirectory.mkdirs() )
        {
            Exception e = new IOException( I18n.err( I18n.ERR_00004_COULD_NOT_CREATE_DIRECTORY,
                pkgRootInstancesDefaultConfDirectory ) );
            log.error( e.getLocalizedMessage() );
            throw new MojoFailureException( e.getMessage() );
        }

        File pkgRootInstancesDefaultDirectoryLog = new File( pkgRootInstancesDefaultDirectory, InstanceLayout.LOG_NAME );

        if ( !pkgRootInstancesDefaultDirectoryLog.mkdirs() )
        {
            Exception e = new IOException( I18n.err( I18n.ERR_00004_COULD_NOT_CREATE_DIRECTORY,
                pkgRootInstancesDefaultDirectoryLog ) );
            log.error( e.getLocalizedMessage() );
            throw new MojoFailureException( e.getMessage() );
        }

        File pkgRootInstancesDefaultDirectoryPartitions = new File( pkgRootInstancesDefaultDirectory,
            InstanceLayout.PARTITIONS_NAME );

        if ( !pkgRootInstancesDefaultDirectoryPartitions.mkdirs() )
        {
            Exception e = new IOException( I18n.err( I18n.ERR_00004_COULD_NOT_CREATE_DIRECTORY,
                pkgRootInstancesDefaultDirectoryPartitions ) );
            log.error( e.getLocalizedMessage() );
            throw new MojoFailureException( e.getMessage() );
        }

        File pkgRootInstancesDefaultDirectoryRun = new File( pkgRootInstancesDefaultDirectory, InstanceLayout.RUN_NAME );

        if ( !pkgRootInstancesDefaultDirectoryRun.mkdirs() )
        {
            Exception e = new IOException( I18n.err( I18n.ERR_00004_COULD_NOT_CREATE_DIRECTORY,
                pkgRootInstancesDefaultDirectoryRun ) );
            log.error( e.getLocalizedMessage() );
            throw new MojoFailureException( e.getMessage() );
        }

        File pkgRootLibraryLaunchDaemons = new File( pkgRootDirectory, LAUNCH_DAEMONS );

        if ( !pkgRootLibraryLaunchDaemons.mkdirs() )
        {
            Exception e = new IOException( I18n.err( I18n.ERR_00004_COULD_NOT_CREATE_DIRECTORY,
                pkgRootLibraryLaunchDaemons ) );
            log.error( e.getLocalizedMessage() );
            throw new MojoFailureException( e.getMessage() );
        }

        // Copying the apacheds files in the root directory
        try
        {
            // Creating the installation and instance layouts
            createLayouts();

            // Copying the apacheds command to /usr/bin
            MojoHelperUtils.copyAsciiFile( mojo, filterProperties, APACHEDS_USR_BIN_SH,
                getClass().getResourceAsStream( APACHEDS_USR_BIN_SH ),
                new File( pkgRootUsrBinDirectory, APACHEDS ), true );

            // Copying the org.apache.directory.server.plist file to /Library/LaunchDaemons/
            MojoHelperUtils.copyAsciiFile( mojo, filterProperties, ORG_APACHE_DIRECTORY_SERVER_PLIST,
                getClass().getResourceAsStream( ORG_APACHE_DIRECTORY_SERVER_PLIST ),
                new File( pkgRootLibraryLaunchDaemons, ORG_APACHE_DIRECTORY_SERVER_PLIST ), true );

            // Create Resources folder and sub-folder
            // Copying the resources files and Info.plist file needed for the 
            // generation of the PKG
            File pkgResourcesEnglishDirectory = new File( targetDirectory, RESOURCES_EN_LPROJ );

            if ( !pkgResourcesEnglishDirectory.mkdirs() )
            {
                Exception e = new IOException( I18n.err( I18n.ERR_00004_COULD_NOT_CREATE_DIRECTORY,
                    pkgResourcesEnglishDirectory ) );
                log.error( e.getLocalizedMessage() );
                throw new MojoFailureException( e.getMessage() );
            }

            File pkgScriptsDirectory = new File( targetDirectory, SCRIPTS );

            if ( !pkgScriptsDirectory.mkdirs() )
            {
                Exception e = new IOException( I18n.err( I18n.ERR_00004_COULD_NOT_CREATE_DIRECTORY, pkgScriptsDirectory ) );
                log.error( e.getLocalizedMessage() );
                throw new MojoFailureException( e.getMessage() );
            }

            MojoHelperUtils.copyBinaryFile( mojo, PKG_BACKGROUND_TIFF,
                getClass().getResourceAsStream( PKG_BACKGROUND_TIFF ),
                new File( pkgResourcesEnglishDirectory, BACKGROUND_TIFF ) );

            MojoHelperUtils.copyBinaryFile( mojo, LICENSE_RTF,
                getClass().getResourceAsStream( LICENSE_RTF ),
                new File( pkgResourcesEnglishDirectory, LICENSE_RTF ) );

            MojoHelperUtils.copyBinaryFile( mojo, INFO_PLIST,
                getClass().getResourceAsStream( INFO_PLIST ), new File( targetDirectory, INFO_PLIST ) );

            MojoHelperUtils.copyBinaryFile( mojo, PREFLIGHT,
                getClass().getResourceAsStream( PREFLIGHT ), new File( pkgScriptsDirectory, PREFLIGHT ) );

            MojoHelperUtils.copyBinaryFile( mojo, POSTFLIGHT,
                getClass().getResourceAsStream( POSTFLIGHT ), new File( pkgScriptsDirectory, POSTFLIGHT ) );
        }
        catch ( Exception e )
        {
            log.error( e.getMessage() );
            throw new MojoFailureException( "Failed to copy PKG resources files." );
        }

        // Generating the PKG
        log.info( "    Generating Mac OS X PKG Installer" );
        Execute createPkgTask = new Execute();
        String[] cmd = new String[]
            {
                mojo.getPackageMakerUtility().getAbsolutePath(),
                "--root", ROOT + "/",
                "--resources", "Resources/",
                "--info", INFO_PLIST,
                "--title", "Apache Directory Server " + mojo.getProject().getVersion(),
                "--version", mojo.getProject().getVersion(),
                "--scripts", SCRIPTS,
                "--out", "Apache Directory Server Installer.pkg"
        };

        createPkgTask.setCommandline( cmd );
        createPkgTask.setWorkingDirectory( targetDirectory );

        try
        {
            createPkgTask.execute();
        }
        catch ( IOException e )
        {
            log.error( e.getMessage() );
            throw new MojoFailureException( "Failed while trying to generate the PKG: " + e.getMessage() );
        }

        log.info( "  Creating Mac OS X DMG..." );

        // Creating the disc image directory
        File dmgDirectory = new File( mojo.getOutputDirectory(), target.getId() + DASH_DMG );

        if ( !dmgDirectory.mkdirs() )
        {
            Exception e = new IOException( I18n.err( I18n.ERR_00004_COULD_NOT_CREATE_DIRECTORY, dmgDirectory ) );
            log.error( e.getLocalizedMessage() );
            throw new MojoFailureException( e.getMessage() );
        }

        log.info( "    Copying DMG files" );

        // Create dmg directory and its sub-directory
        File dmgDmgBackgroundDirectory = new File( dmgDirectory, DMG_DOT_BACKGROUND );

        if ( !dmgDmgBackgroundDirectory.mkdirs() )
        {
            Exception e = new IOException( I18n.err( I18n.ERR_00004_COULD_NOT_CREATE_DIRECTORY, dmgDmgBackgroundDirectory ) );
            log.error( e.getLocalizedMessage() );
            throw new MojoFailureException( e.getMessage() );
        }

        // Copying the files
        try
        {
            MojoHelperUtils.copyBinaryFile( mojo, "dmg-background.png",
                getClass().getResourceAsStream( "dmg-background.png" ),
                new File( dmgDirectory, "dmg/.background/background.png" ) );

            MojoHelperUtils.copyBinaryFile( mojo, "DS_Store",
                getClass().getResourceAsStream( "DS_Store" ),
                new File( dmgDirectory, "dmg/.DS_Store" ) );

            MojoHelperUtils.copyFiles( new File( targetDirectory, "Apache Directory Server Installer.pkg" ), new File(
                dmgDirectory, "dmg/Apache Directory Server Installer.pkg" ) );

        }
        catch ( IOException e )
        {
            log.error( e.getMessage() );
            throw new MojoFailureException( "Failed to copy DMG resources files." );
        }

        // Setting execution permission to the preflight and postflight scripts
        // (unfortunately, the execution permission has been lost after the 
        // copy of the PKG to the dmg folder)
        MojoHelperUtils.exec( new String[]
            { CHMOD, RWX_RX_RX,
                new File( dmgDirectory, "dmg/Apache Directory Server Installer.pkg/Contents/Resources/preflight" )
                    .toString() }, dmgDirectory, false );

        MojoHelperUtils.exec( new String[]
            { CHMOD, RWX_RX_RX,
                new File( dmgDirectory, "dmg/Apache Directory Server Installer.pkg/Contents/Resources/postflight" )
                    .toString() }, dmgDirectory, false );

        // Generating the DMG
        log.info( "    Generating Mac OS X DMG Installer" );
        String finalName = target.getFinalName();

        if ( !finalName.endsWith( ".dmg" ) )
        {
            finalName = finalName + ".dmg";
        }

        try
        {
            Execute createDmgTask = new Execute();
            createDmgTask.setCommandline( new String[]
                { hdiutilUtility.getAbsolutePath(), "makehybrid", "-quiet", "-hfs", "-hfs-volume-name",
                    "Apache Directory Server Installer", "-hfs-openfolder", "dmg/", "dmg/", "-o", "TMP.dmg" } );
            createDmgTask.setWorkingDirectory( dmgDirectory );
            createDmgTask.execute();

            createDmgTask.setCommandline( new String[]
                {
                    hdiutilUtility.getAbsolutePath(),
                    "convert",
                    "-quiet",
                    "-format",
                    "UDZO",
                    "TMP.dmg",
                    "-o",
                    "../" + finalName } );
            createDmgTask.execute();

        }
        catch ( IOException e )
        {
            log.error( e.getMessage() );
            throw new MojoFailureException( "Failed while trying to generate the DMG: " + e.getMessage() );
        }

        log.info( "=> Mac OS X DMG generated at " + new File( mojo.getOutputDirectory(), finalName ) );
    }