Skip to content

Commit

Permalink
[FIXED JENKINS-18662] Added exclude filter.
Browse files Browse the repository at this point in the history
  • Loading branch information
ikedam committed Jul 13, 2014
1 parent 609ca2e commit b605dc8
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 16 deletions.
38 changes: 34 additions & 4 deletions src/main/java/hudson/plugins/copyartifact/Copier.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import hudson.FilePath;
import hudson.model.AbstractBuild;
import hudson.model.Run;
import org.apache.jackrabbit.webdav.client.methods.CopyMethod;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Extension point for how files are copied.
Expand All @@ -22,6 +23,8 @@
*/
public abstract class Copier implements ExtensionPoint {

private static Logger LOG = Logger.getLogger(Copier.class.getName());

/**
* Called before copy-artifact operation.
* @param src
Expand All @@ -36,22 +39,49 @@ public abstract class Copier implements ExtensionPoint {

/**
* @deprecated
* call/override {@link #copyAll(FilePath srcDir, String filter, FilePath targetDir, boolean fingerprintArtifacts)} instead.
* call/override {@link #copyAll(FilePath srcDir, String filter, String excludes, FilePath targetDir, boolean fingerprintArtifacts)} instead.
*/
public int copyAll(FilePath srcDir, String filter, FilePath targetDir) throws IOException, InterruptedException {
return copyAll(srcDir, filter, targetDir, true);
return copyAll(srcDir, filter, null, targetDir, true);
}

/**
* @deprecated
* call/override {@link #copyAll(FilePath, String, String, FilePath, boolean)} instead.
*/
@Deprecated
public int copyAll(FilePath srcDir, String filter, FilePath targetDir, boolean fingerprintArtifacts) throws IOException, InterruptedException {
return copyAll(srcDir, filter, null, targetDir, fingerprintArtifacts);
}

/**
* Copy files matching the given file mask to the specified target.
*
* You must override this when deriving {@link Copier}.
*
* @param srcDir Source directory
* @param filter Ant GLOB pattern
* @param excludes Ant GLOB pattern. Can be null.
* @param targetDir Target directory
* @param fingerprintArtifacts boolean controlling if the copy should also fingerprint the artifacts
* @return Number of files that were copied
* @see FilePath#copyRecursiveTo(String,FilePath)
*/
public abstract int copyAll(FilePath srcDir, String filter, FilePath targetDir, boolean fingerprintArtifacts) throws IOException, InterruptedException;
public int copyAll(FilePath srcDir, String filter, String excludes, FilePath targetDir, boolean fingerprintArtifacts) throws IOException, InterruptedException {
try {
Class<?> classOfCopyAll = getClass().getMethod("copyAll", FilePath.class, String.class, FilePath.class, boolean.class).getDeclaringClass();
if (!Copier.class.equals(classOfCopyAll)) {
// For backward compatibility.
// avoid cyclic invocation.
return copyAll(srcDir, filter, targetDir, fingerprintArtifacts);
}
} catch(SecurityException e) {
LOG.log(Level.WARNING, "Unexpected exception in copyartifact-plugin", e);
} catch(NoSuchMethodException e) {
LOG.log(Level.WARNING, "Unexpected exception in copyartifact-plugin", e);
}
throw new AbstractMethodError("You need override Copier#copyAll(FilePath, String, String, FilePath, boolean)");
}

/**
* @deprecated
Expand Down
34 changes: 26 additions & 8 deletions src/main/java/hudson/plugins/copyartifact/CopyArtifact.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import org.acegisecurity.Authentication;
import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
Expand All @@ -86,6 +87,7 @@ public class CopyArtifact extends Builder {
private String project;
private String parameters;
private final String filter, target;
private final String excludes;
private /*almost final*/ BuildSelector selector;
@Deprecated private transient Boolean stable;
private final Boolean flatten, optional;
Expand All @@ -97,8 +99,14 @@ public CopyArtifact(String projectName, String parameters, BuildSelector selecto
this(projectName, parameters, selector, filter, target, flatten, optional, true);
}

@DataBoundConstructor
@Deprecated
public CopyArtifact(String projectName, String parameters, BuildSelector selector, String filter, String target,
boolean flatten, boolean optional, boolean fingerprintArtifacts) {
this(projectName, parameters, selector, filter, null, target, flatten, optional, fingerprintArtifacts);
}

@DataBoundConstructor
public CopyArtifact(String projectName, String parameters, BuildSelector selector, String filter, String excludes, String target,
boolean flatten, boolean optional, boolean fingerprintArtifacts) {
// check the permissions only if we can
StaplerRequest req = Stapler.getCurrentRequest();
Expand All @@ -118,6 +126,7 @@ public CopyArtifact(String projectName, String parameters, BuildSelector selecto
this.parameters = Util.fixEmptyAndTrim(parameters);
this.selector = selector;
this.filter = Util.fixNull(filter).trim();
this.excludes = Util.fixNull(excludes).trim();
this.target = Util.fixNull(target).trim();
this.flatten = flatten ? Boolean.TRUE : null;
this.optional = optional ? Boolean.TRUE : null;
Expand Down Expand Up @@ -204,6 +213,10 @@ public String getFilter() {
return filter;
}

public String getExcludes() {
return excludes;
}

public String getTarget() {
return target;
}
Expand Down Expand Up @@ -249,6 +262,7 @@ public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListene
upgradeIfNecessary(build.getProject());
PrintStream console = listener.getLogger();
String expandedProject = project, expandedFilter = filter;
String expandedExcludes = getExcludes();
try {
EnvVars env = build.getEnvironment(listener);
env.overrideAll(build.getBuildVariables()); // Add in matrix axes..
Expand Down Expand Up @@ -281,19 +295,23 @@ public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListene
if (target.length() > 0) targetDir = new FilePath(targetDir, env.expand(target));
expandedFilter = env.expand(filter);
if (expandedFilter.trim().length() == 0) expandedFilter = "**";
expandedExcludes = env.expand(expandedExcludes);
if (StringUtils.isBlank(expandedExcludes)) {
expandedExcludes = null;
}

Copier copier = Jenkins.getInstance().getExtensionList(Copier.class).get(0).clone();

if (Hudson.getInstance().getPlugin("maven-plugin") != null && (src instanceof MavenModuleSetBuild) ) {
// use classes in the "maven-plugin" plugin as might not be installed
// Copy artifacts from the build (ArchiveArtifacts build step)
boolean ok = perform(src, build, expandedFilter, targetDir, baseTargetDir, copier, console);
boolean ok = perform(src, build, expandedFilter, expandedExcludes, targetDir, baseTargetDir, copier, console);
// Copy artifacts from all modules of this Maven build (automatic archiving)
for (Iterator<MavenBuild> it = ((MavenModuleSetBuild)src).getModuleLastBuilds().values().iterator(); it.hasNext(); ) {
// for(Run r: ....values()) causes upcasting and loading MavenBuild compiled with jdk 1.6.
// SEE https://wiki.jenkins-ci.org/display/JENKINS/Tips+for+optional+dependencies for details.
Run<?,?> r = it.next();
ok |= perform(r, build, expandedFilter, targetDir, baseTargetDir, copier, console);
ok |= perform(r, build, expandedFilter, expandedExcludes, targetDir, baseTargetDir, copier, console);
}
return ok;
} else if (src instanceof MatrixBuild) {
Expand All @@ -302,11 +320,11 @@ public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListene
// Use MatrixBuild.getExactRuns if available
for (Run r : ((MatrixBuild) src).getExactRuns())
// Use subdir of targetDir with configuration name (like "jdk=java6u20")
ok |= perform(r, build, expandedFilter, targetDir.child(r.getParent().getName()),
ok |= perform(r, build, expandedFilter, expandedExcludes, targetDir.child(r.getParent().getName()),
baseTargetDir, copier, console);
return ok;
} else {
return perform(src, build, expandedFilter, targetDir, baseTargetDir, copier, console);
return perform(src, build, expandedFilter, expandedExcludes, targetDir, baseTargetDir, copier, console);
}
}
catch (IOException ex) {
Expand Down Expand Up @@ -363,7 +381,7 @@ private ItemGroup getItemGroup(AbstractBuild<?, ?> build) {
}


private boolean perform(Run src, AbstractBuild<?,?> dst, String expandedFilter, FilePath targetDir,
private boolean perform(Run src, AbstractBuild<?,?> dst, String expandedFilter, String expandedExcludes, FilePath targetDir,
FilePath baseTargetDir, Copier copier, PrintStream console)
throws IOException, InterruptedException {
FilePath srcDir = selector.getSourceDirectory(src, console);
Expand All @@ -375,10 +393,10 @@ private boolean perform(Run src, AbstractBuild<?,?> dst, String expandedFilter,
try {
int cnt;
if (!isFlatten())
cnt = copier.copyAll(srcDir, expandedFilter, targetDir, isFingerprintArtifacts());
cnt = copier.copyAll(srcDir, expandedFilter, expandedExcludes, targetDir, isFingerprintArtifacts());
else {
targetDir.mkdirs(); // Create target if needed
FilePath[] list = srcDir.list(expandedFilter, null, false);
FilePath[] list = srcDir.list(expandedFilter, expandedExcludes, false);
for (FilePath file : list)
copier.copyOne(file, new FilePath(targetDir, file.getName()), isFingerprintArtifacts());
cnt = list.length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
public class FilePathCopyMethod extends Copier {
/** @see FilePath#recursiveCopyTo(String,FilePath) */
@Override
public int copyAll(FilePath srcDir, String filter, FilePath targetDir, boolean fingerprintArtifacts)
public int copyAll(FilePath srcDir, String filter, String excludes, FilePath targetDir, boolean fingerprintArtifacts)
throws IOException, InterruptedException {
return srcDir.copyRecursiveTo(filter, targetDir);
return srcDir.copyRecursiveTo(filter, excludes, targetDir);
}

/** @see FilePath#copyTo(FilePath) */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ private MessageDigest newMD5() {
}

@Override
public int copyAll(FilePath srcDir, String filter, FilePath targetDir, boolean fingerprintArtifacts) throws IOException, InterruptedException {
public int copyAll(FilePath srcDir, String filter, String excludes, FilePath targetDir, boolean fingerprintArtifacts) throws IOException, InterruptedException {
targetDir.mkdirs(); // Create target if needed
FilePath[] list = srcDir.list(filter, null, false);
FilePath[] list = srcDir.list(filter, excludes, false);
for (FilePath file : list) {
String tail = file.getRemote().substring(srcDir.getRemote().length());
if (tail.startsWith("\\") || tail.startsWith("/"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ THE SOFTWARE.
<f:entry title="${%Artifacts to copy}" field="filter">
<f:textbox/>
</f:entry>
<f:entry title="${%Artifacts not to copy}" field="excludes">
<f:textbox/>
</f:entry>
<f:entry title="${%Target directory}" field="target">
<f:textbox/>
</f:entry>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Project\ name=\u30D7\u30ED\u30B8\u30A7\u30AF\u30C8
Which\ build=\u30D3\u30EB\u30C9
Artifacts\ to\ copy=\u30B3\u30D4\u30FC\u3059\u308B\u6210\u679C\u7269
Artifacts\ not\ to\ copy=\u30b3\u30d4\u30fc\u3057\u306a\u3044\u6210\u679c\u7269
Target\ directory=\u30B3\u30D4\u30FC\u5148\u30C7\u30A3\u30EC\u30AF\u30C8\u30EA
Flatten\ directories=\u30C7\u30A3\u30EC\u30AF\u30C8\u30EA\u69CB\u9020\u3092\u7121\u8996
Optional=\u30AA\u30D7\u30B7\u30E7\u30F3
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div>
Specify paths or patterns of artifacts to exclude, even if specified in "Artifacts to copy".
Can be blank.
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div>
「コピーする成果物」で指定したパターンに含まれるけれども除外したい成果物のパスやパターンを指定します。
必要ない場合は空のままでよいです。
</div>

0 comments on commit b605dc8

Please sign in to comment.