View Javadoc
1   /*-
2    * #%L
3    * io.earcam.maven.plugin.sitemap
4    * %%
5    * Copyright (C) 2017 earcam
6    * %%
7    * SPDX-License-Identifier: (BSD-3-Clause OR EPL-1.0 OR Apache-2.0 OR MIT)
8    *
9    * You <b>must</b> choose to accept, in full - any individual or combination of
10   * the following licenses:
11   * <ul>
12   * 	<li><a href="https://opensource.org/licenses/BSD-3-Clause">BSD-3-Clause</a></li>
13   * 	<li><a href="https://www.eclipse.org/legal/epl-v10.html">EPL-1.0</a></li>
14   * 	<li><a href="https://www.apache.org/licenses/LICENSE-2.0">Apache-2.0</a></li>
15   * 	<li><a href="https://opensource.org/licenses/MIT">MIT</a></li>
16   * </ul>
17   * #L%
18   */
19  package io.earcam.maven.plugin.sitemap;
20  
21  import java.nio.file.Files;
22  import java.nio.file.Path;
23  import java.nio.file.Paths;
24  import java.util.stream.Stream;
25  
26  import org.apache.maven.AbstractMavenLifecycleParticipant;
27  import org.apache.maven.MavenExecutionException;
28  import org.apache.maven.execution.MavenSession;
29  import org.apache.maven.model.Model;
30  import org.apache.maven.model.Reporting;
31  import org.apache.maven.project.MavenProject;
32  import org.codehaus.plexus.component.annotations.Component;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  import io.earcam.unexceptional.Exceptional;
37  import io.earcam.utilitarian.site.sitemap.Sitemaps;
38  
39  @Component(role = AbstractMavenLifecycleParticipant.class, hint = SitemapIndexMavenLifecycleParticipant.NAME, instantiationStrategy = "singleton")
40  public class SitemapIndexMavenLifecycleParticipant extends AbstractMavenLifecycleParticipant {
41  
42  	private static final Logger LOG = LoggerFactory.getLogger(SitemapIndexMavenLifecycleParticipant.class);
43  	static final String NAME = "sitemapindex";
44  	private static final String CATEGORY = '[' + NAME + ']';
45  
46  	private static volatile boolean shouldRun = false;
47  
48  
49  	@Override
50  	public void afterSessionEnd(MavenSession session) throws MavenExecutionException
51  	{
52  		if(session.getResult().hasExceptions()) {
53  			LOG.warn("{} extension: not running due to previous build errors", CATEGORY);
54  			return;
55  		}
56  		LOG.debug("{} extension: configured to run: {}", CATEGORY, shouldRun);
57  		if(shouldRun) {
58  			MavenProject project = session.getTopLevelProject();
59  
60  			Stream<Path> targetDirs = project.getCollectedProjects().stream()
61  					.map(MavenProject::getModel)
62  					.map(Model::getReporting)
63  					.map(Reporting::getOutputDirectory)
64  					.map(Paths::get);
65  
66  			Path targetDir = Paths.get(project.getModel().getReporting().getOutputDirectory());
67  
68  			Path indexFile = Exceptional.apply(Sitemaps::index, targetDir, targetDirs);
69  
70  			Exceptional.apply(Files::lines, indexFile)
71  					.forEach(f -> LOG.debug("{} Created {}", CATEGORY, f));
72  		}
73  	}
74  
75  
76  	static void shouldRun()
77  	{
78  		shouldRun = true;
79  	}
80  }