JavaRush /Java Blog /Random EN /Getting started with Maven Alfresco SDK part 2
CynepHy6
Level 34
Великий Новгород

Getting started with Maven Alfresco SDK part 2

Published in the Random EN group

Getting started with Alfresco Maven SDK

Jeff Potts, January, 2014 <= ​​Part 1
  • We work with your project in the IDE
  • Understanding the project structure
  • Creating a project adapted for Share access
  • General Project Dependencies
  • WAR target
  • Let's try this: Creating a Share project using an archetype
  • Understanding the Share Access directory structure
  • Launch of internal testing on Share
  • Dependency management
  • Other topics for independent study
  • Where to find more information

We work with your project in the IDE

This is not necessary, but many people prefer to work in an IDE when setting up Alfresco. Any IDE will do this, but one of the most popular is Eclipse so let's see how it works.
I'm using the Kepler version of Eclipse Java EE IDE for Web Developers. It comes with built-in Maven support.
To open the created project, do the following:
  • File, Import, Maven, Existing Maven Projects. Click Next.
    Getting started with Maven Alfresco SDK part 2 - 1
  • Specify the directory containing the someco-mvn-tutorial-repo directory. Eclipse will check this directory and show your project in the list of projects. Make sure the checkbox next to the project name is selected and click Next.
    Getting started with Maven Alfresco SDK part 2 - 2
  • Eclipse will show the Maven Plugin Panel settings. You can safely ignore it now. Click Finish.
    Getting started with Maven Alfresco SDK part 2 - 3
The project is now imported into your Eclipse workspace.
In the Markers panel you can see a list of Maven issues saying, "Plugin execution not covered by lifecycle configuration".
Maven has problems with plugins Maven has problems with plugins (set-version)
To fix:
  1. Right-click on the error and select "Quick Fix".
  2. Selecting "Permanently mark goal set-version in pom.xml as ignored in Eclipse build". Ready.
  3. Select the location where your POM is located (mine is com.someco : someco-mvn-tutorial-repo : 1.0-SNAPSHOT) and click OK.
After rebuilding the project, you may see one of the latest problems "Project configuration is not up-to-date with pom.xml". For correction:
  1. Right-click on the project and select Maven, Update Project.
  2. Make sure the project is selected and click OK.
Now Eclipse is probably happy and the only errors should be due to warnings about some unused Java imports.

Understanding the project structure

Your project's directory structure is a little nicer to explore in your IDE. Let's see what we have here.
  • pom.xml In the root of the project you see pom.xml. It tells Maven everything it needs to know about your project. Do you remember some settings you forgot when creating a project from a template? You can make changes in the settings here. For example, version 1.1.1 of the archetype allows you to work with Alfresco Community Edition version 4.2.e. If you want to work with a different version - just change a few properties and tell Maven to update them and it will take care of the rest.
  • src/main/java This is where you should create your own packages to organize your Java code. Things like custom action executer classes, custom behaviors, Java controllers for web scripts are here. If you don’t know what all this is, don’t worry, there are tutorials available here . These files will eventually be placed in a JAR. When AMP is installed in Alfresco WAR, the JAR will be inside WEB-INF/lib .
  • src/test Everything inside src/test is related to testing execution. The unit tests themselves are located in src/test/java . Any resources needed by these classes will be in src/test/resources. Inside src/test/properties/local you will see an alfresco-global.properties file . If you already know anything about Alfresco you know that it is used to configure the Alfresco server. In this case, this is only used to run the embedded server, for testing purposes.
  • src/main/amp Everything else is somewhere in this part of the project. The AMP structure is so well documented that I don't want to duplicate it here. I will just point out the main points:
    • The module.properties file tells Alfresco what it needs to know about AMP such as its ID, version, minimum and maximum version of Alfresco required to run AMP, and any other AMPs with the same dependencies.
    • The config/alfresco/module/someco-mvn-tutorial-repo directory is the heart of AMP. The place where you will place the Spring XML file configurations, the content model of the XML files, and the user interface configuration. As you'll see later, I prefer to separate subdirectories for each of these things.
    • If your module includes web scripts or workflows, they are not contained in the modules directory. Instead they are located in config/alfresco/extension/templates/webscripts and config/alfresco/extension/workflows .
    • Your module may include client resources that must be deployed to the root of the web application. They are located in src/main/amp/web in the css, jsp, scripts, and images directories.
You should check this entire project into source control. You may want to configure your version control client to ignore the target directory and the alf_data_dev directory.
Now you understand how to create a project for repository level settings. Let's take a look at the Share level. As part of this, I will show another option for creating a project without leaving Eclipse.

Creating a project adapted for Share access

Firstly, you must understand that the structure for a project that is created at the repository level is exactly the same as for a project for an adapted Share access layer. In the future of the Alfresco Maven SDK, there are two things that distinguish the Share project: The project's dependencies and the AMP WAR will be built into.

General Project Dependencies

Let's stop for a moment and talk about dependency management. At the moment, the implementation is such that by default the configured archetype in the project pom.xml uses the dependencies of the alfresco-repository artifact. Shared projects do not have this dependency. Fact is, many Shared projects don't use Java at all. Now let's edit pom.xml and remove the alfresco-repository dependencies. This will cause the demo components and associated test classes to generate an error when compiled. They can be removed.

