|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2016, 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 org.jenkinsci.plugins.pipeline.maven; |
| 26 | + |
| 27 | +import org.jenkinsci.lib.configprovider.ConfigProvider; |
| 28 | +import org.jenkinsci.lib.configprovider.model.Config; |
| 29 | +import org.jenkinsci.plugins.configfiles.maven.MavenSettingsConfig.MavenSettingsConfigProvider; |
| 30 | +import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl; |
| 31 | +import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl; |
| 32 | +import org.kohsuke.stapler.DataBoundConstructor; |
| 33 | +import org.kohsuke.stapler.DataBoundSetter; |
| 34 | + |
| 35 | +import hudson.Extension; |
| 36 | +import hudson.ExtensionList; |
| 37 | +import hudson.model.JDK; |
| 38 | +import hudson.tasks.Maven; |
| 39 | +import hudson.tasks.Maven.MavenInstallation; |
| 40 | +import hudson.util.ListBoxModel; |
| 41 | +import jenkins.model.Jenkins; |
| 42 | +import jenkins.mvn.GlobalMavenConfig; |
| 43 | +import jenkins.mvn.SettingsProvider; |
| 44 | + |
| 45 | +/** |
| 46 | + * Configures maven environment to use within a pipeline job by calling <tt>sh mvn</tt> or <tt>bat mvn</tt>. |
| 47 | + * The selected maven installation will be configured and prepended to the path. |
| 48 | + * |
| 49 | + */ |
| 50 | +public class WithMavenStep extends AbstractStepImpl { |
| 51 | + |
| 52 | + |
| 53 | + private String mavenSettingsConfig; |
| 54 | + private String mavenSettingsFilePath; |
| 55 | + private String mavenInstallation; |
| 56 | + private String mavenOpts; |
| 57 | + private String jdk; |
| 58 | + private String mavenLocalRepo; |
| 59 | + |
| 60 | + @DataBoundConstructor |
| 61 | + public WithMavenStep() { |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + public String getMavenSettingsConfig() { |
| 66 | + return mavenSettingsConfig; |
| 67 | + } |
| 68 | + |
| 69 | + @DataBoundSetter |
| 70 | + public void setMavenSettingsConfig(String mavenSettingsConfig) { |
| 71 | + this.mavenSettingsConfig = mavenSettingsConfig; |
| 72 | + } |
| 73 | + |
| 74 | + public String getMavenSettingsFilePath() { |
| 75 | + return mavenSettingsFilePath; |
| 76 | + } |
| 77 | + |
| 78 | + @DataBoundSetter |
| 79 | + public void setMavenSettingsFilePath(String mavenSettingsFilePath) { |
| 80 | + this.mavenSettingsFilePath = mavenSettingsFilePath; |
| 81 | + } |
| 82 | + |
| 83 | + public String getMavenInstallation() { |
| 84 | + return mavenInstallation; |
| 85 | + } |
| 86 | + |
| 87 | + @DataBoundSetter |
| 88 | + public void setMavenInstallation(String mavenInstallation) { |
| 89 | + this.mavenInstallation = mavenInstallation; |
| 90 | + } |
| 91 | + |
| 92 | + public String getMavenOpts() { |
| 93 | + return mavenOpts; |
| 94 | + } |
| 95 | + |
| 96 | + @DataBoundSetter |
| 97 | + public void setMavenOpts(String mavenOpts) { |
| 98 | + this.mavenOpts = mavenOpts; |
| 99 | + } |
| 100 | + |
| 101 | + public String getJdk() { |
| 102 | + return jdk; |
| 103 | + } |
| 104 | + |
| 105 | + @DataBoundSetter |
| 106 | + public void setJdk(String jdk) { |
| 107 | + this.jdk = jdk; |
| 108 | + } |
| 109 | + |
| 110 | + public String getMavenLocalRepo() { |
| 111 | + return mavenLocalRepo; |
| 112 | + } |
| 113 | + |
| 114 | + @DataBoundSetter |
| 115 | + public void setMavenLocalRepo(String mavenLocalRepo) { |
| 116 | + this.mavenLocalRepo = mavenLocalRepo; |
| 117 | + } |
| 118 | + |
| 119 | + @Extension |
| 120 | + public static class DescriptorImpl extends AbstractStepDescriptorImpl { |
| 121 | + |
| 122 | + public DescriptorImpl() { |
| 123 | + super(WithMavenStepExecution.class); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public String getFunctionName() { |
| 128 | + return "withMaven"; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public String getDisplayName() { |
| 133 | + return "Provide Maven environment"; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public boolean takesImplicitBlockArgument() { |
| 138 | + return true; |
| 139 | + } |
| 140 | + |
| 141 | + public SettingsProvider getDefaultSettingsProvider() { |
| 142 | + return GlobalMavenConfig.get().getSettingsProvider(); |
| 143 | + } |
| 144 | + |
| 145 | + private Maven.DescriptorImpl getMavenDescriptor() { |
| 146 | + return Jenkins.getInstance().getDescriptorByType(Maven.DescriptorImpl.class); |
| 147 | + } |
| 148 | + |
| 149 | + public ListBoxModel doFillMavenInstallationItems() { |
| 150 | + ListBoxModel r = new ListBoxModel(); |
| 151 | + r.add("--- Use system default Maven ---",null); |
| 152 | + for (MavenInstallation installation : getMavenDescriptor().getInstallations()) { |
| 153 | + r.add(installation.getName()); |
| 154 | + } |
| 155 | + return r; |
| 156 | + } |
| 157 | + |
| 158 | + private JDK.DescriptorImpl getJDKDescriptor() { |
| 159 | + return Jenkins.getInstance().getDescriptorByType(JDK.DescriptorImpl.class); |
| 160 | + } |
| 161 | + |
| 162 | + public ListBoxModel doFillJdkItems() { |
| 163 | + ListBoxModel r = new ListBoxModel(); |
| 164 | + r.add("--- Use system default JDK ---",null); |
| 165 | + for (JDK installation : getJDKDescriptor().getInstallations()) { |
| 166 | + r.add(installation.getName()); |
| 167 | + } |
| 168 | + return r; |
| 169 | + } |
| 170 | + |
| 171 | + public ListBoxModel doFillMavenSettingsConfigItems() { |
| 172 | + ExtensionList<MavenSettingsConfigProvider> providers = Jenkins.getInstance().getExtensionList(MavenSettingsConfigProvider.class); |
| 173 | + ListBoxModel r = new ListBoxModel(); |
| 174 | + r.add("--- Use system default settings or file path ---",null); |
| 175 | + for (ConfigProvider provider : providers) { |
| 176 | + for(Config config:provider.getAllConfigs()){ |
| 177 | + r.add(config.name, config.id); |
| 178 | + } |
| 179 | + } |
| 180 | + return r; |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments