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.io.File;
22  import java.net.URI;
23  import java.util.List;
24  
25  import org.apache.maven.AbstractMavenLifecycleParticipant;
26  import org.apache.maven.MavenExecutionException;
27  import org.apache.maven.execution.MavenSession;
28  import org.codehaus.plexus.component.annotations.Component;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  import io.earcam.utilitarian.site.sitemap.Sitemaps;
33  
34  @Component(role = AbstractMavenLifecycleParticipant.class, hint = SitemapSubmissionMavenLifecycleParticipant.NAME, instantiationStrategy = "singleton")
35  public class SitemapSubmissionMavenLifecycleParticipant extends AbstractMavenLifecycleParticipant {
36  
37  	private static final Logger LOG = LoggerFactory.getLogger(SitemapIndexMavenLifecycleParticipant.class);
38  	static final String NAME = "sitemapsubmission";
39  	private static final String CATEGORY = '[' + NAME + ']';
40  
41  	static volatile boolean run = false;
42  	static URI baseUrl;
43  	static File targetDir;
44  	static List<String> hosts;
45  
46  
47  	@Override
48  	public void afterSessionEnd(MavenSession session) throws MavenExecutionException
49  	{
50  		if(session.getResult().hasExceptions()) {
51  			LOG.warn("{} extension: not running due to previous build errors", CATEGORY);
52  			return;
53  		}
54  		LOG.debug("{} extension: configured to run: {}", CATEGORY, run);
55  		if(run) {
56  			String responses = Sitemaps.submit(targetDir.toPath(), baseUrl, hosts);
57  			LOG.info("{} extension ran, responses: {}", CATEGORY, responses);
58  		}
59  	}
60  
61  
62  	static void shouldRun(URI baseUrl, File targetDir, List<String> hosts)
63  	{
64  		run = true;
65  		SitemapSubmissionMavenLifecycleParticipant.baseUrl = baseUrl;
66  		SitemapSubmissionMavenLifecycleParticipant.targetDir = targetDir;
67  		SitemapSubmissionMavenLifecycleParticipant.hosts = hosts;
68  	}
69  }