public function serialize()
    {
        Xinc_Logger::getInstance()->flush();
        $this->setLastBuild();

        if (!in_array($this->getStatus(), array(self::PASSED, self::FAILED, self::STOPPED))) {
            throw new Xinc_Build_Exception_NotRun();
        } elseif ($this->getBuildTime() == null) {
            throw new Xinc_Build_Exception_Serialization($this->getProject(),
                                                         $this->getBuildTime());
        }
        $statusDir = Xinc::getInstance()->getStatusDir();

        $buildHistoryFile = $statusDir.DIRECTORY_SEPARATOR
                          .$this->getProject()->getName().'.history';

        $subDirectory = self::generateStatusSubDir($this->getProject()->getName(), $this->getBuildTime());

        $fileName = $statusDir.DIRECTORY_SEPARATOR
                  .$subDirectory
                  .DIRECTORY_SEPARATOR.'build.ser';
        $logfileName = $statusDir.DIRECTORY_SEPARATOR
                  .$subDirectory
                  .DIRECTORY_SEPARATOR.'buildlog.xml';
        $lastBuildFileName = $statusDir.DIRECTORY_SEPARATOR.$this->getProject()->getName()
                           .DIRECTORY_SEPARATOR.'build.ser';
        $lastLogFileName = $statusDir.DIRECTORY_SEPARATOR.$this->getProject()->getName()
                           .DIRECTORY_SEPARATOR.'buildlog.xml';
        if (!file_exists(dirname($fileName))) {
            mkdir(dirname($fileName), 0755, true);
        }
        $contents = serialize($this);

        $written = file_put_contents($lastBuildFileName, $contents);
        if ($written == strlen($contents)) {
            $res = copy($lastBuildFileName, $fileName);
            if (!$res) {
                throw new Xinc_Build_Exception_Serialization($this->getProject(),
                                                             $this->getBuildTime());
            } else {
                if (file_exists($lastLogFileName)) {
                    copy($lastLogFileName, $logfileName);
                    unlink($lastLogFileName);
                }
                Xinc_Build_History::addBuild($this, $fileName);
            }

            return true;
        } else {
            throw new Xinc_Build_Exception_Serialization($this->getProject(),
                                                         $this->getBuildTime());
        }
    }