Featured

Wednesday 30 December 2015

Creating a maven project in cq5

Here we are Setting up only project structure for CQ/AEM project using maven and development of AEM/CQ project with eclipse.

Configuring AEM Maven Archetype for Eclipse can be done by installing and adding AEM plugin to the existing eclipse IDE,

The manual setup for the Maven is as follows


PREREQUISITES TO SETUP

     To set up a development environment based on Maven the following software must be installed before starting:

  •      Apache Maven 3.0.4 or higher
  •      Oracle Java SE 1.5.x or higher
  •      CQ5.5, AEM 5.6 or higher

Now follow these defined steps,

Create the initial structure from Adobe's multimodule-content-package-archetype

In your shell, go to a directory where your project is supposed to be created.

In the following example, line breaks are only to make reading simpler. Everything goes on one line.

$ mvn archetype:generate -DarchetypeRepository=http://repo.adobe.com/nexus/content/groups/public/ -DarchetypeGroupId=com.day.jcr.vault -DarchetypeArtifactId=multimodule-content-package-archetype -DarchetypeVersion=1.0.2 -DgroupId=my-group-id -DartifactId=myproject -Dversion=1.0-SNAPSHOT -Dpackage=com.mycompany.myproject -DappsFolderName=myproject -DartifactName="My Project" -DcqVersion="5.6.1" -DpackageGroup="My Company"

Confirm the provided values by entering "Y" and hitting Return when Maven asks.

Confirm properties configuration:

  • groupId: my-group-id
  • artifactId: myproject
  • version: 1.0-SNAPSHOT
  • package: com.mycompany.myproject
  • appsFolderName: myproject
  • artifactName: My Project
  • cqVersion: 5.6.1
  • packageGroup: My Company
  •  Y: :
Now ,

Maven has created a directory for the project. Change into it:

       # cd myproject

Now build the project and install it into your AEM instance.

       # mvn -PautoInstallPackage install

By the above procedure, it should create different XML files for the project and content.

The archetype has created the following project definition for us.

myproject/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!-- ====================================================================== -->
    <!-- P A R E N T P R O J E C T D E S C R I P T I O N -->
    <!-- ====================================================================== -->

    <groupId>my-group-id</groupId>
    <artifactId>myproject</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>My Project - Reactor Project</name>
    <description>Maven Multimodule project for My Project.</description>

    <prerequisites>
        <maven>3.0.2</maven>
    </prerequisites>

    <!-- ====================================================================== -->
    <!-- P R O P E R T I E S -->
    <!-- ====================================================================== -->
    <properties>
        <crx.host>localhost</crx.host>
        <crx.port>4502</crx.port>
        <crx.username>admin</crx.username>
        <crx.password>admin</crx.password>
        <publish.crx.host>localhost</publish.crx.host>
        <publish.crx.port>4503</publish.crx.port>
        <publish.crx.username>admin</publish.crx.username>
        <publish.crx.password>admin</publish.crx.password>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.osgi</groupId>
                <artifactId>org.osgi.core</artifactId>
                <version>4.2.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.osgi</groupId>
                <artifactId>org.osgi.compendium</artifactId>
                <version>4.2.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.felix</groupId>
                <artifactId>org.apache.felix.scr.annotations</artifactId>
                <version>1.6.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>biz.aQute</groupId>
                <artifactId>bndlib</artifactId>
                <version>1.43.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.5.10</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <version>2.5</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.jcr</groupId>
                <artifactId>jcr</artifactId>
                <version>2.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.sling</groupId>
                <artifactId>org.apache.sling.api</artifactId>
                <version>2.2.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.sling</groupId>
                <artifactId>org.apache.sling.jcr.api</artifactId>
                <version>2.1.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.8.1</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!--
        Inclusion of repositories in POMs is controversial, to say the least.
        It would be best if you proxied the Adobe repository using a Maven
        Repository Manager. Once you do that, remove these sections.
     -->
    <repositories>
        <repository>
            <id>adobe</id>
            <name>Adobe Public Repository</name>
            <url>http://repo.adobe.com/nexus/content/groups/public/</url>
            <layout>default</layout>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>adobe</id>
            <name>Adobe Public Repository</name>
            <url>http://repo.adobe.com/nexus/content/groups/public/</url>
            <layout>default</layout>
        </pluginRepository>
    </pluginRepositories>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.felix</groupId>
                    <artifactId>maven-scr-plugin</artifactId>
                    <version>1.7.4</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.felix</groupId>
                    <artifactId>maven-bundle-plugin</artifactId>
                    <version>2.3.7</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>2.6</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.7</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.4</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.14.1</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-javadoc-plugin</artifactId>
                    <version>2.9</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.sling</groupId>
                    <artifactId>maven-sling-plugin</artifactId>
                    <version>2.1.0</version>
                    <configuration>
                        <username>${crx.username}</username>
                        <password>${crx.password}</password>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>com.day.jcr.vault</groupId>
                    <artifactId>content-package-maven-plugin</artifactId>
                    <version>0.0.20</version>
                    <extensions>true</extensions>
                    <configuration>
                        <failOnError>true</failOnError>
                        <username>${crx.username}</username>
                        <password>${crx.password}</password>
                    </configuration>
                </plugin>
                <!--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.felix
                                        </groupId>
                                        <artifactId>
                                            maven-scr-plugin
                                        </artifactId>
                                        <versionRange>
                                            [1.0.0,)
                                        </versionRange>
                                        <goals>
                                            <goal>scr</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore/>
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <profiles>
        <profile>
            <id>autoInstallBundle</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.sling</groupId>
                        <artifactId>maven-sling-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>install-bundle</id>
                                <goals>
                                    <goal>install</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
  <modules>
    <module>bundle</module>
    <module>content</module>
  </modules>
