/* * The MIT License * * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package hudson.maven.reporters; import hudson.FilePath; import hudson.Util; import hudson.maven.MavenBuild; import hudson.maven.MavenBuildProxy; import hudson.model.BuildListener; import hudson.model.FingerprintMap; import hudson.model.Hudson; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.artifact.handler.ArtifactHandler; import org.apache.maven.artifact.handler.DefaultArtifactHandler; import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager; import com.google.common.collect.Maps; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.Serializable; import java.util.Map; import java.util.logging.Logger; /** * Captures information about an artifact created by Maven and archived by * Hudson, so that we can later deploy it to repositories of our choice. * *
* This object is created within the Maven process and sent back to the master, * so it shouldn't contain anything non-serializable as fields. * *
* Once it's constructed, the object should be considered final and immutable. * * @author Kohsuke Kawaguchi * @since 1.189 */ public final class MavenArtifact implements Serializable { /** * Basic parameters of a Maven artifact. */ public final String groupId, artifactId, version, classifier, type; /** * File name (without directory portion) of this artifact in the Hudson archive. * Remembered explicitly because some times this doesn't follow the * standard naming convention, due to <finalName> setting in POM. * *
* This name is taken directly from the name of the file as used during the build * (thus POM would be most likely just pom.xml and artifacts would * use their finalName if one is configured.) This is often * different from {@link #canonicalName}. */ public final String fileName; /** * The canonical artifact file name, used by Maven in the repository. * This is artifactId-version[-classifier].extension. * *
* The reason we persist this is that the extension is only available
* through {@link ArtifactHandler}.
*/
public final String canonicalName;
/**
* The md5sum for this artifact.
*/
public final String md5sum;
public MavenArtifact(Artifact a) throws IOException {
this.groupId = a.getGroupId();
this.artifactId = a.getArtifactId();
this.version = a.getVersion();
this.classifier = a.getClassifier();
this.type = a.getType();
// TODO: on archive we need to follow the same format as Maven repository
this.fileName = a.getFile().getName();
this.md5sum = Util.getDigestOf(new FileInputStream(a.getFile()));
String extension;
if(a.getArtifactHandler()!=null) // don't know if this can be null, but just to be defensive.
extension = a.getArtifactHandler().getExtension();
else
extension = a.getType();
canonicalName = getSeed(extension);
}
public MavenArtifact(String groupId, String artifactId, String version, String classifier, String type, String fileName, String md5sum) {
this.groupId = groupId;
this.artifactId = artifactId;
this.version = version;
this.classifier = classifier;
this.type = type;
this.fileName = fileName;
this.canonicalName = getSeed(type);
this.md5sum = md5sum;
}
/**
* Convenience method to check if the given {@link Artifact} object contains
* enough information suitable for recording, and if so, create {@link MavenArtifact}.
*/
public static MavenArtifact create(Artifact a) throws IOException {
File file = a.getFile();
if(file==null)
return null; // perhaps build failed and didn't leave an artifact
if(!file.exists() || file.isDirectory())
return null; // during a build maven sets a class folder instead of a jar file as artifact. ignore.
return new MavenArtifact(a);
}
public boolean isPOM() {
return fileName.endsWith(".pom"); // hack
}
/**
* Creates a Maven {@link Artifact} back from the persisted data.
*/
public Artifact toArtifact(ArtifactHandlerManager handlerManager, ArtifactFactory factory, MavenBuild build) throws IOException {
// Hack: presence of custom ArtifactHandler during builds could influence the file extension
// in the repository during deployment. So simulate that behavior if that's necessary.
final String canonicalExtension = canonicalName.substring(canonicalName.lastIndexOf('.')+1);
ArtifactHandler ah = handlerManager.getArtifactHandler(type);
Map