View Javadoc
1   /*-
2    * #%L
3    * io.earcam.maven.plugin.netlify
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.netlify;
20  
21  import static org.apache.maven.plugins.annotations.InstantiationStrategy.SINGLETON;
22  import static org.apache.maven.plugins.annotations.LifecyclePhase.SITE_DEPLOY;
23  
24  import java.util.Objects;
25  
26  import org.apache.maven.plugin.AbstractMojo;
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugin.MojoFailureException;
29  import org.apache.maven.plugins.annotations.Mojo;
30  import org.apache.maven.plugins.annotations.Parameter;
31  import org.apache.maven.project.MavenProject;
32  import org.apache.maven.settings.Server;
33  import org.apache.maven.settings.Settings;
34  
35  /**
36   * <a href="https://netlify.com">Netlify</a> site deploy plugin
37   */
38  @Mojo(name = "upload", requiresProject = true, threadSafe = true, inheritByDefault = false, defaultPhase = SITE_DEPLOY, instantiationStrategy = SINGLETON)
39  public class NetlifyUploadMojo extends AbstractMojo {
40  
41  	@Parameter(defaultValue = "${settings}", readonly = true, required = true)
42  	Settings settings;
43  
44  	@Parameter(defaultValue = "${project}", readonly = true, required = true)
45  	MavenProject project;
46  
47  	/**
48  	 * Server ID, references a {@code <server>} in your {@code ~/.m2/settings.xml}, where your {@code <password>}
49  	 * is your Netlify access token (generate via Netlify ⇒ Account Settings ⇒ OAuth applications ⇒ Personal access
50  	 * tokens)
51  	 */
52  	// TODO default doesn't appear to be picked up
53  	// @Parameter(property = "deploy.netlify.server.id", defaultValue = "${project.distributionManagement.site.id}",
54  	// required = true)
55  	@Parameter(property = "deploy.netlify.server.id", required = true)
56  	String serverId;
57  
58  	/**
59  	 * The unique site name, used as a Netlify identifier and subdomain, e.g.
60  	 * given {@code <siteName>foo</siteName>}, then {@code URL == //foo.netlify.com}
61  	 */
62  	@Parameter(property = "deploy.netlify.site.name", required = false)
63  	String siteName;
64  
65  	/**
66  	 * Configure a custom domain, e.g. given {@code <siteName>foo.acme.com</siteName>} configure a
67  	 * DNS CNAME entry for your subdomain <b>foo.acme.com</b> to point to <b>acme-foo.netlify.com.</b>
68  	 * and set this to {@code <customDomain>foo.acme.com</customDomain>}
69  	 */
70  	@Parameter(property = "deploy.netlify.domain.custom", required = false)
71  	String customDomain;
72  
73  	/**
74  	 * Fails build if site (as identified by {@code <siteName>}) does not already exist and this
75  	 * property is set to {@code false}
76  	 */
77  	@Parameter(property = "deploy.netlify.site.create", defaultValue = "true", required = false)
78  	boolean createSiteIfNotExists;
79  
80  	/**
81  	 * Skip execution of this plugin
82  	 */
83  	@Parameter(property = "deploy.netlify.skip", defaultValue = "false")
84  	boolean skip;
85  
86  
87  	@Override
88  	public void execute() throws MojoExecutionException, MojoFailureException
89  	{
90  		if(skip) {
91  			return;
92  		}
93  		NetlifyUploadLifecycleParticipant.shouldRun();
94  		NetlifyUploadLifecycleParticipant.addPlugin(this);
95  	}
96  
97  
98  	String accessToken()
99  	{
100 		String token = server().getPassword();
101 		Objects.requireNonNull(token, "Did not find Netlify access token as the '" + serverId + "' server password in settings.xml");
102 		return token;
103 	}
104 
105 
106 	private Server server()
107 	{
108 		Server server = settings.getServer(serverId);
109 		Objects.requireNonNull(server, "server with id '" + serverId + "' not found in settings.xml");
110 		return server;
111 	}
112 
113 
114 	/**
115 	 * @deprecated unused
116 	 * @return {@code null}
117 	 */
118 	// Scheduled for delete on next major release, revapi seems overzealous
119 	// in applying semver rules to projects versioned less than 1.0.0
120 	@SuppressWarnings("squid:S1133")
121 	@Deprecated
122 	public MavenProject getProject()
123 	{
124 		throw new UnsupportedOperationException("deprecated; never used");
125 	}
126 }