diff --git a/pom.xml b/pom.xml index d9bf9c91e..88b283be3 100644 --- a/pom.xml +++ b/pom.xml @@ -173,7 +173,7 @@ header-java.txt **/*.java,**/*.groovy,**/*.scala,**/*.properties,**/*.xml,**/*.xsd - **/MyRoutes*.java,**/routes*.java + **/MyRoutes*.java,**/routes*.java,**/verify.groovy checkstyle diff --git a/tooling/camel-k-maven-plugin/pom.xml b/tooling/camel-k-maven-plugin/pom.xml index ce349c6fb..3639ffeb5 100644 --- a/tooling/camel-k-maven-plugin/pom.xml +++ b/tooling/camel-k-maven-plugin/pom.xml @@ -164,6 +164,18 @@ + + org.apache.maven.plugins + maven-jar-plugin + + + + true + true + + + + org.apache.maven.plugins maven-plugin-plugin @@ -187,6 +199,37 @@ + + org.apache.maven.plugins + maven-invoker-plugin + + ${project.build.directory}/it + true + verify + true + true + + generate-resources + + + + + integration-tests + + install + run + verify + + + + + + org.yaml + snakeyaml + 1.25 + + + diff --git a/tooling/camel-k-maven-plugin/src/it/generate-catalog/pom.xml b/tooling/camel-k-maven-plugin/src/it/generate-catalog/pom.xml new file mode 100644 index 000000000..2eb3ee309 --- /dev/null +++ b/tooling/camel-k-maven-plugin/src/it/generate-catalog/pom.xml @@ -0,0 +1,109 @@ + + + + + + 4.0.0 + org.apache.camel.k + camel-k-catalog-generator + 1.0.0 + + + ${project.basedir} + 3.0.0-RC1 + 1.0.2-SNAPSHOT + + + + + + org.apache.camel.k + camel-k-maven-plugin + ${runtime.version} + + + generate-catalog + + generate-catalog + + + + + + org.apache.camel + camel-catalog + ${camel.version} + + + + + + + + + apache.snapshots + Apache Development Snapshot Repository + https://repository.apache.org/content/repositories/snapshots/ + + false + + + true + + + + oss.snapshots + https://oss.sonatype.org/content/repositories/snapshots + + false + + + true + + + + + + + apache.snapshots + Apache Development Snapshot Repository + https://repository.apache.org/content/repositories/snapshots/ + + false + + + true + + + + oss.snapshots + https://oss.sonatype.org/content/repositories/snapshots + + false + + + true + + + + + diff --git a/tooling/camel-k-maven-plugin/src/it/generate-catalog/verify.groovy b/tooling/camel-k-maven-plugin/src/it/generate-catalog/verify.groovy new file mode 100644 index 000000000..b77738f4e --- /dev/null +++ b/tooling/camel-k-maven-plugin/src/it/generate-catalog/verify.groovy @@ -0,0 +1,26 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +def catalogVersion = '3.0.0-RC1' +def runtimeVersion = '1.0.2-SNAPSHOT' + +def source = new File(basedir, "camel-catalog-${catalogVersion}-${runtimeVersion}.yaml") +def catalog = new org.yaml.snakeyaml.Yaml().load(new FileInputStream(source)) + +assert catalog.spec.version == catalogVersion +assert catalog.spec.runtimeVersion == runtimeVersion +assert catalog.metadata.labels['camel.apache.org/catalog.version'] == catalogVersion +assert catalog.metadata.labels['camel.apache.org/runtime.version'] == runtimeVersion \ No newline at end of file diff --git a/tooling/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/GenerateCatalogMojo.java b/tooling/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/GenerateCatalogMojo.java index 5980901e3..95354ea34 100644 --- a/tooling/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/GenerateCatalogMojo.java +++ b/tooling/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/GenerateCatalogMojo.java @@ -17,6 +17,7 @@ package org.apache.camel.k.tooling.maven; import java.io.IOException; +import java.io.InputStream; import java.io.Writer; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -25,6 +26,7 @@ import java.util.Comparator; import java.util.Map; import java.util.Objects; +import java.util.Properties; import java.util.ServiceLoader; import java.util.SortedMap; import java.util.TreeMap; @@ -69,7 +71,7 @@ public class GenerateCatalogMojo extends AbstractMojo { @Parameter(property = "catalog.path", defaultValue = "${project.build.directory}") private String outputPath; - @Parameter(property = "catalog.file", defaultValue = "camel-catalog-${catalog.version}.yaml") + @Parameter(property = "catalog.file", defaultValue = "camel-catalog-${camel.version}-${runtime.version}.yaml") private String outputFile; // ******************** @@ -116,26 +118,35 @@ public void execute() throws MojoExecutionException, MojoFailureException { // apiVersion: camel.apache.org/v1alpha1 // kind: CamelCatalog // metadata: - // name: catalog-x.y.z + // name: catalog-x.y.z-a.b.c // labels: // app: "camel-k" // camel.apache.org/catalog.version: x.y.x // camel.apache.org/catalog.loader.version: x.y.z + // camel.apache.org/runtime.version: x.y.x // spec: // version: + // runtimeVersion: // status: // artifacts: // try (Writer writer = Files.newBufferedWriter(output, StandardCharsets.UTF_8)) { + String catalogName = String.format("camel-catalog-%s-%s", + catalog.getCatalogVersion().toLowerCase(), + getRuntimeVersion().toLowerCase() + ); + CamelCatalog cr = new CamelCatalog.Builder() .metadata(new ObjectMeta.Builder() - .name("camel-catalog-" + catalog.getCatalogVersion().toLowerCase()) + .name(catalogName) .putLabels("app", "camel-k") .putLabels("camel.apache.org/catalog.version", catalog.getCatalogVersion()) .putLabels("camel.apache.org/catalog.loader.version", catalog.getLoadedVersion()) + .putLabels("camel.apache.org/runtime.version", getRuntimeVersion()) .build()) .spec(new CamelCatalogSpec.Builder() .version(catalog.getCatalogVersion()) + .runtimeVersion(getRuntimeVersion()) .artifacts(artifacts) .build()) .build(); @@ -152,6 +163,8 @@ public void execute() throws MojoExecutionException, MojoFailureException { GenerateSupport.getResourceAsString("/catalog-license.txt") ); + getLog().info("Writing catalog file to: " + output); + // write catalog data ObjectMapper mapper = new ObjectMapper(factory); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); @@ -237,4 +250,42 @@ private void processDataFormats(org.apache.camel.catalog.CamelCatalog catalog, M }); } } + + public synchronized String getRuntimeVersion() { + String version = null; + + // try to load from maven properties first + try { + + InputStream is = getClass().getResourceAsStream( + "/META-INF/maven/org.apache.camel.k/camel-k-maven-plugin/pom.properties" + ); + + if (is != null) { + Properties p = new Properties(); + p.load(is); + version = p.getProperty("version", ""); + } + } catch (Exception e) { + // ignore + } + + // fallback to using Java API + if (version == null) { + Package aPackage = getClass().getPackage(); + if (aPackage != null) { + version = aPackage.getImplementationVersion(); + if (version == null) { + version = aPackage.getSpecificationVersion(); + } + } + } + + if (version == null) { + // we could not compute the version so use a blank + throw new IllegalStateException("Unable to determine runtime version"); + } + + return version; + } } diff --git a/tooling/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/model/crd/CamelCatalogSpec.java b/tooling/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/model/crd/CamelCatalogSpec.java index 4173463b3..0e884476d 100644 --- a/tooling/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/model/crd/CamelCatalogSpec.java +++ b/tooling/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/model/crd/CamelCatalogSpec.java @@ -26,9 +26,10 @@ @Value.Immutable @JsonDeserialize(builder = CamelCatalogSpec.Builder.class) -@JsonPropertyOrder({ "version", "artifacts" }) +@JsonPropertyOrder({ "version", "runtimeVersion", "artifacts" }) public interface CamelCatalogSpec { String getVersion(); + String getRuntimeVersion(); @Value.Default default SortedMap getArtifacts() {