1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }