May 13, 2020
Estimated Post Reading Time ~

Core Package

The core package is your java code, it is deployed into AEM as a bundle either as part of an application deploy or on it's own.

pom.xml
The following is an example of a standard core pom
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.sample</groupId>
<version>1.0.0-SNAPSHOT</version>
<artifactId>base</artifactId>
<relativePath>../pom.xml</relativePath>
<artifactId>base</artifactId>
</parent>
<artifactId>core</artifactId>
<packaging>bundle</packaging>
<name>Sample - Core</name>
<description>Core bundle for Sample</description>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Build-Time>${timestamp}</Build-Time>
<Build-Number>${buildNumber}</Build-Number>
<Build-Branch>${scmBranch}</Build-Branch>
<Import-Package>javax.inject;version=0.0.0,*</Import-Package>
<Export-Package> com.sample.core.*</Export-Package>
<Embed-Dependency>*;scope=compile|runtime;optional=false</Embed-Dependency>
<Sling-Model-Packages> com.sample.core</Sling-Model-Packages>
</instructions>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</version>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration&g
t; <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacocout</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>false</skip>
<argLine>${surefireArgLine}</argLine>
<includes>
<!-- Include unit tests within integration-test phase. -->
<include>**/JUnitTest*.java</include>
</includes>
<!-- Show 100% of the lines from the stack trace (doesn't work) -->
<trimStackTrace>false</trimStackTrace>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- AEM Dependencies --> <dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr</artifactId>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.annotations</artifactId>
</dependency>
<dependency>
<groupId>javax.jcr</groupId>
<artifactId>jcr</artifactId>
</dependency>
<dependency>
<groupId>com.adobe.aem</groupId>
<artifactId>uber-jar</artifactId>
<classifier>apis</classifier>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.models.api</artifactId>
</dependency> <dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.models.impl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-atinject_1.0_spec</artifactId>
</dependency>
<!-- Web Dependencies -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</dependency>
<!-- Logging Dependencies -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<!-- Commons Dependencies -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
</dependency>
<!-- Test Dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
</dependency>
</dependencies>
</project>

I won't go into to much detail on the pom as it is pretty standard. You will see I am using jacoco to measure code coverage using sonarqube otherwise the plugins of interest are

<plugin>
<groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> <configuration>
<instructions>
<Import-Package>javax.inject;version=0.0.0,*</Import-Package> <Export-Package> com.sample.core.*</Export-Package> <Embed-Dependency>*;scope=compile|runtime;optional=false</Embed-Dependency> <Sling-Model-Packages> com.sample.core</Sling-Model-Packages>
</instructions> <archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries> <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
</configuration>
</plugin>

In the maven-bundle-plugin plugin, I define a few extra features in its configuration
  • instructions
    • Import-Package
      • Here we are telling the plugin to add javax.inject of any version as a dependency and all other declared packages
    • Export-Package
      • Here we are telling the plugin to add all of our packages to the manifest
    • Embed-Dependency
      • Here we are telling the plugin to embed all dependencies of type compile or runtime inside the built jar file
    • Sling-Model-Packages
      • Here we are telling the plugin to declare that any java file under com.sample.core can be a sling model
  • archive
    • manifest
      • addDefaultImplementationEntries
        • Here we are telling the plugin to write the Implementation version number details into the manifest
      • addDefaultSpecificationEntries
        • Here we are telling the plugin to write the Specification version number details into the manifest

Code

One thing you will see in most of the code examples in the following pages, although there are annotations that will help you configure AEM to use your components, in java side of the code, once you have the right structure, you are pretty much open to write your any java code you want, one note is that not all libraries will work in AEM so you will have to play around with which will and won't, and it is a good idea to see if AEM is pre-bundled with a version of a library.


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.