GenerateMojo.java

  1. /*
  2.  * #%L
  3.  * wcm.io
  4.  * %%
  5.  * Copyright (C) 2015 wcm.io
  6.  * %%
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *      http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  * #L%
  19.  */
  20. package io.wcm.devops.conga.tooling.maven.plugin;

  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Properties;

  25. import javax.inject.Inject;

  26. import org.apache.maven.artifact.resolver.ResolutionErrorHandler;
  27. import org.apache.maven.execution.MavenSession;
  28. import org.apache.maven.plugin.MojoExecutionException;
  29. import org.apache.maven.plugin.MojoFailureException;
  30. import org.apache.maven.plugins.annotations.LifecyclePhase;
  31. import org.apache.maven.plugins.annotations.Mojo;
  32. import org.apache.maven.plugins.annotations.Parameter;
  33. import org.apache.maven.plugins.annotations.ResolutionScope;
  34. import org.apache.maven.project.MavenProject;
  35. import org.eclipse.aether.RepositorySystem;
  36. import org.eclipse.aether.RepositorySystemSession;
  37. import org.eclipse.aether.repository.RemoteRepository;
  38. import org.sonatype.plexus.build.incremental.BuildContext;

  39. import io.wcm.devops.conga.generator.Generator;
  40. import io.wcm.devops.conga.generator.GeneratorOptions;
  41. import io.wcm.devops.conga.generator.spi.context.PluginContextOptions;
  42. import io.wcm.devops.conga.generator.util.PluginManager;
  43. import io.wcm.devops.conga.generator.util.PluginManagerImpl;
  44. import io.wcm.devops.conga.tooling.maven.plugin.util.ClassLoaderUtil;
  45. import io.wcm.devops.conga.tooling.maven.plugin.util.MavenContext;
  46. import io.wcm.devops.conga.tooling.maven.plugin.util.VersionInfoUtil;

  47. /**
  48.  * Generates configuration using CONGA generator.
  49.  */
  50. @Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, requiresProject = true, threadSafe = true,
  51.     requiresDependencyResolution = ResolutionScope.COMPILE)
  52. public class GenerateMojo extends AbstractCongaMojo {

  53.   /**
  54.    * Selected environments to generate.
  55.    */
  56.   @Parameter(property = "conga.environments")
  57.   private String[] environments;

  58.   /**
  59.    * Selected nodes to generate.
  60.    */
  61.   @Parameter(property = "conga.nodes")
  62.   private String[] nodes;

  63.   /**
  64.    * Delete folders of environments before generating the new files.
  65.    */
  66.   @Parameter(defaultValue = "false")
  67.   private boolean deleteBeforeGenerate;

  68.   /**
  69.    * Is it allowed to create symlinks instead of copying files if they are local files e.g. from Maven Repository.
  70.    */
  71.   @Parameter(defaultValue = "true")
  72.   private boolean allowSymlinks;

  73.   /**
  74.    * Plugin keys (groupId:artifactId) of additional Maven plugins of the current project's POM
  75.    * to be included in the model export version information.
  76.    */
  77.   @Parameter(defaultValue = "io.wcm.maven.plugins:wcmio-content-package-maven-plugin")
  78.   private String[] versionInfoAdditionalPlugins;

  79.   @Parameter(property = "project", required = true, readonly = true)
  80.   private MavenProject project;

  81.   @Inject
  82.   private org.apache.maven.repository.RepositorySystem repositorySystem;
  83.   @Inject
  84.   private ResolutionErrorHandler resolutionErrorHandler;
  85.   @Inject
  86.   private RepositorySystem repoSystem;
  87.   @Inject
  88.   private BuildContext buildContext;
  89.   @Parameter(defaultValue = "${repositorySystemSession}", readonly = true)
  90.   private RepositorySystemSession repoSession;
  91.   @Parameter(defaultValue = "${project.remoteProjectRepositories}", readonly = true)
  92.   private List<RemoteRepository> remoteRepos;
  93.   @Parameter(defaultValue = "${session}", readonly = true, required = false)
  94.   private MavenSession session;

  95.   @Override
  96.   public void execute() throws MojoExecutionException, MojoFailureException {

  97.     MavenContext mavenContext = new MavenContext()
  98.         .project(project)
  99.         .session(session)
  100.         .setRepositorySystem(repositorySystem)
  101.         .resolutionErrorHandler(resolutionErrorHandler)
  102.         .buildContext(buildContext)
  103.         .log(getLog())
  104.         .repoSystem(repoSystem)
  105.         .repoSession(repoSession)
  106.         .remoteRepos(remoteRepos)
  107.         .artifactTypeMappings(getArtifactTypeMappings());

  108.     PluginManager pluginManager = new PluginManagerImpl();

  109.     PluginContextOptions pluginContextOptions = new PluginContextOptions()
  110.         .pluginManager(pluginManager)
  111.         .valueProviderConfig(getValueProviderConfig())
  112.         .genericPluginConfig(getPluginConfig())
  113.         .containerContext(mavenContext)
  114.         .logger(new MavenSlf4jLogFacade(getLog()));

  115.     GeneratorOptions options = new GeneratorOptions()
  116.         .baseDir(project.getBasedir())
  117.         .roleDir(getRoleDir())
  118.         .templateDir(getTemplateDir())
  119.         .environmentDir(getEnvironmentDir())
  120.         .destDir(getTargetDir())
  121.         .deleteBeforeGenerate(deleteBeforeGenerate)
  122.         .version(project.getVersion())
  123.         .setAllowSymlinks(allowSymlinks)
  124.         .modelExport(getModelExport())
  125.         .valueProviderConfig(getValueProviderConfig())
  126.         .genericPluginConfig(getPluginConfig())
  127.         .containerContext(mavenContext)
  128.         .containerClasspathUrls(ClassLoaderUtil.getMavenProjectClasspathUrls(project))
  129.         .pluginManager(pluginManager)
  130.         .dependencyVersionBuilder(new DependencyVersionBuilder(pluginContextOptions))
  131.         .containerVersionInfo(buildContainerVersionInfo())
  132.         .logger(new MavenSlf4jLogFacade(getLog()));

  133.     Generator generator = new Generator(options);
  134.     generator.generate(environments, nodes);
  135.   }

  136.   /**
  137.    * Build version information about CONGA Maven plugin and CONGA plugins, plus
  138.    * additional plugin versions as defined in plugin configuration. This version
  139.    * information is included in the model export.
  140.    * @return Version information
  141.    */
  142.   private Map<String, String> buildContainerVersionInfo() {
  143.     Map<String, String> versionInfo = new HashMap<>();

  144.     Properties pluginProps = VersionInfoUtil.getVersionInfoProperties(project);
  145.     for (Map.Entry<Object, Object> entry : pluginProps.entrySet()) {
  146.       versionInfo.put(entry.getKey().toString(), entry.getValue().toString());
  147.     }

  148.     for (String pluginKey : versionInfoAdditionalPlugins) {
  149.       String pluginVersion = VersionInfoUtil.getPluginVersionFromPluginManagement(pluginKey, project);
  150.       if (pluginVersion != null) {
  151.         versionInfo.put(pluginKey, pluginVersion);
  152.       }
  153.     }

  154.     return versionInfo;
  155.   }

  156. }