Debian Codegraph @

jenkins_jobs/modules/project_maven.py

raw
1 # Copyright 2012 Hewlett-Packard Development Company, L.P.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15
16 """
17 The Maven Project module handles creating Maven Jenkins projects.
18
19 To create a Maven project, specify ``maven`` in the ``project-type``
20 attribute to the :ref:`Job` definition. It also requires a ``maven`` section
21 in the :ref:`Job` definition.
22
23 :Job Parameters:
24 * **root-module**:
25 * **group-id** (`str`): GroupId.
26 * **artifact-id** (`str`): ArtifactId.
27 * **root-pom** (`str`): The path to the pom.xml file. (default 'pom.xml')
28 * **goals** (`str`): Goals to execute. (required)
29 * **maven-opts** (`str`): Java options to pass to maven (aka MAVEN_OPTS)
30 * **maven-name** (`str`): Installation of maven which should be used.
31 Not setting ``maven-name`` appears to use the first maven install
32 defined in the global jenkins config.
33 * **private-repository** ('str'): Whether to use a private maven repository
34 Possible values are `default`, `local-to-workspace` and
35 `local-to-executor`.
36 * **ignore-upstream-changes** (`bool`): Do not start a build whenever
37 a SNAPSHOT dependency is built or not. (default true)
38 * **incremental-build** (`bool`): Activate incremental build - only build
39 changed modules (default false).
40 * **automatic-archiving** (`bool`): Activate automatic artifact archiving
41 (default true).
42 * **automatic-site-archiving** (`bool`): Activate automatic site
43 documentation artifact archiving (default true).
44 * **automatic-fingerprinting** (`bool`): Activate automatic fingerprinting
45 of consumed and produced artifacts (default true).
46 * **per-module-email** (`bool`): Send an e-mail for each failed module
47 (default true).
48 * **parallel-build-modules** (`bool`): Build modules in parallel
49 (default false)
50 * **resolve-dependencies** (`bool`): Resolve Dependencies during Pom
51 parsing (default false).
52 * **run-headless** (`bool`): Run headless (default false).
53 * **disable-downstream** (`bool`): Disable triggering of downstream
54 projects (default false).
55 * **process-plugins** (`bool`): Process Plugins during Pom parsing
56 (default false).
57 * **custom-workspace** (`str`): Path to the custom workspace. If no path is
58 provided, custom workspace is not used. (optional)
59 * **settings** (`str`): Path to custom maven settings file. If settings
60 type is 'file' then this is a Path. Otherwise it is the id for
61 ConfigFileProvider. (optional)
62 * **settings-type** (`str`): Type of settings file file|cfp.
63 (default file)
64 * **global-settings** (`str`): Path to custom maven global settings file.
65 If settings type is 'file' then this is a Path. Otherwise it is the id
66 for ConfigFileProvider. (optional)
67 * **global-settings-type** (`str`): Type of settings file file|cfp.
68 (default file)
69 * **post-step-run-condition** (`str`): Run the post-build steps only if the
70 build succeeds ('SUCCESS'), build succeeds or is unstable ('UNSTABLE'),
71 regardless of build result ('FAILURE'). (default 'FAILURE').
72
73 Requires the Jenkins :jenkins-plugins:`Config File Provider Plugin
74 <config-file-provider>`
75 for the Config File Provider "settings" and "global-settings" config.
76
77 Example:
78
79 .. literalinclude:: /../../tests/general/fixtures/project-maven001.yaml
80
81 CFP Example:
82
83 .. literalinclude:: /../../tests/general/fixtures/project-maven003.yaml
84 """
85 import xml.etree.ElementTree as XML
86
87 from jenkins_jobs.errors import InvalidAttributeError
88 import jenkins_jobs.modules.base
89 import jenkins_jobs.modules.helpers as helpers
90 from jenkins_jobs.modules import hudson_model
91
92
93 class Maven(jenkins_jobs.modules.base.Base):
94 sequence = 0
95
96 choices_private_repo = {
97 "default": "hudson.maven.local_repo.DefaultLocalRepositoryLocator",
98 "local-to-workspace": "hudson.maven.local_repo.PerJobLocalRepositoryLocator",
99 "local-to-executor": "hudson.maven.local_repo.PerExecutorLocalRepositoryLocator",
100 }
101
102 def root_xml(self, data):
103 xml_parent = XML.Element("maven2-moduleset")
104 if "maven" not in data:
105 return xml_parent
106
107 # determine version of plugin
108 plugin_ver = self.registry.get_plugin_version("Maven Integration plugin")
109
110 if "root-module" in data["maven"]:
111 root_module = XML.SubElement(xml_parent, "rootModule")
112 XML.SubElement(root_module, "groupId").text = data["maven"]["root-module"][
113 "group-id"
114 ]
115 XML.SubElement(root_module, "artifactId").text = data["maven"][
116 "root-module"
117 ]["artifact-id"]
118 XML.SubElement(xml_parent, "goals").text = data["maven"]["goals"]
119
120 maven_opts = data["maven"].get("maven-opts")
121 if maven_opts:
122 XML.SubElement(xml_parent, "mavenOpts").text = maven_opts
123
124 maven_name = data["maven"].get("maven-name")
125 if maven_name:
126 XML.SubElement(xml_parent, "mavenName").text = maven_name
127
128 private_repo = data["maven"].get("private-repository")
129 if private_repo:
130 if private_repo not in self.choices_private_repo.keys():
131 raise ValueError(
132 'Not a valid private-repository "%s", '
133 'must be one of "%s"'
134 % (private_repo, ", ".join(self.choices_private_repo.keys()))
135 )
136 XML.SubElement(
137 xml_parent,
138 "localRepository",
139 attrib={"class": self.choices_private_repo[private_repo]},
140 )
141
142 XML.SubElement(xml_parent, "ignoreUpstremChanges").text = str(
143 data["maven"].get("ignore-upstream-changes", True)
144 ).lower()
145
146 XML.SubElement(xml_parent, "rootPOM").text = data["maven"].get(
147 "root-pom", "pom.xml"
148 )
149 XML.SubElement(xml_parent, "aggregatorStyleBuild").text = str(
150 not data["maven"].get("parallel-build-modules", False)
151 ).lower()
152 XML.SubElement(xml_parent, "incrementalBuild").text = str(
153 data["maven"].get("incremental-build", False)
154 ).lower()
155 XML.SubElement(xml_parent, "siteArchivingDisabled").text = str(
156 not data["maven"].get("automatic-site-archiving", True)
157 ).lower()
158 XML.SubElement(xml_parent, "fingerprintingDisabled").text = str(
159 not data["maven"].get("automatic-fingerprinting", True)
160 ).lower()
161 if plugin_ver < "2.0.1":
162 XML.SubElement(xml_parent, "perModuleEmail").text = str(
163 data.get("per-module-email", True)
164 ).lower()
165 XML.SubElement(xml_parent, "archivingDisabled").text = str(
166 not data["maven"].get("automatic-archiving", True)
167 ).lower()
168 XML.SubElement(xml_parent, "resolveDependencies").text = str(
169 data["maven"].get("resolve-dependencies", False)
170 ).lower()
171 XML.SubElement(xml_parent, "processPlugins").text = str(
172 data["maven"].get("process-plugins", False)
173 ).lower()
174 XML.SubElement(xml_parent, "mavenValidationLevel").text = "-1"
175 XML.SubElement(xml_parent, "runHeadless").text = str(
176 data["maven"].get("run-headless", False)
177 ).lower()
178 XML.SubElement(xml_parent, "disableTriggerDownstreamProjects").text = str(
179 data["maven"].get("disable-downstream", False)
180 ).lower()
181 if "custom-workspace" in data["maven"]:
182 XML.SubElement(xml_parent, "customWorkspace").text = str(
183 data["maven"].get("custom-workspace")
184 )
185 helpers.config_file_provider_settings(xml_parent, data["maven"])
186
187 run_post_steps = XML.SubElement(xml_parent, "runPostStepsIfResult")
188 run_conditions = ["SUCCESS", "UNSTABLE", "FAILURE"]
189 run_condition = data["maven"].get("post-step-run-condition", "FAILURE")
190 if run_condition not in run_conditions:
191 raise InvalidAttributeError(
192 "post-step-run-condition", run_condition, run_conditions
193 )
194 cond_dict = hudson_model.THRESHOLDS[run_condition]
195 XML.SubElement(run_post_steps, "name").text = cond_dict["name"]
196 XML.SubElement(run_post_steps, "ordinal").text = cond_dict["ordinal"]
197 XML.SubElement(run_post_steps, "color").text = cond_dict["color"]
198
199 return xml_parent