2929import static org .apache .jackrabbit .vault .packaging .PackageProperties .NAME_DEPENDENCIES ;
3030import static org .apache .jackrabbit .vault .packaging .PackageProperties .NAME_NAME ;
3131import static org .apache .jackrabbit .vault .packaging .PackageProperties .NAME_PACKAGE_TYPE ;
32+ import static org .apache .jackrabbit .vault .packaging .PackageProperties .NAME_VERSION ;
3233
3334import java .io .File ;
3435import java .io .FileOutputStream ;
5758import org .apache .jackrabbit .vault .packaging .DependencyUtil ;
5859import org .apache .jackrabbit .vault .packaging .PackageType ;
5960import org .apache .jackrabbit .vault .packaging .VersionRange ;
61+ import org .apache .maven .artifact .ArtifactUtils ;
6062import org .apache .maven .plugin .logging .Log ;
6163import org .apache .maven .plugin .logging .SystemStreamLog ;
6264import org .jetbrains .annotations .NotNull ;
6769import io .wcm .devops .conga .plugins .aem .maven .AutoDependenciesMode ;
6870import io .wcm .devops .conga .plugins .aem .maven .BuildOutputTimestamp ;
6971import io .wcm .devops .conga .plugins .aem .maven .PackageTypeValidation ;
72+ import io .wcm .devops .conga .plugins .aem .maven .PackageVersionMode ;
7073import io .wcm .devops .conga .plugins .aem .maven .RunModeOptimization ;
7174import io .wcm .devops .conga .plugins .aem .maven .model .BundleFile ;
7275import io .wcm .devops .conga .plugins .aem .maven .model .ContentPackageFile ;
@@ -105,6 +108,7 @@ public final class AllPackageBuilder {
105108 private AutoDependenciesMode autoDependenciesMode = AutoDependenciesMode .OFF ;
106109 private RunModeOptimization runModeOptimization = RunModeOptimization .OFF ;
107110 private PackageTypeValidation packageTypeValidation = PackageTypeValidation .STRICT ;
111+ private PackageVersionMode packageVersionMode = PackageVersionMode .DEFAULT ;
108112 private Log log ;
109113 private BuildOutputTimestamp buildOutputTimestamp ;
110114
@@ -113,6 +117,7 @@ public final class AllPackageBuilder {
113117 PackageType .APPLICATION .name ().toLowerCase (),
114118 PackageType .CONTAINER .name ().toLowerCase (),
115119 PackageType .CONTENT .name ().toLowerCase ());
120+ private static final String VERSION_SUFFIX_SEPARATOR = "-" ;
116121
117122 private final List <ContentPackageFileSet > contentPackageFileSets = new ArrayList <>();
118123 private final List <BundleFileSet > bundleFileSets = new ArrayList <>();
@@ -156,6 +161,15 @@ public AllPackageBuilder packageTypeValidation(PackageTypeValidation value) {
156161 return this ;
157162 }
158163
164+ /**
165+ * @param value How to handle versions of packages and sub-packages inside "all" package.
166+ * @return this
167+ */
168+ public AllPackageBuilder packageVersionMode (PackageVersionMode value ) {
169+ this .packageVersionMode = value ;
170+ return this ;
171+ }
172+
159173 /**
160174 * @param value Maven logger
161175 * @return this
@@ -457,8 +471,8 @@ private static String buildRootPath(String groupName, String packageName) {
457471
458472 /**
459473 * Generate suffix for instance and environment run modes.
460- * @param file Content package
461- * @return Package path
474+ * @param file File
475+ * @return Suffix string
462476 */
463477 private static String buildRunModeSuffix (InstallableFile file , String environmentRunMode ) {
464478 StringBuilder runModeSuffix = new StringBuilder ();
@@ -474,6 +488,26 @@ else if (isOnlyPublish(file)) {
474488 return runModeSuffix .toString ();
475489 }
476490
491+ /**
492+ * Generate suffix for versions of content packages.
493+ * @param pkg Content package
494+ * @param ignoreSnapshot Do not build version suffix for SNAPSHOT versions
495+ * @return Suffix string
496+ */
497+ private String buildVersionSuffix (ContentPackageFile pkg , boolean ignoreSnapshot ) {
498+ StringBuilder versionSuffix = new StringBuilder ();
499+
500+ if (this .packageVersionMode == PackageVersionMode .RELEASE_SUFFIX_VERSION
501+ && (!ArtifactUtils .isSnapshot (pkg .getVersion ()) || !ignoreSnapshot )
502+ && this .version != null ) {
503+ versionSuffix .append (VERSION_SUFFIX_SEPARATOR )
504+ // replace dots with underlines in version suffix to avoid confusion with main version number
505+ .append (StringUtils .replace (this .version , "." , "_" ));
506+ }
507+
508+ return versionSuffix .toString ();
509+ }
510+
477511 /**
478512 * Build path to be used for embedded package.
479513 * @param pkg Package
@@ -492,7 +526,11 @@ private String buildPackagePath(ContentPackageFile pkg, String rootPath, String
492526
493527 String versionSuffix = "" ;
494528 String packageVersion = pkg .getVersion ();
495- if (packageVersion != null && pkg .getFile ().getName ().contains (packageVersion )) {
529+ String packageVersionWithoutSuffix = packageVersion ;
530+ if (this .packageVersionMode == PackageVersionMode .RELEASE_SUFFIX_VERSION && this .version != null ) {
531+ packageVersionWithoutSuffix = StringUtils .removeEnd (packageVersion , buildVersionSuffix (pkg , false ));
532+ }
533+ if (packageVersion != null && pkg .getFile ().getName ().contains (packageVersionWithoutSuffix )) {
496534 versionSuffix = "-" + packageVersion ;
497535 }
498536 String fileName = pkg .getName () + versionSuffix
@@ -554,6 +592,7 @@ private List<TemporaryContentPackageFile> processContentPackage(ContentPackageFi
554592 FileVaultProperties fileVaultProps = new FileVaultProperties (is );
555593 Properties props = fileVaultProps .getProperties ();
556594 addSuffixToPackageName (props , pkg , environmentRunMode );
595+ addSuffixToVersion (props , pkg );
557596
558597 // update package dependencies
559598 ContentPackageFile dependencyFile = previousPkg ;
@@ -562,7 +601,7 @@ private List<TemporaryContentPackageFile> processContentPackage(ContentPackageFi
562601 }
563602 updateDependencies (props , dependencyFile , environmentRunMode , allPackagesFromFileSets );
564603
565- // if package type is missing package properties, put in the type defined in model
604+ // if package type is missing in package properties, put in the type defined in model
566605 String packageType = pkg .getPackageType ();
567606 if (props .get (NAME_PACKAGE_TYPE ) == null && packageType != null ) {
568607 props .put (NAME_PACKAGE_TYPE , packageType );
@@ -583,7 +622,7 @@ else if (StringUtils.equals(FilenameUtils.getExtension(zipInEntry.getName()), "z
583622 }
584623
585624 // check if contained ZIP file is really a content package
586- // then process it as well, remove if from the content package is was contained it
625+ // then process it as well, remove if from the content package is was contained in
587626 // and add it as "1st level package" to the all package
588627 TemporaryContentPackageFile tempSubPackage = new TemporaryContentPackageFile (tempSubPackageFile , pkg .getVariants ());
589628 if (packageTypeValidation == PackageTypeValidation .STRICT && !isValidPackageType (tempSubPackage )) {
@@ -644,7 +683,7 @@ else if (in.getLastModifiedTime() != null) {
644683 * @param dependencyFile Dependency package
645684 * @param allPackagesFromFileSets Set with all packages from all file sets as dependency instances
646685 */
647- private static void updateDependencies (Properties props , ContentPackageFile dependencyFile , String environmentRunMode ,
686+ private void updateDependencies (Properties props , ContentPackageFile dependencyFile , String environmentRunMode ,
648687 Set <Dependency > allPackagesFromFileSets ) {
649688 String [] existingDepsStrings = StringUtils .split (props .getProperty (NAME_DEPENDENCIES ), "," );
650689 Dependency [] existingDeps = null ;
@@ -658,9 +697,10 @@ private static void updateDependencies(Properties props, ContentPackageFile depe
658697 Dependency [] deps ;
659698 if (dependencyFile != null ) {
660699 String runModeSuffix = buildRunModeSuffix (dependencyFile , environmentRunMode );
700+ String dependencyVersion = dependencyFile .getVersion () + buildVersionSuffix (dependencyFile , true );
661701 Dependency newDependency = new Dependency (dependencyFile .getGroup (),
662702 dependencyFile .getName () + runModeSuffix ,
663- VersionRange .fromString (dependencyFile . getVersion () ));
703+ VersionRange .fromString (dependencyVersion ));
664704 deps = addDependency (existingDeps , newDependency );
665705 }
666706 else {
@@ -688,6 +728,15 @@ private static void addSuffixToPackageName(Properties props, ContentPackageFile
688728 props .put (NAME_NAME , packageName );
689729 }
690730
731+ private void addSuffixToVersion (Properties props , ContentPackageFile pkg ) {
732+ // package version
733+ if (StringUtils .isEmpty (pkg .getVersion ())) {
734+ return ;
735+ }
736+ String suffixedVersion = pkg .getVersion () + buildVersionSuffix (pkg , true );
737+ props .put (NAME_VERSION , suffixedVersion );
738+ }
739+
691740 /**
692741 * Removes existing references to packages contained in the list of packages to manage by this builder because
693742 * they are added new (and probably with a different package name) during processing.
0 commit comments