</project>

THE BUNDLE MODULE

The bundle module is set up to contain OSGi Services required in your project.

Note that depending on the complexity of your project, you may want to refine this setup and create separate modules for individual areas of your project. For example, it is common to have a separate bundle for the project specific tag library, and to separate infrastrutural components (loggers, filters, login modules, etc) from business components (workflow steps, live action factories, content builders and transport handlers, etc).

The following structure has been created by the Maven archetype.

+-- pom.xml
+-- src
    +-- main
    ¦   +-- java
    ¦       +-- com
    ¦           +-- mycompany
    ¦               +-- myproject
    ¦                   +-- HelloService.java
    ¦                   +-- impl
    ¦                   ¦   +-- HelloServiceImpl.java
    ¦                   ¦   +-- filters
    ¦                   ¦       +-- LoggingFilter.java
    ¦                   +-- package-info.java
    +-- test
        +-- java
            +-- com
                +-- mycompany
                    +-- myproject
                        +-- SimpleUnitTest.java


Below is the Bundle POM file generated by the Maven archetype.

myproject/bundle/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd ">
    <modelVersion>4.0.0</modelVersion>
    <!-- ====================================================================== -->
    <!-- P A R E N T P R O J E C T D E S C R I P T I O N -->
    <!-- ====================================================================== -->
    <parent>
        <groupId>my-group-id</groupId>
        <artifactId>myproject</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <!-- ====================================================================== -->
    <!-- P R O J E C T D E S C R I P T I O N -->
    <!-- ====================================================================== -->

    <artifactId>myproject-bundle</artifactId>
    <packaging>bundle</packaging>
    <name>My Project Bundle</name>

    <dependencies>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.compendium</artifactId>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>org.apache.felix.scr.annotations</artifactId>
        </dependency>
        <dependency>
            <groupId>biz.aQute</groupId>
            <artifactId>bndlib</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.jcr</groupId>
            <artifactId>jcr</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.sling</groupId>
            <artifactId>org.apache.sling.api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.sling</groupId>
            <artifactId>org.apache.sling.jcr.api</artifactId>
        </dependency>
    </dependencies>

    <!-- ====================================================================== -->
    <!-- B U I L D D E F I N I T I O N -->
    <!-- ====================================================================== -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-scr-plugin</artifactId>
                <executions>
                    <execution>
                        <id>generate-scr-descriptor</id>
                        <goals>
                            <goal>scr</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Bundle-SymbolicName>my-group-id.myproject-bundle</Bundle-SymbolicName>
                    </instructions>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.sling</groupId>
                <artifactId>maven-sling-plugin</artifactId>
                <configuration>
                    <slingUrl>http://${crx.host}:${crx.port}/apps/myproject/install</slingUrl>
                    <usePut>true</usePut>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                 <configuration>
                    <excludePackageNames>
                        *.impl
                    </excludePackageNames>
                 </configuration>
            </plugin>
        </plugins>
    </build>
</project>

THE CONTENT MODULE

The content module is set up to build an AEM content package which combines

components
templates
configurations
sample content
the OSGi bundle that is built by the bundle module
The archetype provides the structure below. As you can see, it deviates a little from a standard Maven Java module as there are no Java sources in the context of a content module. Accordingly, the module contains a directory src/main/content.

+-- pom.xml
+-- src
    +-- main
        +-- content
            +-- META-INF
            ¦   +-- vault
            ¦       +-- config.xml
            ¦       +-- definition
            ¦       ¦   +-- .content.xml
            ¦       +-- filter.xml
            ¦       +-- nodetypes.cnd
            ¦       +-- properties.xml
            +-- jcr_root
                +-- apps
                    +-- myproject
                        +-- config
                        ¦   +-- put-your-configs-here.txt
                        +-- install
                            +-- .vltignore
                            +-- bundle-will-go-here.txt


