[opencms-dev] Ant task for OpenCms 6.0 Synchronization (Eclipse)

Craig Byrne craig.byrne at gmail.com
Tue Apr 5 11:06:17 CEST 2005


Don't think attachments work entirely with this mailing list, so I'll 
post again in text.  There is an attachment in the original message 
which has the eclipse project for the task which can be downloaded.

http://mail.opencms.org/pipermail/opencms-dev/2005q2/016209.html

Anyway, this is an ant task to perform OpenCms synchronization.

I had to cheat a little in making it work due to member visibility in a 
few of the OpenCms classes.  I had to put the class into the 
org.opencms.main package (it would be good if maybe the OpenCms 
developers could relax it a bit.)

Cheers,

########## /src/org/opencms/main/CmsSynchronizeAntTask.java ################

package org.opencms.main;

import org.apache.commons.collections.ExtendedProperties;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.BuildException;
import org.opencms.file.CmsObject;
import org.opencms.report.CmsShellReport;
import org.opencms.synchronize.CmsSynchronize;
import org.opencms.synchronize.CmsSynchronizeSettings;
import org.opencms.util.CmsPropertyUtils;

/**
 * @author cbyrne at bigpond.net.au
 *
 * OpenCMS 6.0 Ant Task to perform synchronization.
 *
 * Most of the code is borrowed from the CmsShell and CmsShellCommand 
sources.
 *
 * Had to do a nasty hack, named this package the same as an OpenCMS package
 * to get around problems with some method visibility.  It would be good if
 * OpenCMS could relax some of the visibility on methods.
 *
 * Created on Mar 29, 2005
 *
 */
public class CmsSynchronizeAntTask extends Task {

    public String username;

    public String password;

    public String vfs;
   
    public String rfs;
   
    public String home;

    /**
     * Get password of the OpenCMS user.
     *
     * @return Returns the password.
     */
    public String getPassword() {
        return password;
    }

    /**
     * Set the password of the OpenCMS user.
     *
     * @param password
     *            The password to set.
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * Get the username of the OpenCMS user.
     *
     * @return Returns the username.
     */
    public String getUsername() {
        return username;
    }

    /**
     * Set the username of the OpenCMS user.
     *
     * @param username
     *            The username to set.
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * Set the home directory for OpenCms.
     *
     * @return Returns the home.
     */
    public String getHome() {
        return home;
    }

    /**
     * Get the home directory for OpenCms.
     *
     * @param home
     *            The home to set.
     */
    public void setHome(String home) {
        this.home = home;
    }

    /**
     * Get the directory of the real file system.
     *
     * @return Returns the rfs.
     */
    public String getRfs() {
        return rfs;
    }
   
    /**
     * Set the directory of the real file system.
     *
     * @param rfs The rfs to set.
     */
    public void setRfs(String rfs) {
        this.rfs = rfs;
    }
   
    /**
     * Get the directory of the virtual file system.
     *
     * @return Returns the vfs.
     */
    public String getVfs() {
        return vfs;
    }
   
    /**
     * Set the directory of the virtual file system.
     *
     * @param vfs The vfs to set.
     */
    public void setVfs(String vfs) {
        this.vfs = vfs;
    }
       
    /* (non-Javadoc)
     * @see org.apache.tools.ant.Task#execute()
     */
    public void execute() throws BuildException {
        try {
            // first initialize runlevel 1
            OpenCmsCore opencms = OpenCmsCore.getInstance();

            // search for the WEB-INF folder
            // set the path to the WEB-INF folder (the 2nd and 3rd 
parameters
            // are just reasonable dummies)
            opencms.getSystemInfo().init(home, "/opencms/*", null, "ROOT");

            // now read the configuration properties
            String propertyPath = 
opencms.getSystemInfo().getConfigurationFileRfsPath();
            ExtendedProperties configuration = 
CmsPropertyUtils.loadProperties(propertyPath);

            // now upgrade to runlevel 2
            opencms = opencms.upgradeRunlevel(configuration);
           
            // create a context object with 'Guest' permissions
            CmsObject cms = 
opencms.initCmsObject(opencms.getDefaultUsers().getUserGuest());

            // login using username and password from ant task
            cms.loginUser(username, password);
           
            // set the project to the offline project
            cms.getRequestContext().setCurrentProject(cms.readProject(4));

            // initialize the synchronization settings
            CmsSynchronizeSettings settings = new CmsSynchronizeSettings();
            settings.setDestinationPathInRfs(rfs);
            settings.setSourcePathInVfs(vfs);
            OpenCms.getSystemInfo().setSynchronizeSettings(settings);
           
            // cms report where output messages are sent
            CmsShellReport report = new CmsShellReport();

            // instantiate and execute the synchronization
            CmsSynchronize sync = new CmsSynchronize(cms, report);
           
            // Shutdown the opencms system
            opencms.destroy();
        } catch (Exception e) {
            throw new BuildException(e);
        }
    }
}

