aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-maven-plugin
diff options
context:
space:
mode:
authorMartin Polden <mpolden@mpolden.no>2022-03-16 12:52:57 +0100
committerMartin Polden <mpolden@mpolden.no>2022-03-16 13:01:13 +0100
commite9425943c648abaaaf877e4d621f7fa22925777a (patch)
treef9e1910d22ec6cb00338bc47567f971c43a00545 /vespa-maven-plugin
parent49e06b444d93d5ffde1601b0025eed436ea029cd (diff)
Include allowMajor when requesting compile version
Diffstat (limited to 'vespa-maven-plugin')
-rw-r--r--vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/CompileVersionMojo.java36
-rw-r--r--vespa-maven-plugin/src/test/java/ai/vespa/hosted/plugin/CompileVersionMojoTest.java28
-rw-r--r--vespa-maven-plugin/src/test/resources/deployment-with-major.xml6
-rw-r--r--vespa-maven-plugin/src/test/resources/deployment.xml6
4 files changed, 75 insertions, 1 deletions
diff --git a/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/CompileVersionMojo.java b/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/CompileVersionMojo.java
index eff1acfeb06..707da31b5ef 100644
--- a/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/CompileVersionMojo.java
+++ b/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/CompileVersionMojo.java
@@ -1,14 +1,18 @@
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.hosted.plugin;
+import com.yahoo.text.XML;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
+import org.w3c.dom.Element;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
+import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.util.OptionalInt;
/**
* Finds the Vespa version to compile against, for a hosted Vespa application.
@@ -24,11 +28,41 @@ public class CompileVersionMojo extends AbstractVespaMojo {
@Override
protected void doExecute() throws IOException {
Path output = Paths.get(outputFile).toAbsolutePath();
- String compileVersion = controller.compileVersion(id);
+ OptionalInt allowMajor = majorVersion(Paths.get("src/main/application/deployment.xml"));
+ String compileVersion = controller.compileVersion(id, allowMajor);
+ if (allowMajor.isPresent()) {
+ getLog().info("Allowing major version " + allowMajor.getAsInt() + ".");
+ }
getLog().info("Vespa version to compile against is '" + compileVersion + "'.");
getLog().info("Writing compile version to '" + output + "'.");
Files.createDirectories(output.getParent());
Files.writeString(output, compileVersion);
}
+ /** Returns the major version declared in given deploymentXml, if any */
+ static OptionalInt majorVersion(Path deploymentXml) {
+ try {
+ String xml = Files.readString(deploymentXml);
+ Element deploymentTag = XML.getDocument(xml).getDocumentElement();
+ if (deploymentTag == null) return OptionalInt.empty();
+ String allowMajor = deploymentTag.getAttribute("major-version");
+ if (allowMajor.isEmpty()) return OptionalInt.empty();
+ return OptionalInt.of(parseMajor(allowMajor));
+ } catch (NoSuchFileException ignored) {
+ return OptionalInt.empty();
+ } catch (IOException e) {
+ throw new UncheckedIOException(e);
+ }
+ }
+
+ private static int parseMajor(String s) {
+ try {
+ int major = Integer.parseInt(s);
+ if (major < 1) throw new IllegalArgumentException("Major version must be positive, got " + major);
+ return major;
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException("Invalid major version '" + s + "'", e);
+ }
+ }
+
}
diff --git a/vespa-maven-plugin/src/test/java/ai/vespa/hosted/plugin/CompileVersionMojoTest.java b/vespa-maven-plugin/src/test/java/ai/vespa/hosted/plugin/CompileVersionMojoTest.java
new file mode 100644
index 00000000000..ebb91934470
--- /dev/null
+++ b/vespa-maven-plugin/src/test/java/ai/vespa/hosted/plugin/CompileVersionMojoTest.java
@@ -0,0 +1,28 @@
+package ai.vespa.hosted.plugin;
+
+import org.junit.jupiter.api.Test;
+
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.OptionalInt;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * @author mpolden
+ */
+class CompileVersionMojoTest {
+
+ @Test
+ public void allow_major() {
+ assertMajorVersion(OptionalInt.empty(), Paths.get("non-existent-deployment.xml"));
+ assertMajorVersion(OptionalInt.empty(), Paths.get("src/test/resources/deployment.xml"));
+ assertMajorVersion(OptionalInt.of(8), Paths.get("src/test/resources/deployment-with-major.xml"));
+ }
+
+ private void assertMajorVersion(OptionalInt expected, Path deploymentXml) {
+ OptionalInt allowMajor = CompileVersionMojo.majorVersion(deploymentXml);
+ assertEquals(expected, allowMajor);
+ }
+
+}
diff --git a/vespa-maven-plugin/src/test/resources/deployment-with-major.xml b/vespa-maven-plugin/src/test/resources/deployment-with-major.xml
new file mode 100644
index 00000000000..5ffc3e81fcc
--- /dev/null
+++ b/vespa-maven-plugin/src/test/resources/deployment-with-major.xml
@@ -0,0 +1,6 @@
+<!-- Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -->
+<deployment version='1.0' major-version='8'>
+ <prod>
+ <region>us-east</region>
+ </prod>
+</deployment>
diff --git a/vespa-maven-plugin/src/test/resources/deployment.xml b/vespa-maven-plugin/src/test/resources/deployment.xml
new file mode 100644
index 00000000000..3d63224cf13
--- /dev/null
+++ b/vespa-maven-plugin/src/test/resources/deployment.xml
@@ -0,0 +1,6 @@
+<!-- Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -->
+<deployment version='1.0'>
+ <prod>
+ <region>us-east</region>
+ </prod>
+</deployment>