WAR target

Another thing that distinguishes the Share project is the WAR in which AMP is deployed. Instead of alfresco WAR, here you need to deploy to Share WAR. This is configured in the alfresco.client.war property in your pom.xml. By default this is set to "alfresco". For a Share project, you need to replace it with “share”.

Let's try this: Creating a Share project using an archetype

Let's create a new project with General Settings. You can go to the command line and run the same archetype command as you ran before, minus the new artifactId, and then change the alfresco.client.war property to "share". If you're not using Eclipse, go ahead and do it now and skip the next section.
Another option is to configure Eclipse so that you can create a new Alfresco project using the Alfresco Maven SDK without leaving the IDE. Let's do that.
  1. File, New Maven Project.
  2. Select the directory that contains the "repo" project directory, click Next.
    Getting started with Maven Alfresco SDK part 2 - 4
  3. Click Configure and we can add the Alfresco directory to the list.
    Getting started with Maven Alfresco SDK part 2 - 5
  4. Click Add Remote Catalog
  5. Select "https://artifacts.alfresco.com/nexus/content/groups/public/archetype-catalog.xml" as Catalog File. Select the description "Alfresco Archetypes". Afterwards, click OK and OK again closer to the Preferences panel.
    Getting started with Maven Alfresco SDK part 2 - 6
  6. Now select "Alfresco Archetypes" in the directory and you will see a bunch of archetypes in the list.
  7. Select "org.alfresco.maven.archetype" and the same two archetypes will appear in the filter as at the beginning of the tutorial.
  8. Select alfresco-amp-archetype and click Next.
    Getting started with Maven Alfresco SDK part 2 - 7
  9. Selecting "com.someco" for groupId, "someco-mvn-tutorial-share" as artifactId, and change alfresco_target_amp_client_war to "share". Click Finish.
    Getting started with Maven Alfresco SDK part 2 - 8
Your Share project is now in the workspace. The next time you create a new project using the archetype, it will take a little less steps because you won't need to add a directory.

Understanding the Share Access directory structure

As I mentioned earlier, the structure of this project is exactly the same as our repo project. The only difference is that in the repo project such things as web scripts are located inside src/main/amp/config/alfresco/extension/templates/webscripts. In the General project they will be in src/main/amp/config/alfresco /web-extension/site-webscripts.

Launch of internal testing on Share

Often you will be working on both levels of settings at the same time. Your General level needs a repository for messages. One way to do this is to tell Maven to start your repo project using:
cd someco-mvn-tutorial-repo
mvn integration-test -Pamp-to-war
And then run your Share project using:
cd someco-mvn-tutorial-share
mvn integration-test -Pamp-to-war -Dmaven.tomcat.port=8081
Once both servers are up, you can open http://localhost:8081/share and login to test your module.

Dependency management

The cool thing about Apache Maven is that it manages your project's dependencies for you. All you need to do is tell Maven about them by configuring your pom.xml. By default, the Alfresco Maven SDK creates two dependencies for your project: alfresco-repository and junit.
As I wrote earlier, the Alfresco Share project does not depend on the Alfresco repository so for the someco-mvn-tutorial-share project, these dependencies can be removed. But what if I want to put some Java stuff into my Share project, such as Java-based web scripts? In this case, we need to add dependencies.
Web scripts can run at any level. If you write Java-based web scripts in your repo project, the class will be built because the dependencies of the alfresco-repository artifact in turn depend on the spring-webscripts artifact. You can see this if you open the Dependency Hierarchy in Eclipse in your pom.xml file:
Dependency Hierarchy in Eclipse
Dependency hierarchy in Eclipse Another option. You can see the dependency hierarchy by running:
mvn dependency:list
So, to add Java-based web scripts to your Share project, we need to add spring-webscripts as a dependency. You can do this by editing pom.xml like this:
<dependency>
    <groupId>org.springframework.extensions.surf</groupId>
    <artifactId>spring-webscripts</artifactId>
    <version>1.2.0-M14</version>
    <scope>provided</scope>
</dependency>
Now Java-based web scripts will be able to find their parent class - DeclarativeWebScript.
You may be wondering how you were to know that the DeclarativeWebScript class was included in the spring-webscripts artifact. Well, for example, having found it by going to http://artifacts.alfresco.com . You can search by class and you will be shown all the artifacts that contain it.

Other topics for independent study

You now know how to use the Alfresco Maven SDK to create projects and as your Alfresco repository and configure Alfresco Share. If you're new to Alfresco development, I hope you'll agree that it's really easy to put together a project to get started. If you've already gotten your feet wet with Alfresco but are still using the old SDKs, I hope this motivates you to switch to the new SDK to create your AMPs.
There are many topics that were not covered in this tutorial. I'll leave them for you to explore at your own discretion. Here are some of them:
  • The Alfresco Maven SDK supports dynamic class reloading when used in conjunction with a tool called JRebel .
  • This tutorial covers the AMP archetype. But Alfresco Maven SDK includes another archetype called All-in-One. This archetype gives you a complete Alfresco installation including SOLR.
  • Alfresco Maven SDK is supported in both Community Edition and Enterprise Edition. If you need assistance accessing artifacts in Enterprise Edition, contact Alfresco Support.

Where to find more information

<= Part 1
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION