Skip to content

Commit

Permalink
[FIXED JENKINS-12834] use file pattern to disambiguate multiple war
Browse files Browse the repository at this point in the history
  • Loading branch information
ndeloof committed Feb 20, 2012
1 parent 465aeb4 commit 9121f32
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
45 changes: 36 additions & 9 deletions src/main/java/org/jenkins/plugins/cloudbees/CloudbeesDeployer.java
Expand Up @@ -3,21 +3,25 @@
import com.cloudbees.api.UploadProgress;
import hudson.FilePath;
import hudson.Launcher;
import hudson.console.ConsoleNote;
import hudson.maven.MavenBuild;
import hudson.maven.MavenModuleSetBuild;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.Hudson;
import hudson.model.Node;
import hudson.tasks._maven.MavenErrorNote;
import hudson.util.IOException2;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.tools.ant.types.selectors.SelectorUtils;
import org.jenkins.plugins.cloudbees.util.FileFinder;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class CloudbeesDeployer {
Expand All @@ -29,7 +33,7 @@ public class CloudbeesDeployer {
public final String filePattern;

public final CloudbeesAccount cloudbeesAccount;

public CloudbeesDeployer(CloudbeesPublisher cloudbeesPublisher) {
this.accountName = cloudbeesPublisher.accountName;
this.applicationId = cloudbeesPublisher.applicationId;
Expand All @@ -41,7 +45,7 @@ public boolean deploy(AbstractBuild<?, ?> build, Launcher launcher, final BuildL
throws InterruptedException, IOException {

if (cloudbeesAccount == null) {
listener.getLogger().println(Messages._CloudbeesPublisher_noAccount());
listener.error(Messages.CloudbeesPublisher_noAccount());
return false;
}

Expand All @@ -56,38 +60,61 @@ public boolean deploy(AbstractBuild<?, ?> build, Launcher launcher, final BuildL


String warPath = null;
findWarPath:
List<String> candidates = new ArrayList<String>();
for (ArtifactFilePathSaveAction artifactFilePathSaveAction : artifactFilePathSaveActions) {
for (MavenArtifactWithFilePath artifactWithFilePath : artifactFilePathSaveAction.mavenArtifactWithFilePaths) {
if (StringUtils.equals("war", artifactWithFilePath.type)) {
listener.getLogger().println(Messages.CloudbeesPublisher_WarPathFound(artifactWithFilePath));
warPath = artifactWithFilePath.filePath;
break findWarPath;
candidates.add(artifactWithFilePath.filePath);
}
}
}

if (candidates.size() > 1) {
if (StringUtils.isBlank(filePattern)) {
listener.error(Messages.CloudbeesDeployer_AmbiguousMavenWarArtifact());
return false;
}
Iterator<String> it= candidates.iterator();
while (it.hasNext()) {
if (SelectorUtils.match(filePattern, it.next())) continue;
it.remove();
}

if (candidates.size() > 1) {
listener.error(Messages.CloudbeesDeployer_StillAmbiguousMavenWarArtifact());
return false;
}

if (candidates.size() == 0) {
listener.error(Messages.CloudbeesDeployer_NoWarArtifactToMatchPattern());
return false;
}

warPath = candidates.get(0);
}

if (StringUtils.isBlank(warPath)) {
if (StringUtils.isBlank(filePattern)) {
listener.getLogger().println(Messages._CloudbeesPublisher_noWarArtifacts());
listener.error(Messages.CloudbeesPublisher_noWarArtifacts());
return false;
} else {
//search file in the workspace with the pattern
FileFinder fileFinder = new FileFinder(filePattern);
List<String> fileNames = build.getWorkspace().act(fileFinder);
listener.getLogger().println("found remote files : " + fileNames);
if (fileNames.size() > 1) {
listener.getLogger().println(Messages.CloudbeesPublisher_ToManyFilesMatchingPattern());
listener.error(Messages.CloudbeesPublisher_ToManyFilesMatchingPattern());
return false;
} else if (fileNames.size() == 0) {
listener.getLogger().println(Messages._CloudbeesPublisher_noArtifactsFound(filePattern));
listener.error(Messages.CloudbeesPublisher_noArtifactsFound(filePattern));
return false;
}
// so we use only the first found
warPath = fileNames.get(0);
}
}

listener.getLogger().println(Messages.CloudbeesPublisher_WarPathFound(warPath));
doDeploy(build, listener, warPath);

return true;
Expand Down
Expand Up @@ -13,3 +13,6 @@ CloudbeesPublisher.noAccount = There is no CloudBees account configured. Please
CloudbeesPublisher.WarPathFound = Deployable artifact found {0}
CloudbeesPublisher.ToManyFilesMatchingPattern = Your pattern must return only one file to deploy
CloudbeesPublisher.Deploying=Deploying archive to cloudbees application {0}
CloudbeesDeployer.AmbiguousMavenWarArtifact=Maven build generated multiple WAR artifacts, provide a filePattern to disambiguate
CloudbeesDeployer.NoWarArtifactToMatchPattern=filePattern did not select any WAR artifacts from produced artifacts
CloudbeesDeployer.StillAmbiguousMavenWarArtifact=filePattern was not strong enough to disambiguate multiple WAR artifacts

0 comments on commit 9121f32

Please sign in to comment.