Wednesday 28 January 2015

How to Make Suggestions Available in JSP Files in CQ5/AEM6 using JSPC plugin

As a newbie in AEM one of the challenge that I was faced was the suggestions availability inside AEM. Sightly is emerging to replace the JSP files of components but for the time being if we are working on the JSPs and it takes much time to code the jsp file, this blog will help in enabling the suggestions inside jsp files using maven-jspc-plugin. To make this work I used the below project structure.

/apps/
     |--->aemproject/
                    |--->components
                    |--->config
                    |--->install
                    |--->templates
                    |--->init.jsp
                    |--->global.jsp[which is included inside init.jsp]

Now if we have <%@include file="/apps/aemproject/init.jsp" %> inside our template/component then we will have suggestions enabled for their respective JSPs.

Open your content module's pom.xml, and then add the dependencies in pom.xml as per your global.jsp. i.e
       <%@page session="false" import="javax.jcr.*,
        org.apache.sling.api.resource.Resource,
        org.apache.sling.api.resource.ValueMap,
        com.day.cq.commons.inherit.InheritanceValueMap,
        com.day.cq.wcm.commons.WCMUtils,
        com.day.cq.wcm.api.Page,
        com.day.cq.wcm.api.NameConstants,
        com.day.cq.wcm.api.PageManager,
        com.day.cq.wcm.api.designer.Designer,
        com.day.cq.wcm.api.designer.Design,
        com.day.cq.wcm.api.designer.Style,
        com.day.cq.wcm.api.components.ComponentContext,
        com.day.cq.wcm.api.components.EditContext"
        %>
       
Now add the maven-jspc-plugin inside the same file. Below is the snapshot of pom.xml need to add.

        <pluginManagement>
            <plugins>
                <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.apache.sling</groupId>
                                        <artifactId>maven-jspc-plugin</artifactId>
                                        <versionRange>[2.0.6,)</versionRange>
                                        <goals>
                                            <goal>jspc</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore/>
                                    </action>
                                </pluginExecution>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.apache.maven.plugins</groupId>
                                        <artifactId>maven-clean-plugin</artifactId>
                                        <versionRange>[2.4.1,)</versionRange>
                                        <goals>
                                            <goal>clean</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore/>
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>

            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/jsps-to-compile</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>src/main/content/jcr_root</directory>
                                    <excludes>
                                        <exclude>libs/**</exclude>
                                    </excludes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.sling</groupId>
                <artifactId>maven-jspc-plugin</artifactId>
                <version>2.0.6</version>
                <executions>
                    <execution>
                        <id>compile-jsp</id>
                        <goals>
                            <goal>jspc</goal>
                        </goals>
                        <configuration>
                            <jasperClassDebugInfo>false</jasperClassDebugInfo>
                            <sourceDirectory>${project.build.directory}/jsps-to-compile</sourceDirectory>
                            <outputDirectory>${project.build.directory}/ignoredjspc</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <executions>
                    <execution>
                        <id>remove-compiled-jsps</id>
                        <goals>
                            <goal>clean</goal>
                        </goals>
                        <phase>process-classes</phase>
                        <configuration>
                            <excludeDefaultDirectories>true</excludeDefaultDirectories>
                            <filesets>
                                <fileset>
                                    <directory>${project.build.directory}/jsps-to-compile</directory>
                                    <directory>${project.build.directory}/ignoredjspc</directory>
                                </fileset>
                            </filesets>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.day.jcr.vault</groupId>
                <artifactId>content-package-maven-plugin</artifactId>
                <extensions>true</extensions>
                <configuration>
                    <group>AEM Training</group>
                    <filterSource>src/main/content/META-INF/vault/filter.xml</filterSource>
                    <embeddeds>
                        <embedded>
                            <groupId>${project.groupId}</groupId>
                            <artifactId>Training-bundle</artifactId>
                            <target>/apps/aemproject/install</target>
                        </embedded>
                    </embeddeds>
                    <targetURL>http://${crx.host}:${crx.port}/crx/packmgr/service.jsp</targetURL>
                </configuration>
            </plugin>
        </plugins>
        </pluginManagement>

Build codebase -- mvn clean install -P yourbuildprofile

You can see the suggestion available via ctrl+space inside your jsp file.



Hope this will help.

Thanks