/* * 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.maven.*; import hudson.maven.RedeployPublisher.WrappedArtifactRepository; import hudson.model.Action; import hudson.model.TaskListener; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.deployer.ArtifactDeployer; import org.apache.maven.artifact.deployer.ArtifactDeploymentException; import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager; import org.apache.maven.artifact.installer.ArtifactInstallationException; import org.apache.maven.artifact.installer.ArtifactInstaller; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.project.artifact.ProjectArtifactMetadata; import org.codehaus.plexus.component.repository.exception.ComponentLookupException; import java.io.IOException; import java.io.PrintStream; import java.util.List; import java.util.Map; /** * {@link Action} that remembers {@link MavenArtifact artifact}s that are built. * * Defines the methods and UIs to do (delayed) deployment and installation. * * @author Kohsuke Kawaguchi * @see MavenArtifactArchiver */ public class MavenArtifactRecord extends MavenAbstractArtifactRecord implements AggregatableAction { /** * The build to which this record belongs. */ public final MavenBuild parent; /** * POM artifact. */ public final MavenArtifact pomArtifact; /** * The main artifact (like jar or war, but could be anything.) * * If this is a POM module, the main artifact contains the same value as {@link #pomArtifact}. */ public final MavenArtifact mainArtifact; /** * Attached artifacts. Can be empty but never null. */ public final List attachedArtifacts; /** * The repository identifier (matching maven settings) used for credentials to deploy artifacts */ public final String repositoryId; /** * The repository URL used for credentials to deploy artifacts */ public final String repositoryUrl; @Deprecated public MavenArtifactRecord(MavenBuild parent, MavenArtifact pomArtifact, MavenArtifact mainArtifact, List attachedArtifacts) { this(parent, pomArtifact, mainArtifact, attachedArtifacts, null, null); } public MavenArtifactRecord(MavenBuild parent, MavenArtifact pomArtifact, MavenArtifact mainArtifact, List attachedArtifacts, String repositoryUrl, String repositoryId) { assert parent != null; assert pomArtifact != null; assert attachedArtifacts != null; if (mainArtifact == null) mainArtifact = pomArtifact; this.parent = parent; this.pomArtifact = pomArtifact; this.mainArtifact = mainArtifact; this.attachedArtifacts = attachedArtifacts; this.repositoryUrl = repositoryUrl; this.repositoryId = repositoryId; } public MavenBuild getBuild() { return parent; } public boolean isPOM() { return mainArtifact.isPOM(); } public MavenAggregatedReport createAggregatedAction(MavenModuleSetBuild build, Map> moduleBuilds) { return new MavenAggregatedArtifactRecord(build); } @Override public void deploy(MavenEmbedder embedder, ArtifactRepository deploymentRepository, TaskListener listener) throws MavenEmbedderException, IOException, ComponentLookupException, ArtifactDeploymentException { ArtifactHandlerManager handlerManager = embedder.lookup(ArtifactHandlerManager.class); ArtifactFactory artifactFactory = embedder.lookup(ArtifactFactory.class); PrintStream logger = listener.getLogger(); boolean maven3orLater = MavenUtil.maven3orLater(parent.getModuleSetBuild().getMavenVersionUsed()); boolean uniqueVersion = true; if (!deploymentRepository.isUniqueVersion()) { if (maven3orLater) { logger.println("[ERROR] uniqueVersion == false is not anymore supported in maven 3"); } else { ((WrappedArtifactRepository) deploymentRepository).setUniqueVersion(false); uniqueVersion = false; } } else { ((WrappedArtifactRepository) deploymentRepository).setUniqueVersion(true); } Artifact main = mainArtifact.toArtifact(handlerManager, artifactFactory, parent); if (!isPOM()) main.addMetadata(new ProjectArtifactMetadata(main, pomArtifact.getFile(parent))); ArtifactDeployer deployer = embedder.lookup(ArtifactDeployer.class, uniqueVersion ? "default" : "maven2"); logger.println( "[INFO] Deployment in " + deploymentRepository.getUrl() + " (id=" + deploymentRepository.getId() + ",uniqueVersion=" + deploymentRepository.isUniqueVersion()+")"); // deploy the main artifact. This also deploys the POM logger.println(Messages.MavenArtifact_DeployingMainArtifact(main.getFile().getName())); deployer.deploy(main.getFile(), main, deploymentRepository, embedder.getLocalRepository()); for (MavenArtifact aa : attachedArtifacts) { Artifact a = aa.toArtifact(handlerManager, artifactFactory, parent); logger.println(Messages.MavenArtifact_DeployingMainArtifact(a.getFile().getName())); deployer.deploy(a.getFile(), a, deploymentRepository, embedder.getLocalRepository()); } } /** * Installs the artifact to the local Maven repository. */ public void install(MavenEmbedder embedder) throws MavenEmbedderException, IOException, ComponentLookupException, ArtifactInstallationException { ArtifactHandlerManager handlerManager = embedder.lookup(ArtifactHandlerManager.class); ArtifactInstaller installer = embedder.lookup(ArtifactInstaller.class); ArtifactFactory factory = embedder.lookup(ArtifactFactory.class); Artifact main = mainArtifact.toArtifact(handlerManager,factory,parent); if(!isPOM()) main.addMetadata(new ProjectArtifactMetadata(main,pomArtifact.getFile(parent))); installer.install(mainArtifact.getFile(parent),main,embedder.getLocalRepository()); for (MavenArtifact aa : attachedArtifacts) installer.install(aa.getFile(parent),aa.toArtifact(handlerManager,factory,parent),embedder.getLocalRepository()); } public void recordFingerprints() throws IOException { // record fingerprints if(mainArtifact!=null) mainArtifact.recordFingerprint(parent); for (MavenArtifact a : attachedArtifacts) a.recordFingerprint(parent); } }