"Smart" Appengine Devserver restarts for faster development lifecycle
Autor: Fábio Uechi | Publicado: 2013-07-06T09:27:00-03:00

We just started a new AppEngine Java project using the appengine-maven-plugin and the Eclipse IDE (Juno). We are using the appengine:devserver goal to start the devserver. It basically builds the entire project (compile, test and package) and after that launches the devserver pointing it to the generated webapp directory - which by default is: ${project.build.directory}/${project.build.finalName}



The Problem
Every edit made in the source directory is not recognized unless the server is restarted - neither static content nor&nbsp;newly compiled classes. Which is obvious since the devserver is monitoring only the target directory.
It's a very "bureaucratic" and nonproductive development environment - we need to stop and start the server even for a single CSS line change.



The Dream
Achieve the same productivity level we have when working with dynamic languages based development environment (i.e. Python). Just hit F5 in the browser to see the changes in static files and automatic server reload every time a Java class or descriptor file is compiled/changed.



The Solution&nbsp;
Using a little Ant-foo, we were able to create a target which synchronizes both directories.
Thie snippet below uses the sync Ant task to perform the static content synchronization (lines 1-9). Notice that everything inside src.webapp.dir is sync'ed except for the 3 directories declared in the preserveintarget element. We had to exclude them from the synchronization process because since they only exist in the target directory they'd be deleted otherwise. And finally a second sync to synchronize the compiled classes (lines 11-13).

&lt;sync verbose="true" todir="${target.webapp.dir}" includeEmptyDirs="true"&gt;
 &lt;fileset dir="${src.webapp.dir}" /&gt;
 &lt;preserveintarget&gt;
     &lt;!-- Ignore the directories below --&gt;
  &lt;include name="WEB-INF/lib/**" /&gt;
  &lt;include name="WEB-INF/classes/**" /&gt;
  &lt;include name="WEB-INF/appengine-generated/**" /&gt;
 &lt;/preserveintarget&gt;
&lt;/sync&gt;

&lt;sync verbose="true" todir="${target.webapp.dir}/WEB-INF/classes"&gt;
 &lt;fileset dir="${basedir}/target/classes" /&gt;
&lt;/sync&gt;


Then, we attached it to an Eclipse builder, which is triggered every time a change is made in the project ("Build automatically" flag enabled).
The same behavior can be achieved by creating a special maven profile and using a combination of the m2e lifecycle mappings and the maven antrun plugin.
Something like this:

&lt;profile&gt;
    &lt;id&gt;m2e&lt;/id&gt;
    &lt;activation&gt;
        &lt;property&gt;
            &lt;name&gt;m2e.version&lt;/name&gt;
        &lt;/property&gt;
    &lt;/activation&gt;
    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-antrun-plugin&lt;/artifactId&gt;
                &lt;version&gt;1.7&lt;/version&gt;
                &lt;executions&gt;
                    &lt;execution&gt;
                        &lt;phase&gt;process-classes&lt;/phase&gt;
                        &lt;goals&gt;
                            &lt;goal&gt;run&lt;/goal&gt;
                        &lt;/goals&gt;
                        &lt;configuration&gt;
                            &lt;target&gt;
                                &lt;property name="target.webapp.dir" value="${project.build.directory}/${project.build.finalName}" /&gt;
                                &lt;property name="src.webapp.dir" value="${basedir}/src/main/webapp" /&gt;
                                &lt;sync verbose="true" todir="${target.webapp.dir}" includeEmptyDirs="true"&gt;
                                    &lt;fileset dir="${src.webapp.dir}" /&gt;
                                    &lt;preserveintarget&gt;
                                        &lt;include name="WEB-INF/lib/**" /&gt;
                                        &lt;include name="WEB-INF/classes/**" /&gt;
                                        &lt;include name="WEB-INF/appengine-generated/**" /&gt;
                                    &lt;/preserveintarget&gt;
                                &lt;/sync&gt;
                                &lt;sync verbose="true" todir="${target.webapp.dir}/WEB-INF/classes"&gt;
                                    &lt;fileset dir="${basedir}/target/classes" /&gt;
                                &lt;/sync&gt;
                            &lt;/target&gt;
                        &lt;/configuration&gt;
                    &lt;/execution&gt;
                &lt;/executions&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
        &lt;pluginManagement&gt;
            &lt;plugins&gt;
                &lt;!-- This plugin's configuration is used to store Eclipse m2e settings 
      only. It has no influence on the Maven build itself. --&gt;
                &lt;plugin&gt;
                    &lt;groupId&gt;org.eclipse.m2e&lt;/groupId&gt;
                    &lt;artifactId&gt;lifecycle-mapping&lt;/artifactId&gt;
                    &lt;version&gt;1.0.0&lt;/version&gt;
                    &lt;configuration&gt;
                        &lt;lifecycleMappingMetadata&gt;
                            &lt;pluginExecutions&gt;
                                &lt;pluginExecution&gt;
                                    &lt;pluginExecutionFilter&gt;
                                        &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                                        &lt;artifactId&gt;maven-antrun-plugin&lt;/artifactId&gt;
                                        &lt;versionRange&gt;[1.6,)&lt;/versionRange&gt;
                                        &lt;goals&gt;
                                            &lt;goal&gt;run&lt;/goal&gt;
                                        &lt;/goals&gt;
                                    &lt;/pluginExecutionFilter&gt;
                                    &lt;action&gt;
                                        &lt;execute&gt;
                                            &lt;runOnIncremental&gt;true&lt;/runOnIncremental&gt;
                                        &lt;/execute&gt;
                                    &lt;/action&gt;
                                &lt;/pluginExecution&gt;
                            &lt;/pluginExecutions&gt;
                        &lt;/lifecycleMappingMetadata&gt;
                    &lt;/configuration&gt;
                &lt;/plugin&gt;
            &lt;/plugins&gt;
        &lt;/pluginManagement&gt;
    &lt;/build&gt;
&lt;/profile&gt;