Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
Add addDependencyBasedRequires Parameter
Browse files Browse the repository at this point in the history
To avoid duplication of dependency information from the Maven pom file towards the <requires> 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.
  • Loading branch information
EagleErwin committed Mar 29, 2018
1 parent 9e0ce64 commit 53ae116
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/main/java/org/codehaus/mojo/rpm/AbstractRPMMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/site/apt/adv-params.apt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ Advanced Parameters
</conflicts>
+-----+

* addDependencyBasedRequires

If you set the boolean property <<<addDependencyBasedRequires>>> to <<<true>>>,
all Maven dependencies of type <<<rpm>>> in the current project will be added as
a dependency to the generated RPM.

Using this option, you don't need to specify the <<<requires>>> option (see above)
for the direct RPM dependencies that you specified in your pom.xml.

This option defaults to <<<false>>>.

* RPM Build Dependency Management

<<These parameters relate to the dependencies required to build the RPM package contents.>>
Expand Down

0 comments on commit 53ae116

Please sign in to comment.