diff options
author | Jon Marius Venstad <jvenstad@yahoo-inc.com> | 2019-05-07 11:50:37 +0200 |
---|---|---|
committer | Jon Marius Venstad <jvenstad@yahoo-inc.com> | 2019-05-07 12:02:22 +0200 |
commit | 01155d5d23f2dbc4589b9860df26ebf29aaf90fc (patch) | |
tree | a69dd6e49a95490a8b0ceb594bee2b4e54ff26fb /vespa-maven-plugin | |
parent | 272f8185a355f003ebc9255ee456e3c41523893b (diff) |
Factor out common Mojo code into AbstractVespaMojo
Diffstat (limited to 'vespa-maven-plugin')
-rw-r--r-- | vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/AbstractVespaMojo.java | 78 | ||||
-rw-r--r-- | vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/SubmitMojo.java | 57 |
2 files changed, 84 insertions, 51 deletions
diff --git a/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/AbstractVespaMojo.java b/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/AbstractVespaMojo.java new file mode 100644 index 00000000000..30113c83e92 --- /dev/null +++ b/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/AbstractVespaMojo.java @@ -0,0 +1,78 @@ +package ai.vespa.hosted.plugin; + +import ai.vespa.hosted.api.ControllerHttpClient; +import com.yahoo.config.provision.ApplicationId; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.project.MavenProject; + +import java.net.URI; +import java.nio.file.Path; +import java.nio.file.Paths; + +/** + * Base class for hosted Vespa plugin mojos. + * + * @author jonmv + */ +public abstract class AbstractVespaMojo extends AbstractMojo { + + @Parameter(defaultValue = "${project}", readonly = true) + protected MavenProject project; + + @Parameter(property = "endpoint", defaultValue = "https://api.vespa.corp.yahoo.com:4443") // TODO jvenstad: Change default + protected String endpointUri; + + @Parameter(property = "tenant") + protected String tenant; + + @Parameter(property = "application") + protected String application; + + @Parameter(property = "instance") + protected String instance; + + @Parameter(property = "privateKeyFile", required = true) + protected String privateKeyFile; + + @Parameter(property = "certificateFile") + protected String certificateFile; + + // Fields set up as part of setup(). + protected ApplicationId id; + protected ControllerHttpClient controller; + + @Override + public final void execute() { + setup(); + doExecute(); + } + + /** Override this in subclasses, instead of {@link #execute()}. */ + protected abstract void doExecute(); + + protected void setup() { + tenant = firstNonBlank(tenant, project.getProperties().getProperty("tenant")); + application = firstNonBlank(application, project.getProperties().getProperty("application")); + instance = firstNonBlank(instance, project.getProperties().getProperty("instance")); + id = ApplicationId.from(tenant, application, instance); + + controller = certificateFile == null + ? ControllerHttpClient.withSignatureKey(URI.create(endpointUri), Paths.get(privateKeyFile), id) + : ControllerHttpClient.withKeyAndCertificate(URI.create(endpointUri), Paths.get(privateKeyFile), Paths.get(certificateFile)); + } + + protected String projectPathOf(String first, String... rest) { + return project.getBasedir().toPath().resolve(Path.of(first, rest)).toString(); + } + + /** Returns the first of the given strings which is non-null and non-blank, or throws IllegalArgumentException. */ + protected static String firstNonBlank(String... values) { + for (String value : values) + if (value != null && ! value.isBlank()) + return value; + + throw new IllegalArgumentException("No valid value given"); + } + +} diff --git a/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/SubmitMojo.java b/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/SubmitMojo.java index 6f39c29963c..9bebe3c32cd 100644 --- a/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/SubmitMojo.java +++ b/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/SubmitMojo.java @@ -19,22 +19,7 @@ import java.nio.file.Paths; * @author jonmv */ @Mojo(name = "submit") -public class SubmitMojo extends AbstractMojo { - - @Parameter(defaultValue = "${project}", readonly = true) - private MavenProject project; - - @Parameter(property = "endpoint", defaultValue = "https://api.vespa.corp.yahoo.com:4443") // TODO jvenstad: Change default - private String endpointUri; - - @Parameter(property = "tenant") - private String tenant; - - @Parameter(property = "application") - private String application; - - @Parameter(property = "instance") - private String instance; +public class SubmitMojo extends AbstractVespaMojo { @Parameter(property = "applicationZip") private String applicationZip; @@ -42,12 +27,6 @@ public class SubmitMojo extends AbstractMojo { @Parameter(property = "applicationTestZip") private String applicationTestZip; - @Parameter(property = "privateKeyFile", required = true) - private String privateKeyFile; - - @Parameter(property = "certificateFile") - private String certificateFile; - @Parameter(property = "authorEmail", required = true) private String authorEmail; @@ -61,38 +40,14 @@ public class SubmitMojo extends AbstractMojo { private String commit; @Override - public void execute() { - setup(); - ApplicationId id = ApplicationId.from(tenant, application, instance); - ControllerHttpClient controller = certificateFile == null - ? ControllerHttpClient.withSignatureKey(URI.create(endpointUri), Paths.get(privateKeyFile), id) - : ControllerHttpClient.withKeyAndCertificate(URI.create(endpointUri), Paths.get(privateKeyFile), Paths.get(certificateFile)); - - Submission submission = new Submission(repository, branch, commit, authorEmail, - Paths.get(applicationZip), Paths.get(applicationTestZip)); - - System.out.println(controller.submit(submission, id.tenant(), id.application())); - } - - private void setup() { - tenant = firstNonBlank(tenant, project.getProperties().getProperty("tenant")); - application = firstNonBlank(application, project.getProperties().getProperty("application")); - instance = firstNonBlank(instance, project.getProperties().getProperty("instance")); + public void doExecute() { applicationZip = firstNonBlank(applicationZip, projectPathOf("target", "application.zip")); applicationTestZip = firstNonBlank(applicationTestZip, projectPathOf("target", "application-test.zip")); - } - - private String projectPathOf(String first, String... rest) { - return project.getBasedir().toPath().resolve(Path.of(first, rest)).toString(); - } - - /** Returns the first of the given strings which is non-null and non-blank, or throws IllegalArgumentException. */ - private static String firstNonBlank(String... values) { - for (String value : values) - if (value != null && ! value.isBlank()) - return value; + Submission submission = new Submission(repository, branch, commit, authorEmail, + Paths.get(applicationZip), + Paths.get(applicationTestZip)); - throw new IllegalArgumentException("No valid value given"); + System.out.println(controller.submit(submission, id.tenant(), id.application())); } } |