Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Work Your Own Way With Crafter CMS (Series Part 1): Step-Through Debugging

DZone's Guide to

Work Your Own Way With Crafter CMS (Series Part 1): Step-Through Debugging

CMS platforms are typically built for more non-technical audiences. But in this article, we explore a developer friendly CMS, and it can be used to debug your web apps.

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

Most CMS platforms do a decent job of simplifying content and digital experience creation and editing for non-technical content managers. The challenges really start once you need to innovate and development is required. Traditionally CMS platforms have been pretty bad for developers. They require a lot of CMS specific knowledge and don't integrate with developer tools and process.
Here are 7 things that developers really want with a CMS:

  1. Let me work locally with my own tools like my IDE and my source code management.
  2. Let me leverage my existing skills. I want a low learning curve. Don't make me learn a new, niche framework.
  3. Let me work in teams on multiple projects at the same time without interfering.
  4. Let me maintain a real development process.
  5. Make the integration with the CMS seamless.
  6. Don't make me do builds.
  7. Don't make me do heavy deployments.

In this installment of the Work Your Way Series, we're going to tackle item #1: Let me work locally with my own tools like my IDE and my source code management. Let's start with some background: Crafter CMS uses Git as its primary content store. That's the foundation of the solution for developer desire #1. A developer can mount a local clone of a Crafter CMS project directly with their IntelliJ, Netbeans, Eclipse or other IDE. That means they can use their preferred development tools to edit and debug code and templates. And as they work, all of the changes they make are tracked by the Crafter CMS via its native Git support. Sounds awesome right? Let's learn how to get set up.

Step 1: Get a Local Copy of Crafter CMS Running

You are going to use your IDE to update and debug your code and templates. You'll want a local instance of Crafter CMS running so you to execute the code and to which you can attach your debugger.

To install and get Crafter CMS running (authoring environment) locally follow this guide.

Step 2: Start a Crafter CMS Project

Now that Crafter CMS is up and running, let's create a project so we have a place to work. We'll create a local project for the sake of this article, however, as we'll learn later in this series, it's possible to clone a remotely managed project as well.

To create your CMS project, follow this guide.

Step 3: Mount Your IDE on Top of Your Crafter CMS Project

For this article, I'm going to use IntelliJ. Any IDE that allows you to connect to a remote JVM via Java Debug Wire Protocol (JDWP) should work. Eclipse and Netbeans are other solid IDE options.

1. Within IntellJ, create a new project: 

2. Select Create New Project: 

3. Choose Groovy: 


  • A: Configure your project name to something you like.
  • B: Browse to the Git repo "Sandbox" under your "CRAFTER-INSTALL/data/repos/sites/PROJECTID/sandbox"
  • C: Change the module name from Sandbox to your project name.

  • Once your project is created, open it to see all the files. IntelliJ automatically recognizes you are sitting on top of a Git repo and will allow you to do Git operations right from the IDE.
  • Note that a Groovy project creates an "src" folder. You can delete this. You don't need it.


  • Right click on your model and add a new file called ".gitignore"
  • Do not add this file to Git when prompted.

The contents of the .gitingore file should be as follows:

*.iml
.gitignore
.idea/*
  • This will Git and Crafter CMS to ignore your IDE project files. 

Great, now that we've got our IDE sitting on top of our project and we've got the Git integration configured, we're ready to set up the step through-debugging.

  • Shut down Crafter CMS.
  • Add the following lines to CRAFTER-INSTALL/bin/apache-tomcat/bin/setenv.sh (or setenv.bat).

export CATALINA_OPTS="$CATALINA_OPTS -agentlib:jdwp=transport=dt_socket,address=localhost:8000,server=y,suspend=n"
  • Start Crafter CMS.

Back in our IDE, we can now configure and start our remote debugging session!

  • Go to 'Run' and select 'Debug.'

  • Since this is a new project, we’ve got to configure our debugging connection. The next time you click Debug, you’ll just select the existing configuration and click Run.

  • Click the + (plus) icon to create a new configuration.
  • Select "Remote."

  • Give the debug configuration a name.
  • Change the port from 5005 to 8000 (to match the value in setenv.sh).
  • Click "Apply" to accept the changes.
  • Click "Run" to attach to the JVM running Crafter CMS.

  • Congrats! If your JVM configuration is correct and your ports match in the debug config when you click “Run” you’ll see the “Connected” message in the console as shown above.

Step 5: Start Debugging

Now that we're connected, we can start stepping through code in our Groovy Scripts.

  • Open the "Scripts" folder in your project.
  • Browse down through classes, org, craftercms, sites, editorial, and open SearchHelper.groovy.
  • Set a breakpoint at line 56. This method is used by the Home Page controller of the editorial website we created in step 2.
  • In a browser, open the Crafter CMS preview for the homepage of the website we created.

  • When you load the homepage in Crafter Studio, the rendering will pause.

  • IntelliJ's window will typically come to the foreground automatically.
  • The thread has been paused and the IDE is now in control of the thread execution.
  • You can now use the step-through debugging tools in the IDE to walk through the code.

Step 6: Magic

Found a bug? Here's where things get really fun. We're about to see the benefit of bringing Git, Groovy, and your IDE together in one place. Fixing the bug and sharing that fix with the rest of your team is a breeze:

  1. Once you understand the bug and you know what code you want to change, click Play to let the thread complete.
  2. Using the IDE, fix the Groovy code.
  3. Simply refresh the browser again to test. Step through and verify things are working.
  4. Using the IDE to interface with Git, commit the code and push the code forward to your team mates.
  5. Find something fun to do or work on. You have a lot more time on your hands now that you are working in a truly integrated, no-compile required, easy to code and debug CMS.

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

Topics:
web dev ,cms ,bug fixing ,ide

Published at DZone with permission of Russ Danner, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}