########## build.xml ##########

<project name="opencmsanttask" default="compile" basedir=".">

<!-- ===================== Property Definitions 
=========================== -->

  <property file="build.properties"/>
  <property file="${user.home}/build.properties"/>

<!-- ==================== File and Directory Names 
======================== -->

  <property environment="env"/> 
  <property name="catalina.home"    value="${env.CATALINA_HOME}"/>
  <property name="build.home"       value="${basedir}/build"/>
  <property name="dist.home"        value="${basedir}/dist"/>
  <property name="docs.home"        value="${basedir}/docs"/>
  <property name="src.home"         value="${basedir}/src"/>
  <property name="web.home"         value="${basedir}/web"/>
   
<!--  ==================== Compilation Control Options 
==================== -->

  <property name="compile.debug"       value="true"/>
  <property name="compile.deprecation" value="true"/>
  <property name="compile.optimize"    value="false"/>

<!-- ==================== Compilation Classpath 
=========================== -->

  <path id="compile.classpath">
    <pathelement 
location="${catalina.home}/webapps/opencms/WEB-INF/classes"/>
    <fileset dir="${catalina.home}/webapps/opencms/WEB-INF/lib">
      <include name="*.jar"/>
      <exclude name="xercesImpl-2.6.2.jar"/>
    </fileset>
    <fileset dir="${catalina.home}/common/lib">
      <include name="*.jar"/>
    </fileset>
 </path>

<!-- ==================== All Target 
====================================== -->

  <target name="all" depends="clean,compile" description="Clean build 
and dist directories, then compile"/>

<!-- ==================== Clean Target 
==================================== -->

  <target name="clean" description="Delete old build and dist directories">
    <delete dir="${build.home}"/>
    <delete dir="${dist.home}"/>
     <delete>
        <fileset dir="." includes="**/*.bak,**/*.*~,**/*.*~~" 
defaultexcludes="no"/>
    </delete>
  </target>
   
<!-- ==================== Compile Target 
================================== -->

  <target name="compile" description="Compile Java sources">
    <mkdir    dir="${build.home}"/>
    <javac srcdir="${src.home}"
          destdir="${build.home}"
            debug="${compile.debug}"
      deprecation="${compile.deprecation}"
         optimize="${compile.optimize}"
                  verbose="false">
        <classpath refid="compile.classpath"/>
    </javac>
  </target>

<!-- ==================== Dist Target 
===================================== -->

  <target name="dist" depends="compile" description="Create binary 
distribution">
    <mkdir  dir="${dist.home}"/>
    <jar jarfile="${dist.home}/${app.name}.jar" basedir="${build.home}"/>
  </target>

<!-- ==================== Javadoc Target 
================================== -->

  <target name="javadoc" depends="compile" description="Create Javadoc 
API documentation">
    <mkdir dir="${dist.home}/docs/api"/>
    <javadoc sourcepath="${src.home}"
                destdir="${dist.home}/docs/api"
           packagenames="*.*">
      <classpath refid="compile.classpath"/>
    </javadoc>
  </target>

</project>


########### test.xml ##########

<project name="opencmsanttest" default="test" basedir=".">

<!-- ==================== File and Directory Names 
======================== -->

  <property environment="env"/> 
  <property name="catalina.home"    value="${env.CATALINA_HOME}"/>
  <property name="build.home"       value="${basedir}/build"/>

<!-- ==================== OpenCms Classpath 
=============================== -->

  <path id="opencms.classpath">
    <pathelement location="${build.home}"/>
    <pathelement 
location="${catalina.home}/webapps/opencms/WEB-INF/classes"/>
    <fileset dir="${catalina.home}/webapps/opencms/WEB-INF/lib">
      <include name="*.jar"/>
      <exclude name="xercesImpl-2.6.2.jar"/>
    </fileset>
    <fileset dir="${catalina.home}/common/lib">
      <include name="*.jar"/>
    </fileset>
  </path>
     
<!-- ================== Custom Ant Task Definitions 
======================= -->

  <taskdef name="synchronize" 
classname="org.opencms.main.CmsSynchronizeAntTask" 
classpathref="opencms.classpath"/>

<!-- ================== Test Ant Task 
===================================== -->

  <target name="test">
    <synchronize username="Admin" password="admin" vfs="/sites/default" 
rfs="c:/sync" home="${catalina.home}/webapps/opencms/WEB-INF"/>
  </target>

</project>




More information about the opencms-dev mailing list