View Javadoc
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  
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Properties;
26  
27  import org.apache.maven.artifact.resolver.ResolutionErrorHandler;
28  import org.apache.maven.execution.MavenSession;
29  import org.apache.maven.plugin.MojoExecutionException;
30  import org.apache.maven.plugin.MojoFailureException;
31  import org.apache.maven.plugins.annotations.Component;
32  import org.apache.maven.plugins.annotations.LifecyclePhase;
33  import org.apache.maven.plugins.annotations.Mojo;
34  import org.apache.maven.plugins.annotations.Parameter;
35  import org.apache.maven.plugins.annotations.ResolutionScope;
36  import org.apache.maven.project.MavenProject;
37  import org.eclipse.aether.RepositorySystem;
38  import org.eclipse.aether.RepositorySystemSession;
39  import org.eclipse.aether.repository.RemoteRepository;
40  import org.sonatype.plexus.build.incremental.BuildContext;
41  
42  import io.wcm.devops.conga.generator.Generator;
43  import io.wcm.devops.conga.generator.GeneratorOptions;
44  import io.wcm.devops.conga.generator.spi.context.PluginContextOptions;
45  import io.wcm.devops.conga.generator.util.PluginManager;
46  import io.wcm.devops.conga.generator.util.PluginManagerImpl;
47  import io.wcm.devops.conga.tooling.maven.plugin.util.ClassLoaderUtil;
48  import io.wcm.devops.conga.tooling.maven.plugin.util.MavenContext;
49  import io.wcm.devops.conga.tooling.maven.plugin.util.VersionInfoUtil;
50  
51  /**
52   * Generates configuration using CONGA generator.
53   */
54  @Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, requiresProject = true, threadSafe = true,
55      requiresDependencyResolution = ResolutionScope.COMPILE)
56  public class GenerateMojo extends AbstractCongaMojo {
57  
58    /**
59     * Selected environments to generate.
60     */
61    @Parameter(property = "conga.environments")
62    private String[] environments;
63  
64    /**
65     * Selected nodes to generate.
66     */
67    @Parameter(property = "conga.nodes")
68    private String[] nodes;
69  
70    /**
71     * Delete folders of environments before generating the new files.
72     */
73    @Parameter(defaultValue = "false")
74    private boolean deleteBeforeGenerate;
75  
76    /**
77     * Is it allowed to create symlinks instead of copying files if they are local files e.g. from Maven Repository.
78     */
79    @Parameter(defaultValue = "true")
80    private boolean allowSymlinks;
81  
82    /**
83     * Plugin keys (groupId:artifactId) of additional Maven plugins of the current project's POM
84     * to be included in the model export version information.
85     */
86    @Parameter(defaultValue = "io.wcm.maven.plugins:wcmio-content-package-maven-plugin")
87    private String[] versionInfoAdditionalPlugins;
88  
89    @Parameter(property = "project", required = true, readonly = true)
90    private MavenProject project;
91  
92    @Component
93    private org.apache.maven.repository.RepositorySystem repositorySystem;
94    @Component
95    private ResolutionErrorHandler resolutionErrorHandler;
96    @Component
97    private RepositorySystem repoSystem;
98    @Component
99    private BuildContext buildContext;
100   @Parameter(defaultValue = "${repositorySystemSession}", readonly = true)
101   private RepositorySystemSession repoSession;
102   @Parameter(defaultValue = "${project.remoteProjectRepositories}", readonly = true)
103   private List<RemoteRepository> remoteRepos;
104   @Parameter(defaultValue = "${session}", readonly = true, required = false)
105   private MavenSession session;
106 
107   @Override
108   public void execute() throws MojoExecutionException, MojoFailureException {
109 
110     MavenContext mavenContext = new MavenContext()
111         .project(project)
112         .session(session)
113         .setRepositorySystem(repositorySystem)
114         .resolutionErrorHandler(resolutionErrorHandler)
115         .buildContext(buildContext)
116         .log(getLog())
117         .repoSystem(repoSystem)
118         .repoSession(repoSession)
119         .remoteRepos(remoteRepos)
120         .artifactTypeMappings(getArtifactTypeMappings());
121 
122     PluginManager pluginManager = new PluginManagerImpl();
123 
124     PluginContextOptions pluginContextOptions = new PluginContextOptions()
125         .pluginManager(pluginManager)
126         .valueProviderConfig(getValueProviderConfig())
127         .genericPluginConfig(getPluginConfig())
128         .containerContext(mavenContext)
129         .logger(new MavenSlf4jLogFacade(getLog()));
130 
131     GeneratorOptions options = new GeneratorOptions()
132         .baseDir(project.getBasedir())
133         .roleDir(getRoleDir())
134         .templateDir(getTemplateDir())
135         .environmentDir(getEnvironmentDir())
136         .destDir(getTargetDir())
137         .deleteBeforeGenerate(deleteBeforeGenerate)
138         .version(project.getVersion())
139         .setAllowSymlinks(allowSymlinks)
140         .modelExport(getModelExport())
141         .valueProviderConfig(getValueProviderConfig())
142         .genericPluginConfig(getPluginConfig())
143         .containerContext(mavenContext)
144         .containerClasspathUrls(ClassLoaderUtil.getMavenProjectClasspathUrls(project))
145         .pluginManager(pluginManager)
146         .dependencyVersionBuilder(new DependencyVersionBuilder(pluginContextOptions))
147         .containerVersionInfo(buildContainerVersionInfo())
148         .logger(new MavenSlf4jLogFacade(getLog()));
149 
150     Generator generator = new Generator(options);
151     generator.generate(environments, nodes);
152   }
153 
154   /**
155    * Build version information about CONGA Maven plugin and CONGA plugins, plus
156    * additional plugin versions as defined in plugin configuration. This version
157    * information is included in the model export.
158    * @return Version information
159    */
160   private Map<String, String> buildContainerVersionInfo() {
161     Map<String, String> versionInfo = new HashMap<>();
162 
163     Properties pluginProps = VersionInfoUtil.getVersionInfoProperties(project);
164     for (Map.Entry<Object, Object> entry : pluginProps.entrySet()) {
165       versionInfo.put(entry.getKey().toString(), entry.getValue().toString());
166     }
167 
168     for (String pluginKey : versionInfoAdditionalPlugins) {
169       String pluginVersion = VersionInfoUtil.getPluginVersionFromPluginManagement(pluginKey, project);
170       if (pluginVersion != null) {
171         versionInfo.put(pluginKey, pluginVersion);
172       }
173     }
174 
175     return versionInfo;
176   }
177 
178 }