The POM generated by the archetype has a couple of noteworthy points.

The content-package-maven-plugin is configured to embed the OSGi bundle at /apps/myproject/install, (where Apache Sling's JCR Installer Provider will pick it up and install it with the OSGi Installer)
It is also configured to install the package at the host and port configured in the parent pom
By default, the content-package-maven-plugin uses the project's resources definition.
Depending on your other development tools, you may want to exclude more files, such as .git, .project etc.
A profile autoInstallPackage is defined, which binds the content-package-maven-plugin's install goal to Maven's install phase. This profile allows you to automatically install the package to AEM after it has been built.
A profile autoInstallPackagePublish is defined, which does the same for a publish server

myproject/content/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!-- ====================================================================== -->
    <!-- P A R E N T P R O J E C T D E S C R I P T I O N -->
    <!-- ====================================================================== -->
    <parent>
        <groupId>my-group-id</groupId>
        <artifactId>myproject</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <!-- ====================================================================== -->
    <!-- P R O J E C T D E S C R I P T I O N -->
    <!-- ====================================================================== -->

    <artifactId>myproject-content</artifactId>
    <packaging>content-package</packaging>
    <name>My Project Package</name>

    <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>myproject-bundle</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/content/jcr_root</directory>
                <filtering>false</filtering>
                <excludes>
                    <exclude>**/.vlt</exclude>
                    <exclude>**/.vltignore</exclude>
                </excludes>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <includeEmptyDirs>true</includeEmptyDirs>
                </configuration>
            </plugin>

            <plugin>
                <groupId>com.day.jcr.vault</groupId>
                <artifactId>content-package-maven-plugin</artifactId>
                <extensions>true</extensions>
                <configuration>
                    <group>My Company</group>
                    <filterSource>src/main/content/META-INF/vault/filter.xml</filterSource>
                    <embeddeds>
                        <embedded>
                            <groupId>${project.groupId}</groupId>
                            <artifactId>myproject-bundle</artifactId>
                            <target>/apps/myproject/install</target>
                        </embedded>
                    </embeddeds>
                    <targetURL>http://${crx.host}:${crx.port}/crx/packmgr/service.jsp</targetURL>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <profiles>
        <profile>
            <id>autoInstallPackage</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>com.day.jcr.vault</groupId>
                        <artifactId>content-package-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>install-content-package</id>
                                <phase>install</phase>
                                <goals>
                                    <goal>install</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>autoInstallPackagePublish</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>com.day.jcr.vault</groupId>
                        <artifactId>content-package-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>install-content-package-publish</id>
                                <phase>install</phase>
                                <goals>
                                    <goal>install</goal>
                                </goals>
                                <configuration>
                                    <targetURL>http://${publish.crx.host}:${publish.crx.port}/crx/packmgr/service.jsp</targetURL>
                                    <username>${publish.crx.username}</username>
                                    <password>${publish.crx.password}</password>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

Typical Steps in a Development Cycle

CONTENT PACKAGE DEVELOPMENT TASKS

The typical development cycle for your content package is to

  • make changes to the project, either on the filesystem or in the repository
  • run mvn -PautoInstallPackage clean install to deploy the package to the repository
  • Making Changes to the Project
You can make changes to the content/ project in two ways:

  • Make changes below content/src/main/content/jcr_root and deploy them to the repository
  • Make changes in the repository and use the vlt tool to sync them back to the filesystem

mvn -PautoInstallPackage clean install

Running mvn -PautoInstallPackage clean install will

  • build the content package according to the definition in myproject/content/pom.xml and save that package to content/target/myproject-content-1.0-SNAPSHOT.zip
  • install the package into your local maven repository (that is what Maven's "install" phase always does)
  • activate the autoInstallPackage profile of the content module, which binds the content-package-maven-plugin:install goal to the install phase and so installs the package into AEM through the Package Manager's HTTP API. See the documentation of the Content Package Maven Plugin for details.

BUNDLE DEVELOPMENT TASKS

The bundle module follows a standard Java setup, and so does the process:

  • make changes to your Java sources
  • run mvn -PautoInstallBundle install in the bundle/ directory to compile and deploy the bundle
And Further,

VLT is used to keep the file system and the repository synchronized. The basic steps for setting this up are
  • install the package to your repository
  • cd to content/src/main/content/jcr_root
  • run vlt co --force http://localhost:4502/ -- this will check out the content from the repository into your filesystem and set up vlt to keep track
  • run vlt sync install -- this will install the vlt sync service into your repository if not already available
  • run vlt sync register -- this will register this project's directory with the vlt sync service
We can setup appropriate vlt commands to keep the system up to date. For this you need to configure the XML file accordingly.


Hope it helps :)
Thanks,
SonyCharan


No comments: