Skip to content

Commit

Permalink
camel-k-maven-plugin: include checksum information in the generated d…
Browse files Browse the repository at this point in the history
…ependencies list
  • Loading branch information
lburgazzoli committed Apr 2, 2020
1 parent 020c6b5 commit 064672f
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 5 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<joor.version>0.9.12</joor.version>
<commons-lang.version>3.10</commons-lang.version>
<commons-text.version>1.8</commons-text.version>
<commons-codec.version>1.14</commons-codec.version>
<commons-dbcp2.version>2.7.0</commons-dbcp2.version>
<assertj.version>3.15.0</assertj.version>
<log4j2.version>2.13.1</log4j2.version>
Expand Down
10 changes: 9 additions & 1 deletion tooling/camel-k-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@
<artifactId>commons-text</artifactId>
<version>${commons-text.version}</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<dependency>
<groupId>com.vdurmont</groupId>
<artifactId>semver4j</artifactId>
Expand Down Expand Up @@ -235,18 +240,21 @@
<quarkusVersion>${quarkus.version}</quarkusVersion>
</scriptVariables>
<pomIncludes>
<!--
<pomInclude>generate-catalog-main/pom.xml</pomInclude>
<pomInclude>generate-catalog-quarkus/pom.xml</pomInclude>
<pomInclude>generate-rest-dsl-from-v3/pom.xml</pomInclude>
<pomInclude>generate-rest-dsl-from-v2/pom.xml</pomInclude>
-->
<pomInclude>generate-dependencies/pom.xml</pomInclude>
</pomIncludes>
<cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
<cloneClean>true</cloneClean>
<postBuildHookScript>verify</postBuildHookScript>
<addTestClassPath>true</addTestClassPath>
<streamLogs>true</streamLogs>
<goals>
<goal>generate-resources</goal>
<goal>prepare-package</goal>
</goals>
</configuration>
<executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<version>1.0.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<catalog.path>${project.basedir}</catalog.path>
<catalog.runtime>main</catalog.runtime>
<catalog.file>catalog.yaml</catalog.file>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<version>1.0.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<catalog.path>${project.basedir}</catalog.path>
<catalog.runtime>quarkus</catalog.runtime>
<catalog.file>catalog.yaml</catalog.file>
Expand Down
107 changes: 107 additions & 0 deletions tooling/camel-k-maven-plugin/src/it/generate-dependencies/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.camel.k</groupId>
<artifactId>camel-k-catalog-generator</artifactId>
<version>1.0.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<dependencies.file>${project.basedir}/dependencies.yaml</dependencies.file>
</properties>

<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.14</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.camel.k</groupId>
<artifactId>camel-k-maven-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>generate-dependency-list</id>
<goals>
<goal>generate-dependency-list</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>apache.snapshots</id>
<name>Apache Development Snapshot Repository</name>
<url>https://repository.apache.org/content/repositories/snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>oss.snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>apache.snapshots</id>
<name>Apache Development Snapshot Repository</name>
<url>https://repository.apache.org/content/repositories/snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>oss.snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.
*/

import org.apache.commons.codec.digest.DigestUtils

import java.nio.charset.StandardCharsets
import java.nio.file.Files

new File(basedir, "dependencies.yaml").withReader {
def deps = new groovy.yaml.YamlSlurper().parse(it)

for (Map<String, String> dependency: deps.dependencies) {
dependency.checksum != null
dependency.location != null
dependency.id != null

File checksum

if ((checksum = new File("${dependency.location}.md5")).exists()) {
dependency.checksum == "md5:" + Files.readString(checksum.toPath(), StandardCharsets.UTF_8)
} else if ((checksum = new File("${dependency.location}.sha1")).exists()) {
dependency.checksum == "sha1:" + Files.readString(checksum.toPath(), StandardCharsets.UTF_8)
} else {
def file = new File(dependency.location)
def bytes = Files.readAllBytes(file.toPath())

dependency.checksum == "sha1:" + DigestUtils.sha1Hex(bytes)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<version>1.0.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<openapi.spec>${project.basedir}/document.json</openapi.spec>
<dsl.out>${project.basedir}/document.xml</dsl.out>
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<version>1.0.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<openapi.spec>${project.basedir}/document.yaml</openapi.spec>
<dsl.out>${project.basedir}/document.xml</dsl.out>
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@
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;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.plugin.AbstractMojo;
Expand All @@ -49,17 +51,20 @@
requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME)
public class GenerateDependencyListMojo extends AbstractMojo {
private final String[] CHECKSUM_TYPES = { "md5", "sha1"};

@Parameter(readonly = true, defaultValue = "${project}")
private MavenProject project;

@Parameter(defaultValue = "${project.build.directory}/dependencies.yaml")
@Parameter(property = "dependencies.file", defaultValue = "${project.build.directory}/dependencies.yaml")
private String outputFile;

@Parameter(defaultValue = "true")
private boolean includeLocation;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
getLog().info(">>>" + this.outputFile);
final Path output = Paths.get(this.outputFile);

try {
Expand Down Expand Up @@ -92,13 +97,40 @@ private boolean isCompileOrRuntime(Artifact artifact) {
}

private Map<String, String> artifactToMap(Artifact artifact) {
Map<String, String> dep = new HashMap<>();
Map<String, String> dep = new LinkedHashMap<>();
dep.put("id", artifact.getId());

if (includeLocation && artifact.getFile() != null) {
if (artifact.getFile() == null) {
return dep;
}

if (includeLocation) {
dep.put("location", artifact.getFile().getAbsolutePath());
}

try {
String location = artifact.getFile().getAbsolutePath();
String checksum = null;

for (String checksumType : CHECKSUM_TYPES) {
Path checksumFile = Paths.get(location + "." + checksumType);
if (Files.exists(checksumFile)) {
checksum = checksumType + ":" + Files.readString(checksumFile, StandardCharsets.UTF_8);
break;
}
}

if (checksum == null) {
try (InputStream is = Files.newInputStream(artifact.getFile().toPath())) {
checksum = "sh1:" + DigestUtils.sha1Hex(is);
}
}

dep.put("checksum", checksum);
} catch (IOException e) {
throw new RuntimeException(e);
}

return dep;
}
}

0 comments on commit 064672f

Please sign in to comment.