2323import static io .wcm .devops .conga .plugins .aem .maven .allpackage .RunModeUtil .RUNMODE_AUTHOR ;
2424import static io .wcm .devops .conga .plugins .aem .maven .allpackage .RunModeUtil .RUNMODE_PUBLISH ;
2525import static org .apache .jackrabbit .vault .packaging .PackageProperties .NAME_DEPENDENCIES ;
26+ import static org .apache .jackrabbit .vault .packaging .PackageProperties .NAME_NAME ;
2627
2728import java .io .File ;
2829import java .io .FileOutputStream ;
4041import java .util .zip .ZipOutputStream ;
4142
4243import org .apache .commons .io .FileUtils ;
44+ import org .apache .commons .io .FilenameUtils ;
4345import org .apache .commons .io .IOUtils ;
4446import org .apache .commons .lang3 .StringUtils ;
4547import org .apache .jackrabbit .vault .packaging .Dependency ;
@@ -128,12 +130,12 @@ public boolean build(List<ContentPackageFile> contentPackages,
128130 Set <String > cloudManagerTarget , Map <String , String > properties ) throws IOException {
129131
130132 // collect list of cloud manager environment run modes
131- List <String > environmentRunmodes = new ArrayList <>();
133+ List <String > environmentRunModes = new ArrayList <>();
132134 if (cloudManagerTarget .isEmpty ()) {
133- environmentRunmodes .add (RUNMODE_DEFAULT );
135+ environmentRunModes .add (RUNMODE_DEFAULT );
134136 }
135137 else {
136- environmentRunmodes .addAll (cloudManagerTarget );
138+ environmentRunModes .addAll (cloudManagerTarget );
137139 }
138140
139141 // generate warnings for each invalid content packages that is skipped
@@ -179,10 +181,10 @@ public boolean build(List<ContentPackageFile> contentPackages,
179181 // build content package
180182 // if auto dependencies is active: build separate "dependency chains" between mutable and immutable packages
181183 try (ContentPackage contentPackage = builder .build (targetFile )) {
182- for (String environmentRunmode : environmentRunmodes ) {
184+ for (String environmentRunMode : environmentRunModes ) {
183185 List <ContentPackageFile > previousPackages = new ArrayList <>();
184186 for (ContentPackageFile pkg : validContentPackages ) {
185- String path = buildPackagePath (pkg , rootPath , environmentRunmode );
187+ String path = buildPackagePath (pkg , rootPath , environmentRunMode );
186188
187189 // get last previous package
188190 // if autoDependenciesSeparateMutable active only that of the same mutability type
@@ -191,14 +193,9 @@ public boolean build(List<ContentPackageFile> contentPackages,
191193 .reduce ((first , second ) -> second )
192194 .orElse (null );
193195
194- if (autoDependencies && previousPkg != null ) {
195- // wire previous package in package dependency
196- addFileWithDependency (contentPackage , path , pkg , previousPkg );
197- }
198- else {
199- // add package file directly
200- contentPackage .addFile (path , pkg .getFile ());
201- }
196+ // set package name, wire previous package in package dependency
197+ addFileWithDependency (contentPackage , path , pkg , autoDependencies ? previousPkg : null , environmentRunMode );
198+
202199 previousPackages .add (pkg );
203200 }
204201 }
@@ -239,23 +236,43 @@ private static String buildRootPath(String groupName, String packageName) {
239236 }
240237
241238 /**
242- * Build path to be used for embedded package .
239+ * Generate suffix for instance and environment run modes .
243240 * @param pkg Package
244- * @param rootPath Root path
245241 * @return Package path
246242 */
247- private static String buildPackagePath (ContentPackageFile pkg , String rootPath , String environmentRunmode ) {
243+ private static String buildRunModeSuffix (ContentPackageFile pkg , String environmentRunMode ) {
248244 StringBuilder runModeSuffix = new StringBuilder ();
249245 if (RunModeUtil .isOnlyAuthor (pkg )) {
250246 runModeSuffix .append ("." ).append (RUNMODE_AUTHOR );
251247 }
252248 else if (RunModeUtil .isOnlyPublish (pkg )) {
253249 runModeSuffix .append ("." ).append (RUNMODE_PUBLISH );
254250 }
255- if (!StringUtils .equals (environmentRunmode , RUNMODE_DEFAULT )) {
256- runModeSuffix .append ("." ).append (environmentRunmode );
251+ if (!StringUtils .equals (environmentRunMode , RUNMODE_DEFAULT )) {
252+ runModeSuffix .append ("." ).append (environmentRunMode );
253+ }
254+ return runModeSuffix .toString ();
255+ }
256+
257+ /**
258+ * Build path to be used for embedded package.
259+ * @param pkg Package
260+ * @param rootPath Root path
261+ * @return Package path
262+ */
263+ private static String buildPackagePath (ContentPackageFile pkg , String rootPath , String environmentRunMode ) {
264+ String runModeSuffix = buildRunModeSuffix (pkg , environmentRunMode );
265+
266+ // add run mode suffix to both install folder path and package file name
267+ String path = rootPath + "/" + pkg .getPackageType () + "/install" + runModeSuffix ;
268+
269+ String versionSuffix = "" ;
270+ if (pkg .getVersion () != null && pkg .getFile ().getName ().contains (pkg .getVersion ())) {
271+ versionSuffix = "-" + pkg .getVersion ();
257272 }
258- return rootPath + "/" + pkg .getPackageType () + "/install" + runModeSuffix .toString () + "/" + pkg .getFile ().getName ();
273+ String fileName = pkg .getName () + runModeSuffix + versionSuffix
274+ + "." + FilenameUtils .getExtension (pkg .getFile ().getName ());
275+ return path + "/" + fileName ;
259276 }
260277
261278 /**
@@ -265,10 +282,11 @@ else if (RunModeUtil.isOnlyPublish(pkg)) {
265282 * @param path Path in target content package
266283 * @param pkg Package to add
267284 * @param previousPkg Previous package to get dependency information from
285+ * @param environmentRunMode Environment run mode
268286 * @throws IOException I/O error
269287 */
270288 private static void addFileWithDependency (ContentPackage contentPackage , String path ,
271- ContentPackageFile pkg , ContentPackageFile previousPkg ) throws IOException {
289+ ContentPackageFile pkg , ContentPackageFile previousPkg , String environmentRunMode ) throws IOException {
272290
273291 // create temp zip file to create rewritten copy of package
274292 File tempFile = File .createTempFile ("pkg" , ".zip" );
@@ -290,7 +308,10 @@ private static void addFileWithDependency(ContentPackage contentPackage, String
290308 try (InputStream is = zipFileIn .getInputStream (zipInEntry )) {
291309 Properties props = new Properties ();
292310 props .loadFromXML (is );
293- updateDependencies (props , previousPkg );
311+ addSuffixToPackageName (props , pkg , environmentRunMode );
312+ if (previousPkg != null ) {
313+ updateDependencies (props , previousPkg , environmentRunMode );
314+ }
294315 props .storeToXML (zipOut , null );
295316 }
296317 }
@@ -318,14 +339,16 @@ private static void addFileWithDependency(ContentPackage contentPackage, String
318339 * @param props Properties
319340 * @param dependencyFile Dependency package
320341 */
321- private static void updateDependencies (Properties props , ContentPackageFile dependencyFile ) {
342+ private static void updateDependencies (Properties props , ContentPackageFile dependencyFile , String environmentRunMode ) {
322343 String [] existingDepsStrings = StringUtils .split (props .getProperty (NAME_DEPENDENCIES ), "," );
323344 Dependency [] existingDeps = null ;
324345 if (existingDepsStrings != null && existingDepsStrings .length > 0 ) {
325346 existingDeps = Dependency .fromString (existingDepsStrings );
326347 }
327348
328- Dependency newDependency = new Dependency (dependencyFile .getGroup (), dependencyFile .getName (),
349+ String runModeSuffix = buildRunModeSuffix (dependencyFile , environmentRunMode );
350+ Dependency newDependency = new Dependency (dependencyFile .getGroup (),
351+ dependencyFile .getName () + runModeSuffix ,
329352 VersionRange .fromString (dependencyFile .getVersion ()));
330353 Dependency [] deps ;
331354 if (existingDeps != null ) {
@@ -338,4 +361,10 @@ private static void updateDependencies(Properties props, ContentPackageFile depe
338361 props .put (NAME_DEPENDENCIES , Dependency .toString (deps ));
339362 }
340363
364+ private static void addSuffixToPackageName (Properties props , ContentPackageFile pkg , String environmentRunMode ) {
365+ String runModeSuffix = buildRunModeSuffix (pkg , environmentRunMode );
366+ String packageName = props .getProperty (NAME_NAME ) + runModeSuffix ;
367+ props .put (NAME_NAME , packageName );
368+ }
369+
341370}
0 commit comments