2929
3030import org .apache .maven .plugin .MojoExecutionException ;
3131import org .apache .maven .plugin .MojoFailureException ;
32+ import org .apache .maven .plugins .annotations .Component ;
3233import org .apache .maven .plugins .annotations .Mojo ;
3334import org .apache .maven .plugins .annotations .Parameter ;
35+ import org .apache .maven .project .MavenProject ;
36+ import org .apache .maven .project .MavenProjectHelper ;
3437
3538import io .wcm .devops .conga .plugins .aem .maven .allpackage .AllPackageBuilder ;
3639import io .wcm .devops .conga .plugins .aem .maven .model .ContentPackageFile ;
3942/**
4043 * Builds an "all" content package dedicated for deployment via Adobe Cloud Manager
4144 * for the given environment and node(s).
45+ * <p>
46+ * By default, it builds one "all" package per environment and node without adding any Cloud Manager
47+ * environment-specific run mode suffixes to the folders. By defining a parameter <code>cloudManager.target</code>
48+ * (contains a list of string values) in the CONGA environment it is possible:
49+ * </p>
50+ * <ul>
51+ * <li>If it contains <code>none</code> no "all" package is build.</li>
52+ * <li>If set to one or multiple environment names (normally dev/stage/prod) one "all" package for each of
53+ * them is defined, and the environment name is added as runmode suffix to all config and install folders.</li>
54+ * </ul>
4255 */
4356@ Mojo (name = "cloudmanager-all-package" , threadSafe = true )
4457public final class CloudManagerAllPackageMojo extends AbstractCloudManagerMojo {
@@ -55,6 +68,19 @@ public final class CloudManagerAllPackageMojo extends AbstractCloudManagerMojo {
5568 @ Parameter (property = "conga.cloudManager.allPackage.group" , required = true )
5669 private String group ;
5770
71+ /**
72+ * Build one single content package for all environments and nodes.
73+ */
74+ @ Parameter (property = "conga.cloudManager.allPackage.singlePackage" , defaultValue = "false" )
75+ private boolean singlePackage ;
76+
77+ /**
78+ * Attach "all" content package(s) as artifacts to maven build lifecycle.
79+ * The given package name will be used as classifier.
80+ */
81+ @ Parameter (property = "conga.cloudManager.allPackage.attachArtifact" , defaultValue = "false" )
82+ private boolean attachArtifact ;
83+
5884 /**
5985 * Automatically generate dependencies between content packages based on file order in CONGA configuration.
6086 * <p>
@@ -100,6 +126,11 @@ public final class CloudManagerAllPackageMojo extends AbstractCloudManagerMojo {
100126 @ Parameter (property = "conga.cloudManager.allPackage.skip" , defaultValue = "false" )
101127 private boolean skip ;
102128
129+ @ Parameter (readonly = true , defaultValue = "${project}" )
130+ private MavenProject project ;
131+ @ Component
132+ private MavenProjectHelper projectHelper ;
133+
103134 private static final String CLOUDMANAGER_TARGET_NONE = "none" ;
104135
105136 @ Override
@@ -122,42 +153,102 @@ public void execute() throws MojoExecutionException, MojoFailureException {
122153 }
123154 }
124155
125- List <File > environmentDirs = getEnvironmentDir ();
126- for (File environmentDir : environmentDirs ) {
127- List <File > nodeDirs = getNodeDirs (environmentDir );
128- ModelParser modelParser = new ModelParser ();
129- for (File nodeDir : nodeDirs ) {
130- Set <String > cloudManagerTarget = modelParser .getCloudManagerTarget (nodeDir );
131- if (!cloudManagerTarget .contains (CLOUDMANAGER_TARGET_NONE )) {
132- buildAllPackage (environmentDir , nodeDir , cloudManagerTarget , modelParser );
133- }
134- }
156+ if (singlePackage ) {
157+ buildSingleAllPackage ();
158+ }
159+ else {
160+ buildAllPackagesPerEnvironmentNode ();
135161 }
136162 }
137163
138- private void buildAllPackage (File environmentDir , File nodeDir , Set <String > cloudManagerTarget ,
139- ModelParser modelParser ) throws MojoExecutionException {
140- String groupName = this .group ;
141- String packageName = environmentDir .getName () + "." + nodeDir .getName () + "." + this .name ;
164+ /**
165+ * Build an "all" package for each environment and node.
166+ */
167+ private void buildAllPackagesPerEnvironmentNode () throws MojoExecutionException , MojoFailureException {
168+ visitEnvironmentsNodes ((environmentDir , nodeDir , cloudManagerTarget , contentPackages ) -> {
169+ String packageName = environmentDir .getName () + "." + nodeDir .getName () + "." + this .name ;
170+ AllPackageBuilder builder = createBuilder (packageName );
171+
172+ try {
173+ builder .add (contentPackages , cloudManagerTarget );
174+ }
175+ catch (IllegalArgumentException ex ) {
176+ throw new MojoFailureException (ex .getMessage (), ex );
177+ }
142178
143- List <ContentPackageFile > contentPackages = modelParser .getContentPackagesForNode (nodeDir );
144- File targetFile = new File (getTargetDir (), packageName + ".zip" );
179+ buildAllPackage (builder );
180+ });
181+ }
145182
146- AllPackageBuilder builder = new AllPackageBuilder (targetFile , groupName , packageName )
183+ /**
184+ * Build a single "all" package containing packages from all environments and nodes.
185+ */
186+ private void buildSingleAllPackage () throws MojoExecutionException , MojoFailureException {
187+ String packageName = this .name ;
188+ AllPackageBuilder builder = createBuilder (packageName );
189+
190+ visitEnvironmentsNodes ((environmentDir , nodeDir , cloudManagerTarget , contentPackages ) -> {
191+ try {
192+ builder .add (contentPackages , cloudManagerTarget );
193+ }
194+ catch (IllegalArgumentException ex ) {
195+ throw new MojoFailureException (ex .getMessage (), ex );
196+ }
197+ });
198+
199+ buildAllPackage (builder );
200+ }
201+
202+ private AllPackageBuilder createBuilder (String packageName ) {
203+ String fileName ;
204+ if (attachArtifact ) {
205+ fileName = project .getArtifactId () + "." + packageName + "-" + project .getVersion () + ".zip" ;
206+ }
207+ else {
208+ fileName = packageName + ".zip" ;
209+ }
210+ File targetFile = new File (getTargetDir (), fileName );
211+ return new AllPackageBuilder (targetFile , this .group , packageName )
212+ .version (project .getVersion ())
147213 .autoDependenciesMode (this .autoDependenciesMode )
148214 .logger (getLog ());
215+ }
149216
217+ private void buildAllPackage (AllPackageBuilder builder ) throws MojoExecutionException {
150218 try {
151- if (builder .build (contentPackages , cloudManagerTarget , properties )) {
152- getLog ().info ("Generated " + getCanonicalPath (targetFile ));
219+ if (builder .build (properties )) {
220+ getLog ().info ("Generated " + getCanonicalPath (builder .getTargetFile ()));
221+ if (attachArtifact ) {
222+ projectHelper .attachArtifact (this .project , "zip" , builder .getPackageName (), builder .getTargetFile ());
223+ }
153224 }
154225 else {
155- getLog ().debug ("Skipped " + getCanonicalPath (targetFile ) + " - no valid package." );
226+ getLog ().debug ("Skipped " + getCanonicalPath (builder . getTargetFile () ) + " - no valid package." );
156227 }
157228 }
158229 catch (IOException ex ) {
159- throw new MojoExecutionException ("Unable to generate " + getCanonicalPath (targetFile ), ex );
230+ throw new MojoExecutionException ("Unable to generate " + getCanonicalPath (builder . getTargetFile () ), ex );
160231 }
161232 }
162233
234+ private void visitEnvironmentsNodes (EnvironmentNodeVisitor visitor ) throws MojoExecutionException , MojoFailureException {
235+ ModelParser modelParser = new ModelParser ();
236+ List <File > environmentDirs = getEnvironmentDir ();
237+ for (File environmentDir : environmentDirs ) {
238+ List <File > nodeDirs = getNodeDirs (environmentDir );
239+ for (File nodeDir : nodeDirs ) {
240+ Set <String > cloudManagerTarget = modelParser .getCloudManagerTarget (nodeDir );
241+ if (!cloudManagerTarget .contains (CLOUDMANAGER_TARGET_NONE )) {
242+ List <ContentPackageFile > contentPackages = modelParser .getContentPackagesForNode (nodeDir );
243+ visitor .visit (environmentDir , nodeDir , cloudManagerTarget , contentPackages );
244+ }
245+ }
246+ }
247+ }
248+
249+ interface EnvironmentNodeVisitor {
250+ void visit (File environmentDir , File nodeDir , Set <String > cloudManagerTarget ,
251+ List <ContentPackageFile > contentPackages ) throws MojoExecutionException , MojoFailureException ;
252+ }
253+
163254}
0 commit comments