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