groovy publication tools System Administrator Jahia 7.3 Jahia 8

How to force a publication

Question

You have a message "Nothing to publish" but you know that something has been modified. 

Answer

A solution is to force a publication.

To see if a node needs to be published or not, Jahia compare the last modification date with the publication date. So a good way to force a publication is to update the last publication node of all nodes of a sub-tree.
Here is a script you could use to force a publication. Please set the startPath with the starting node path. You can run this script from the Groovy Console of the tools

import org.jahia.api.Constants
import javax.jcr.NodeIterator
import javax.jcr.RepositoryException
import javax.jcr.query.Query

String startPath = "/sites/mySite/home";

JCRTemplate.getInstance().doExecuteWithSystemSession(null, Constants.EDIT_WORKSPACE, null, new JCRCallback() {
    @Override
    Object doInJCR(JCRSessionWrapper session) throws RepositoryException {
        try {
            // touch the starting node
            JCRNodeWrapper startNode = session.getNode(startPath);
            touch(startNode);

            // touch all sub nodes
            String query = "select * from [nt:base] as p where isdescendantnode('" +  startPath + "')";
            NodeIterator iterator = session.getWorkspace().getQueryManager().createQuery(query, Query.JCR_SQL2).execute().getNodes();
            while (iterator.hasNext()) {
                JCRNodeWrapper node = (JCRNodeWrapper) iterator.nextNode();
                touch(node);
            }
        } catch (javax.jcr.PathNotFoundException e) {
            log.info("Could not find node with path " + startPath);
        }
        session.save();
        return null;
    }
});


private void touch(JCRNodeWrapper node) {
    if (node != null) {
        try {
            node.setProperty("jcr:lastModified", new GregorianCalendar());
            log.info(node.getPath())
        } catch (javax.jcr.nodetype.ConstraintViolationException e) {
        }
    }
}