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

Commit

Permalink
Added new parameter: hardLinkDependencies
Browse files Browse the repository at this point in the history
Instead of copying artifact to directory, hard link files.
This option cannot be used if local maven repository is in different partition.
Two benefits:
1) Requires less space to build
2) Faster because only link needs to be written
And cons:
1) Implementation requires Java 7 (nio)
2) Code is not backwards compatible (integration tests were broken by previous
regression, copied fix from #85)

Also fixed issue with Travis: default distro (trusty) does not support oraclejdk7
  • Loading branch information
Turmio committed Sep 10, 2017
1 parent 0ae25c4 commit 0c3126b
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: java

jdk:
- openjdk6
- oraclejdk7
- openjdk7 #Travis does not support oraclejdk7 anymore
- oraclejdk8

# Install the rpmbuild tool we need.
Expand Down
2 changes: 1 addition & 1 deletion provision.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function install_mvn {
fi
}

install java-1.6.0-openjdk-devel
install java-1.7.0-openjdk-devel
install rpm-build
install wget
install_mvn 2.2.1
Expand Down
1 change: 1 addition & 0 deletions src/it/rpm-dependency-hardlink/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals = clean package
54 changes: 54 additions & 0 deletions src/it/rpm-dependency-hardlink/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.codehaus.mojo.rpm.its</groupId>
<artifactId>rpm-dependency-hardlink-module-rpm</artifactId>
<version>1.0</version>
<packaging>rpm</packaging>

<inceptionYear>2017</inceptionYear>
<organization>
<name>my org</name>
<url>www.my.org</url>
</organization>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>rpm-maven-plugin</artifactId>
<version>@pom.version@</version>
<extensions>true</extensions>
<configuration>
<distribution>My App</distribution>
<group>Application/Collectors</group>
<release>rel</release>
<needarch>false</needarch>
<defaultUsername>myuser</defaultUsername>
<defaultGroupname>mygroup</defaultGroupname>
<hardLinkDependencies>true</hardLinkDependencies>
<mappings>
<mapping>
<directory>/usr/myusr/app</directory>
<dependency />
<directoryIncluded>false</directoryIncluded>
</mapping>
</mappings>
</configuration>
</plugin>
</plugins>
</build>
</project>
12 changes: 12 additions & 0 deletions src/it/rpm-dependency-hardlink/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
File rpm = new File(basedir, "target/rpm/rpm-dependency-hardlink-module-rpm/RPMS/noarch/rpm-dependency-hardlink-module-rpm-1.0-rel.noarch.rpm")
if (!rpm.exists())
throw new AssertionError("rpm file does not exist: ${rpm.getAbsolutePath()}")

def proc = ["rpm", "-qlp", rpm.getAbsolutePath()].execute()
proc.waitFor()
proc.in.text.eachLine {
if (!it.equals("/usr/myusr/app/log4j-1.2.14.jar"))
throw new AssertionError("rpm.dependency-hardlink log4j-1.2.14.jar missing!")
}

return true
23 changes: 21 additions & 2 deletions src/main/java/org/codehaus/mojo/rpm/AbstractRPMMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,16 @@ abstract class AbstractRPMMojo
*/
@Parameter(defaultValue = "false")
private boolean skipPOMs;

/**
* Indicates if the dependencies should be hard linked instead of copied.
* <br />
* <b>NOTE:</b> Hard link requires maven repository to be on same partition as project's build dir.
*
* @since TODO
*/
@Parameter(defaultValue = "false")
private boolean hardLinkDependencies;

//////////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -1628,22 +1638,30 @@ final List<FilterWrapper> getFilterWrappers()
return this.defaultFilterWrappers;
}

/**
* @return Returns the {@link #hardLinkDependencies}
*/
final boolean isHardLinkDependencies() {
return hardLinkDependencies;
}

/**
* @return the rpmbuildStage
*/
final public String getRpmbuildStage()
final String getRpmbuildStage()
{
return rpmbuildStage;
}

/**
* @param rpmRpmbuildStage the rpmRpmbuildStage to set
*/
final public void setRpmbuildStage( String rpmbuildStage )
final void setRpmbuildStage( String rpmbuildStage )
{
this.rpmbuildStage = rpmbuildStage;
}


/**
* Load and decrypt gpg passphrase from maven settings if not given from plugin configuration
*
Expand Down Expand Up @@ -1673,4 +1691,5 @@ private void loadGpgPassphrase()
}
}
}

}
39 changes: 33 additions & 6 deletions src/main/java/org/codehaus/mojo/rpm/FileHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
*/

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.LinkedList;
Expand Down Expand Up @@ -135,7 +138,7 @@ public void installFiles()
List<Artifact> artlist = selectArtifacts( art );
for ( Artifact artifactInstance : artlist )
{
copyArtifact( artifactInstance, dest, false );
copyArtifact( artifactInstance, dest, false, false);
map.addCopiedFileNameRelativeToDestination( artifactInstance.getFile().getName() );
}
}
Expand All @@ -147,7 +150,7 @@ public void installFiles()
for ( Artifact artifactInstance : deplist )
{
// pass in dependency stripVersion parameter
String outputFileName = copyArtifact( artifactInstance, dest, dep.getStripVersion() );
String outputFileName = copyArtifact( artifactInstance, dest, dep.getStripVersion(), mojo.isHardLinkDependencies());
map.addCopiedFileNameRelativeToDestination( outputFileName );
}
}
Expand All @@ -168,7 +171,27 @@ public void installFiles()
}
}
}


/**
*
* @param src The source directory/file
* @param outputFileName The destination of link
* @throws MojoExecutionException
*/
private void hardLinkSource(File src, Path outputFileName) throws MojoExecutionException {
try {
//Delete existing file always, previous file may be different version with same filename
if(Files.exists(outputFileName)) {
mojo.getLog().debug("Deleting previous file for ensuring hard link points to right target.");
Files.delete(outputFileName);
}
mojo.getLog().debug("Creating hard link from "+ src.getAbsolutePath() + " to " + outputFileName);
Files.createLink(outputFileName, src.toPath());
} catch (IOException e) {
throw new MojoExecutionException("Unable to hard link source (" + src.getAbsolutePath() +"): " + e.getMessage(), e);
}
}

/**
* Copy a set of files.
*
Expand Down Expand Up @@ -265,7 +288,7 @@ public static DefaultFileSet fileSet( File directory )
* @return Artifact file name
* @throws MojoExecutionException if a problem occurs
*/
private String copyArtifact( Artifact art, File dest, boolean stripVersion )
private String copyArtifact( Artifact art, File dest, boolean stripVersion, boolean hardLinkOnly )
throws MojoExecutionException
{
if ( art.getFile() == null )
Expand Down Expand Up @@ -294,11 +317,15 @@ private String copyArtifact( Artifact art, File dest, boolean stripVersion )
{
outputFileName = art.getFile().getName();
}

copySource( art.getFile(), outputFileName, dest, null, null, false, false );
if(hardLinkOnly) {
hardLinkSource(art.getFile(), dest.toPath().resolve(outputFileName));
} else {
copySource( art.getFile(), outputFileName, dest, null, null, false, false );
}
return outputFileName;
}


/**
* Make a list of the artifacts to package in this mapping.
*
Expand Down

0 comments on commit 0c3126b

Please sign in to comment.