Skip to content

Commit fbdcaa9

Browse files
authored
Merge pull request #234 from jglick/matrix-project
Make dep on `matrix-project` optional
2 parents fdd4cc7 + 3f8a452 commit fbdcaa9

File tree

3 files changed

+77
-8
lines changed

3 files changed

+77
-8
lines changed

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@
7575
<artifactId>jenkins-test-harness-tools</artifactId>
7676
<version>2.2</version>
7777
<scope>test</scope>
78+
<exclusions>
79+
<exclusion>
80+
<groupId>org.jenkins-ci.plugins</groupId>
81+
<artifactId>gradle</artifactId>
82+
</exclusion>
83+
</exclusions>
7884
</dependency>
7985
<dependency>
8086
<groupId>org.jenkins-ci.plugins</groupId>
@@ -84,6 +90,7 @@
8490
<dependency>
8591
<groupId>org.jenkins-ci.plugins</groupId>
8692
<artifactId>matrix-project</artifactId>
93+
<optional>true</optional>
8794
</dependency>
8895
<dependency>
8996
<groupId>org.jenkins-ci.plugins.workflow</groupId>

src/main/java/hudson/plugins/copyartifact/CopyArtifact.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,14 @@ private static synchronized void setUpgradeNeeded() {
286286

287287
// get all CopyArtifacts configured to AbstractProject. This works both for Project and MatrixProject.
288288
private static List<CopyArtifact> getCopyArtifactsInProject(AbstractProject<?,?> project) {
289-
DescribableList<Builder,Descriptor<Builder>> list =
290-
project instanceof Project ? ((Project<?,?>)project).getBuildersList()
291-
: (project instanceof MatrixProject ?
292-
((MatrixProject)project).getBuildersList() : null);
289+
DescribableList<Builder,Descriptor<Builder>> list;
290+
if (project instanceof Project) {
291+
list = ((Project<?,?>)project).getBuildersList();
292+
} else if (Jenkins.get().getPlugin("matrix-project") != null && project instanceof MatrixProject) {
293+
list = ((MatrixProject)project).getBuildersList();
294+
} else {
295+
list = null;
296+
}
293297
if (list == null) {
294298
return Collections.emptyList();
295299
}
@@ -543,7 +547,7 @@ public void perform(@NonNull Run<?, ?> build, @NonNull FilePath workspace, @NonN
543547
if (!ok) {
544548
throw new AbortException(Messages.CopyArtifact_FailedToCopy(expandedProject, expandedFilter));
545549
}
546-
} else if (src instanceof MatrixBuild) {
550+
} else if (jenkins.getPlugin("matrix-project") != null && src instanceof MatrixBuild) {
547551
boolean ok = false;
548552
// Copy artifacts from all configurations of this matrix build
549553
// Use MatrixBuild.getExactRuns if available
@@ -854,10 +858,10 @@ public FormValidation doCheckProjectName(
854858
if (item != null) {
855859
if (jenkins.getPlugin("maven-plugin") != null && item instanceof MavenModuleSet) {
856860
result = FormValidation.warning(Messages.CopyArtifact_MavenProject());
861+
} else if (jenkins.getPlugin("matrix-project") != null && item instanceof MatrixProject) {
862+
result = FormValidation.warning(Messages.CopyArtifact_MatrixProject());
857863
} else {
858-
result = (item instanceof MatrixProject)
859-
? FormValidation.warning(Messages.CopyArtifact_MatrixProject())
860-
: FormValidation.ok();
864+
result = FormValidation.ok();
861865
}
862866
} else if (value.indexOf('$') >= 0) {
863867
result = FormValidation.warning(Messages.CopyArtifact_ParameterizedName());
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright 2024 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+
25+
package hudson.plugins.copyartifact;
26+
27+
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
28+
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
29+
import org.junit.Rule;
30+
import org.junit.Test;
31+
import org.jvnet.hudson.test.JenkinsRule;
32+
import org.jvnet.hudson.test.RealJenkinsRule;
33+
import org.jvnet.hudson.test.TailLog;
34+
35+
/** Verifies that basic functionality works without optional plugin dependencies. */
36+
public final class OptionalDepsTest {
37+
38+
@Rule public RealJenkinsRule r = new RealJenkinsRule().omitPlugins("maven-plugin", "matrix-project");
39+
40+
@Test public void usePipeline() throws Throwable {
41+
r.then(OptionalDepsTest::_usePipeline);
42+
}
43+
44+
/** Adapted from {@link CopyArtifactWorkflowTest#testLastCompletedBuildSelector}. */
45+
private static void _usePipeline(JenkinsRule r) throws Throwable {
46+
var upstream = r.createProject(WorkflowJob.class, "upstream");
47+
upstream.setDefinition(new CpsFlowDefinition("node {writeFile text: 'upstream content', file: 'x.txt'; archiveArtifacts 'x.txt'}", true));
48+
try (var tail = new TailLog(r, "upstream", 1)) {
49+
r.buildAndAssertSuccess(upstream);
50+
}
51+
var downstream = r.createProject(WorkflowJob.class, "downstream");
52+
downstream.setDefinition(new CpsFlowDefinition("node {copyArtifacts(projectName: 'upstream', selector: lastCompleted()); echo readFile('x.txt')}", true));
53+
try (var tail = new TailLog(r, "downstream", 1)) {
54+
r.assertLogContains("upstream content", r.buildAndAssertSuccess(downstream));
55+
}
56+
}
57+
58+
}

0 commit comments

Comments
 (0)