Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FIXED JENKINS-13533] Maven build fails on CleanTempFilesAction#tempF…
…iles serialization
  • Loading branch information
imod committed Apr 27, 2012
1 parent 1de482f commit 0e8baa3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
@@ -1,9 +1,8 @@
package org.jenkinsci.plugins.configfiles.buildwrapper;

import hudson.FilePath;
import hudson.model.InvisibleAction;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
Expand All @@ -14,13 +13,19 @@
*/
public class CleanTempFilesAction extends InvisibleAction {

private final transient List<String> tempFiles;

public CleanTempFilesAction(List<String> tempFiles) {
this.tempFiles = tempFiles;
}

/**
* list of the temp files to be removed - never <code>null</code>
* List of the temp files to be removed - never <code>null</code>.
*
* @return list of temp files
*/
public List<FilePath> tempFiles;

public CleanTempFilesAction(List<FilePath> tempFiles) {
this.tempFiles = tempFiles == null ? new ArrayList<FilePath>() : tempFiles;
public List<String> getTempFiles() {
return tempFiles == null ? Collections.<String> emptyList() : tempFiles;
}

}
Expand Up @@ -7,7 +7,11 @@
import hudson.FilePath;
import hudson.model.TaskListener;
import hudson.model.AbstractBuild;
import hudson.model.Node;
import hudson.model.listeners.RunListener;
import hudson.remoting.VirtualChannel;

import java.util.List;

/**
* Removes the temporarily created files at 'onComplete()' of each build, doing it at this state, ensures the files are available also for publishers.
Expand All @@ -19,23 +23,31 @@ public class CleanTempFilesRunListener extends RunListener<AbstractBuild<?, ?>>

@Override
public void onCompleted(AbstractBuild<?, ?> build, TaskListener listener) {
CleanTempFilesAction action = build.getAction(CleanTempFilesAction.class);
if (action != null) {

final List<CleanTempFilesAction> actions = build.getActions(CleanTempFilesAction.class);
for (CleanTempFilesAction action : actions) {
try {
for (FilePath fp : action.tempFiles) {
for (String remotePath : action.getTempFiles()) {
try {
if (fp != null && fp.exists()) {
fp.delete();
final Node builtOn = build.getBuiltOn();
if (builtOn != null) {
final VirtualChannel channel = builtOn.getChannel();
if (channel != null) {
FilePath fp = new FilePath(channel, remotePath);
if (fp.exists()) {
fp.delete();
}
}
}
} catch (Exception e) {
listener.getLogger().println("[WARN] failed to delete temp file: " + fp.getRemote());
listener.getLogger().println("[WARN] failed to delete temp file: " + remotePath + " - " + e.getMessage());
}
}
} finally {
// remove the action, there is nothing we want to persist on the build
build.getActions().remove(action);
}
}
}

}
}
Expand Up @@ -37,7 +37,7 @@ of this software and associated documentation files (the "Software"), to deal
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -104,12 +104,11 @@ public class ManagedFilesEnvironment extends Environment {
private final Map<ManagedFile, FilePath> file2Path;

public ManagedFilesEnvironment(Map<ManagedFile, FilePath> file2Path) {
this.file2Path = file2Path == null ? new HashMap<ManagedFile, FilePath>() : file2Path;
this.file2Path = file2Path == null ? Collections.<ManagedFile, FilePath> emptyMap() : file2Path;
}

@Override
public void buildEnvVars(Map<String, String> env) {
System.out.println("ConfigFileBuildWrapper.ManagedFilesEnvironment.buildEnvVars()");
for (Map.Entry<ManagedFile, FilePath> entry : file2Path.entrySet()) {
ManagedFile mf = entry.getKey();
FilePath fp = entry.getValue();
Expand All @@ -122,14 +121,14 @@ public void buildEnvVars(Map<String, String> env) {
/**
* Provides access to the files which have to be removed after the build
*
* @return a list of temp files
* @return a list of paths to the temp files (remotes)
*/
public List<FilePath> getTempFiles() {
List<FilePath> tempFiles = new ArrayList<FilePath>();
List<String> getTempFiles() {
List<String> tempFiles = new ArrayList<String>();
for (Entry<ManagedFile, FilePath> entry : file2Path.entrySet()) {
boolean noTargetGiven = StringUtils.isBlank(entry.getKey().targetLocation);
if (noTargetGiven) {
tempFiles.add(entry.getValue());
tempFiles.add(entry.getValue().getRemote());
}
}
return tempFiles;
Expand Down

0 comments on commit 0e8baa3

Please sign in to comment.