AEM Project Maven Build - Easy setup and steps to run commands via command line

We generally run maven commands every day to deploy the bundles or any other changes in the project to AEM server.

There are a lot of options to sync them automatically like using Brackets or AEM Server plugin configurations in IDE's etc.. These are very faster for deployments and changes to be reflected in AEM server

In case if you would like to do a complete build, we can run the same from Eclipse by using the run commands or via command prompt using the complete maven command but IDE's generally occupy lot of memory and might slow down sometimes.

I generally use Command prompt for the builds. I generally navigate to the repo folder and type the maven command "mvn clean install -PautoInstallPackage,adobe-public"

Note: adobe-public can be ignored if you already set the repo in the maven settings

I work on different projects and repositories every day and navigating to different folders and typing the same command (You can always copy and paste the command from a clipboard or notes - but you have to navigate b/w windows and multiple operations). To have a faster execution I tried using the batch scripts and worked very well. Hope this will be your use case too and might be useful.


Set Up:

  • Create a new folder "batch" in any directory. 
  • Create a new batch file lets say mvnc.bat
  • Copy the below snippet in your batch file and save it

cd C:\repository\common-repo-folder
echo Running build for Common code
mvn clean install -PautoInstallPackage

Now navigate to the batch folder and just execute mvnc and it should execute the maven command for you for that specific code repository, but again in this case you have to navigate to the batch folder. To avoid that we can add the batch to the path variable in our environment variables.


Add the batch folder to the path variable and save the changes

You can find different ways to add path variables based on your environment in below URL:


Once the path variable is set, you can run the command anywhere in the command prompt.

In my scenario we use Findbugs, if you would like to skip tests you can pass an argument while running the script.

Use the below script in the bat file.

@echo off

echo Switching to directory C:\repository\common-repo-folder

cd C:\repository\common-repo-folder

set skip=%1

set skipTests=

if "%skip%" == "skip" (
  set skipTests=-Dfindbugs.skip=true
  echo Skipping findbugs
)

echo Running build for Commons code Command: mvn clean install -PautoInstallPackage %skipTests%

mvn clean install -PautoInstallPackage %skipTests%


For Shell Scripts (to execute in Mac Terminal) use below Script:


echo Switching to directory /c/repository/common-rep-folder
cd /c/repository/common-rep-folder


skipTests=
if [ "$1" = "skip" ]
then 
 skipTests=-Dfindbugs.skip=true
 echo Skipping findbugs
fi

echo Running build for Commons code Command: mvn clean install -PautoInstallPackage $skipTests

mvn clean install -PautoInstallPackage $skipTests


Note: Change the path in the first line to your local path

Now run the command as "mvnc skip"

In the above command"skip" is an argument which is passed to your batch script to skip the tests. You can use your own argument and update the script accordingly

In the similar fashion create different batch scripts for different repositories and use simple commands to run the build.


Few samples:
mvnc --> runs the build for common code
mvnp --> runs the build for Project code
mvnp2 --> runs the build for project code

Commands are nothing but your batch file names.

Note: There is no restriction in the folder and file names. you can use your preferred naming conventions.

Shorter version of the setup in just 2 steps :) 


  • Create a batch folder and add it to the path variable 
  • Create a new mvnc.bat folder and add the below script and change the path in the first line to your local path
@echo off

echo Switching to directory C:\repository\common-repo-folder

cd C:\repository\common-repo-folder

set skip=%1

set skipTests=

if "%skip%" == "skip" (
  set skipTests=-Dfindbugs.skip=true
  echo Skipping findbugs
)

echo Running build for Commons code Command: mvn clean install -PautoInstallPackage %skipTests%

mvn clean install -PautoInstallPackage %skipTests%

That's it, you are all set to use your short commands


You can add more steps like checking the branch name or status and pull the latest code every time before running the build like 

git branch
git pull









Comments

Popular posts from this blog

Delete DAM Asset using Workflow

Groovy Console Integration and executing scripts in AEM