File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\maven-plugin\src\main\java\hudson\maven\reporters\MavenArtifactRecord.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, CloudBees, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.maven.reporters;
25
26 import hudson.maven.*;
27 import hudson.maven.RedeployPublisher.WrappedArtifactRepository;
28 import hudson.model.AbstractItem;
29 import hudson.model.Action;
30 import hudson.model.TaskListener;
31
32 import java.io.IOException;
33 import java.io.PrintStream;
34 import java.util.List;
35 import java.util.Map;
36
37 import org.apache.maven.artifact.Artifact;
38 import org.apache.maven.artifact.deployer.ArtifactDeployer;
39 import org.apache.maven.artifact.deployer.ArtifactDeploymentException;
40 import org.apache.maven.artifact.factory.ArtifactFactory;
41 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
42 import org.apache.maven.artifact.installer.ArtifactInstallationException;
43 import org.apache.maven.artifact.installer.ArtifactInstaller;
44 import org.apache.maven.artifact.repository.ArtifactRepository;
45 import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
46 import org.apache.maven.plugin.descriptor.PluginDescriptor;
47 import org.apache.maven.project.artifact.ProjectArtifactMetadata;
48 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
49 import org.kohsuke.stapler.export.Exported;
50 import org.kohsuke.stapler.export.ExportedBean;
51
52 /**
53 * {@link Action} that remembers {@link MavenArtifact artifact}s that are built.
54 *
55 * Defines the methods and UIs to do (delayed) deployment and installation.
56 *
57 * @author Kohsuke Kawaguchi
58 * @see MavenArtifactArchiver
59 */
60 @ExportedBean
61 public class MavenArtifactRecord extends MavenAbstractArtifactRecord<MavenBuild> implements AggregatableAction {
62 /**
63 * The build to which this record belongs.
64 */
65 @Exported
66 public final MavenBuild parent;
67
68 /**
69 * POM artifact.
70 */
71 @Exported(inline=true)
72 public final MavenArtifact pomArtifact;
73
74 /**
75 * The main artifact (like jar or war, but could be anything.)
76 *
77 * If this is a POM module, the main artifact contains the same value as {@link #pomArtifact}.
78 */
79 @Exported(inline=true)
80 public final MavenArtifact mainArtifact;
81
82 /**
83 * Attached artifacts. Can be empty but never null.
84 */
85 @Exported(inline=true)
86 public final List<MavenArtifact> attachedArtifacts;
87
88 /**
89 * The repository identifier (matching maven settings) used for credentials to deploy artifacts
90 */
91 public final String repositoryId;
92
93 /**
94 * The repository URL used for credentials to deploy artifacts
95 */
96 public final String repositoryUrl;
97
98 @Deprecated
99 public MavenArtifactRecord(MavenBuild parent, MavenArtifact pomArtifact, MavenArtifact mainArtifact, List<MavenArtifact> attachedArtifacts) {
100 this(parent, pomArtifact, mainArtifact, attachedArtifacts, null, null);
101 }
102
103 public MavenArtifactRecord(MavenBuild parent, MavenArtifact pomArtifact, MavenArtifact mainArtifact,
104 List<MavenArtifact> attachedArtifacts, String repositoryUrl, String repositoryId) {
105 assert parent != null;
106 assert pomArtifact != null;
107 assert attachedArtifacts != null;
108 if (mainArtifact == null) mainArtifact = pomArtifact;
109
110 this.parent = parent;
111 this.pomArtifact = pomArtifact;
112 this.mainArtifact = mainArtifact;
113 this.attachedArtifacts = attachedArtifacts;
114 this.repositoryUrl = repositoryUrl;
115 this.repositoryId = repositoryId;
116 }
117
118 public MavenBuild getBuild() {
119 return parent;
120 }
121
122 /**
123 * Returns the URL of this record relative to the context root of the application.
124 *
125 * @see AbstractItem#getUrl() for how to implement this.
126 *
127 * @return
128 * URL that ends with '/'.
129 */
130 public String getUrl() {
131 return parent.getUrl()+"mavenArtifacts/";
132 }
133
134 /**
135 * Obtains the absolute URL to this build.
136 *
137 * @deprecated
138 * This method shall <b>NEVER</b> be used during HTML page rendering, as it's too easy for
139 * misconfiguration to break this value, with network set up like Apache reverse proxy.
140 * This method is only intended for the remote API clients who cannot resolve relative references.
141 */
142 @Exported(visibility=2,name="url")
143 public String getAbsoluteUrl() {
144 return parent.getAbsoluteUrl()+"mavenArtifacts/";
145 }
146
147 public boolean isPOM() {
148 return mainArtifact.isPOM();
149 }
150
151 public MavenAggregatedArtifactRecord createAggregatedAction(MavenModuleSetBuild build, Map<MavenModule, List<MavenBuild>> moduleBuilds) {
152 return new MavenAggregatedArtifactRecord(build);
153 }
154
155 @Override
156 public void deploy(MavenEmbedder embedder, ArtifactRepository deploymentRepository, TaskListener listener) throws MavenEmbedderException, IOException, ComponentLookupException, ArtifactDeploymentException {
157 ArtifactHandlerManager handlerManager = embedder.lookup(ArtifactHandlerManager.class);
158
159 ArtifactFactory artifactFactory = embedder.lookup(ArtifactFactory.class);
160 PrintStream logger = listener.getLogger();
161 boolean maven3orLater = MavenUtil.maven3orLater(parent.getModuleSetBuild().getMavenVersionUsed());
162 boolean uniqueVersion = true;
163 if (!deploymentRepository.isUniqueVersion()) {
164 if (maven3orLater) {
165 logger.println("[ERROR] uniqueVersion == false is not anymore supported in maven 3");
166 } else {
167 ((WrappedArtifactRepository) deploymentRepository).setUniqueVersion(false);
168 uniqueVersion = false;
169 }
170 } else {
171 ((WrappedArtifactRepository) deploymentRepository).setUniqueVersion(true);
172 }
173 Artifact main = mainArtifact.toArtifact(handlerManager, artifactFactory, parent);
174 if (!isPOM())
175 main.addMetadata(new ProjectArtifactMetadata(main, pomArtifact.getFile(parent)));
176
177 if (main.getType().equals("maven-plugin")) {
178 GroupRepositoryMetadata metadata = new GroupRepositoryMetadata(main.getGroupId());
179 String goalPrefix = PluginDescriptor.getGoalPrefixFromArtifactId(main.getArtifactId());
180 metadata.addPluginMapping(goalPrefix, main.getArtifactId(), null);
181 main.addMetadata(metadata);
182 }
183
184 ArtifactDeployer deployer = embedder.lookup(ArtifactDeployer.class, uniqueVersion ? "default" : "maven2");
185 logger.println(
186 "[INFO] Deployment in " + deploymentRepository.getUrl() + " (id=" + deploymentRepository.getId() + ",uniqueVersion=" + deploymentRepository.isUniqueVersion()+")");
187
188 // deploy the main artifact. This also deploys the POM
189 logger.println(Messages.MavenArtifact_DeployingMainArtifact(main.getFile().getName()));
190 deployer.deploy(main.getFile(), main, deploymentRepository, embedder.getLocalRepository());
191
192 for (MavenArtifact aa : attachedArtifacts) {
193 Artifact a = aa.toArtifact(handlerManager, artifactFactory, parent);
194 logger.println(Messages.MavenArtifact_DeployingMainArtifact(a.getFile().getName()));
195 deployer.deploy(a.getFile(), a, deploymentRepository, embedder.getLocalRepository());
196 }
197 }
198
199 /**
200 * Installs the artifact to the local Maven repository.
201 */
202 public void install(MavenEmbedder embedder) throws MavenEmbedderException, IOException, ComponentLookupException, ArtifactInstallationException {
203 ArtifactHandlerManager handlerManager = embedder.lookup(ArtifactHandlerManager.class);
204 ArtifactInstaller installer = embedder.lookup(ArtifactInstaller.class);
205 ArtifactFactory factory = embedder.lookup(ArtifactFactory.class);
206
207 Artifact main = mainArtifact.toArtifact(handlerManager,factory,parent);
208 if(!isPOM())
209 main.addMetadata(new ProjectArtifactMetadata(main,pomArtifact.getFile(parent)));
210 installer.install(mainArtifact.getFile(parent),main,embedder.getLocalRepository());
211
212 for (MavenArtifact aa : attachedArtifacts)
213 installer.install(aa.getFile(parent), aa.toArtifact(handlerManager, factory, parent), embedder.getLocalRepository());
214 }
215
216 public void recordFingerprints() throws IOException {
217 // record fingerprints
218 if(mainArtifact!=null)
219 mainArtifact.recordFingerprint(parent);
220 for (MavenArtifact a : attachedArtifacts)
221 a.recordFingerprint(parent);
222 }
223 }
224