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

Add addDependencyBasedRequires configuration option #95

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 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,47 @@ 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 = helper.getRpmAttribute( rpmFile, "NAME" );
String rpmVersion = helper.getRpmAttribute( rpmFile, "VERSION" );
String rpmRelease = helper.getRpmAttribute( rpmFile, "RELEASE" );
require.append( rpmName )
.append( " >= " )
.append( rpmVersion )
.append( "-" )
.append( rpmRelease );
getLog().info( "Add dependency-based require: " + require.toString() );
requires.add( require.toString() );
}
}
}
EagleErwin marked this conversation as resolved.
Show resolved Hide resolved

/**
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/org/codehaus/mojo/rpm/RPMHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
* under the License.
*/

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
Expand Down Expand Up @@ -250,4 +253,48 @@ public String getArch()

return stdConsumer.getOutput().trim();
}

/**
* Gets the value of the RPM attribute for the given RPM file..
* @param rpmFile The full path of the RPM file.
* @param attribute The attribute to get the value for.
* @return A String containing the value of the requested attribute.
* @throws MojoExecutionException if the attribute could not be read.
*/
public String getRpmAttribute( File rpmFile, String attribute )
throws MojoExecutionException
{
final Commandline cl = new Commandline();
cl.setExecutable( "rpm" );
cl.createArg().setValue( "--query" );
cl.createArg().setValue( "--package" );
cl.createArg().setValue( rpmFile.getAbsolutePath() );
cl.createArg().setValue( "--queryformat" );
cl.createArg().setValue( "%{" + attribute + "}" );

final Log log = mojo.getLog();

final StringStreamConsumer stdout = new StringStreamConsumer();
final StreamConsumer stderr = new LogStreamConsumer( LogStreamConsumer.INFO, log );
try
{
if ( log.isDebugEnabled() )
{
log.debug( "About to execute \'" + cl.toString() + "\'" );
}

int result = CommandLineUtils.executeCommandLine( cl, stdout, stderr );
if ( result != 0 )
{
throw new MojoExecutionException( "rpm -query returned: \'" + result + "\' executing \'"
+ cl.toString() + "\'" );
}
}
catch ( CommandLineException e )
{
throw new MojoExecutionException( "Unable to get " + attribute + " from rpm", e );
}

return stdout.getOutput().trim();
}
}
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