From 53ae116faccb058c81141ffc6f79d76d7ace0db5 Mon Sep 17 00:00:00 2001 From: Erwin Bronkhorst Date: Thu, 25 Jan 2018 16:08:12 +0000 Subject: [PATCH] Add addDependencyBasedRequires Parameter To avoid duplication of dependency information from the Maven pom file towards the list of the rpm-maven-plugin configuration a new Parameter is added: addDependencyBasedRequires. The idea of this feature is that the rpm-maven-plugin itself inspects the dependencies of the project in which it is executed and adds a Require: [rpm-name] >= [rpm-version] for each found RPM dependency. --- .../codehaus/mojo/rpm/AbstractRPMMojo.java | 70 +++++++++++++++++++ src/site/apt/adv-params.apt | 11 +++ 2 files changed, 81 insertions(+) diff --git a/src/main/java/org/codehaus/mojo/rpm/AbstractRPMMojo.java b/src/main/java/org/codehaus/mojo/rpm/AbstractRPMMojo.java index 1a69d094..0c82592f 100644 --- a/src/main/java/org/codehaus/mojo/rpm/AbstractRPMMojo.java +++ b/src/main/java/org/codehaus/mojo/rpm/AbstractRPMMojo.java @@ -23,6 +23,7 @@ import java.io.File; import java.io.FileReader; import java.io.FileWriter; +import java.io.InputStreamReader; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; @@ -608,6 +609,14 @@ public abstract class AbstractRPMMojo @Parameter( required = true, defaultValue = "rpm.release" ) private String releaseProperty; + /** + * When enabled, automatically generates requires for all (transitive) rpm-dependencies. + * + * @since 2.2.1 + */ + @Parameter + private boolean addDependencyBasedRequires; + /** * The changelog file. If the file does not exist, it is ignored. * @@ -1069,6 +1078,67 @@ else if ( "true".equalsIgnoreCase( needarch ) ) } processDefineStatements(); + + if (addDependencyBasedRequires) { + appendDependencyBasedRequires(); + } + } + + /** + * Extends this.requires with additional require for each immediate rpm-type dependency + * of the project, requiring the same or a higher version as the resolved-dependency-version. + * + * @throws MojoExecutionException if the projects dependency-tree cannot be generated. + * @since 2.2.1 + */ + private void appendDependencyBasedRequires() throws MojoExecutionException { + if (this.requires == null) { + this.requires = new LinkedHashSet(); + } + for(Object dependency: project.getDependencyArtifacts()) { + Artifact dependencyArtifact = (Artifact) dependency; + if ("rpm".equals(dependencyArtifact.getType())) { + StringBuilder require = new StringBuilder(); + getLog().debug("Determining rpm name and version of rpm-dependency " + dependencyArtifact); + File rpmFile = dependencyArtifact.getFile(); + String rpmName = getRpmAttribute(rpmFile, "NAME"); + String rpmVersion = getRpmAttribute(rpmFile, "VERSION"); + String rpmRelease = getRpmAttribute(rpmFile, "RELEASE"); + require.append(rpmName) + .append(" >= ") + .append(rpmVersion) + .append("-") + .append(rpmRelease); + getLog().info("Add dependency-based require: " + require.toString()); + requires.add(require.toString()); + } + } + } + + private String getRpmAttribute(File rpmFile, String attribute) throws MojoExecutionException { + String attributeValue = null; + try { + Process rpmQuery = Runtime.getRuntime().exec(new String[]{"rpm", "--query", "--package", rpmFile.getAbsolutePath(), + "--queryformat", "%{" + attribute + "}"}); + if (rpmQuery.waitFor() == 0) { + attributeValue = new BufferedReader(new InputStreamReader(rpmQuery.getInputStream())).readLine().trim(); + } else { + StringBuilder errorMsg = new StringBuilder(); + BufferedReader errorStreamReader = new BufferedReader(new InputStreamReader(rpmQuery.getErrorStream())); + String line = errorStreamReader.readLine(); + while (line != null) { + errorMsg.append(line).append("\n"); + line = errorStreamReader.readLine(); + } + throw new MojoExecutionException("Failed to get attribute '" + attribute + "' from rpm '" + rpmFile + "'\n" + errorMsg); + } + } catch (InterruptedException e) { + throw new MojoExecutionException("Failed to get attribute '" + attribute + "' from rpm '" + rpmFile + "'", e); + } catch (IOException e) { + throw new MojoExecutionException("Failed to get attribute '" + attribute + "' from rpm '" + rpmFile + "'", e); + } + // read stdout, and return as String + return attributeValue; } /** diff --git a/src/site/apt/adv-params.apt b/src/site/apt/adv-params.apt index ddecd5fa..17497248 100644 --- a/src/site/apt/adv-params.apt +++ b/src/site/apt/adv-params.apt @@ -76,6 +76,17 @@ Advanced Parameters +-----+ +* addDependencyBasedRequires + + If you set the boolean property <<>> to <<>>, + all Maven dependencies of type <<>> in the current project will be added as + a dependency to the generated RPM. + + Using this option, you don't need to specify the <<>> option (see above) + for the direct RPM dependencies that you specified in your pom.xml. + + This option defaults to <<>>. + * RPM Build Dependency Management <>