summaryrefslogtreecommitdiffstats
path: root/jenkins_jobs/modules/project_maven.py
blob: 91178598cb0ad1def9ce1d6d8f9e0d976e5b8959 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# Copyright 2012 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.


"""
The Maven Project module handles creating Maven Jenkins projects.

To create a Maven project, specify ``maven`` in the ``project-type``
attribute to the :ref:`Job` definition. It also requires a ``maven`` section
in the :ref:`Job` definition.

:Job Parameters:
    * **root-module**:
        * **group-id** (`str`): GroupId.
        * **artifact-id** (`str`): ArtifactId.
    * **root-pom** (`str`): The path to the pom.xml file. (default 'pom.xml')
    * **goals** (`str`): Goals to execute. (required)
    * **maven-opts** (`str`): Java options to pass to maven (aka MAVEN_OPTS)
    * **maven-name** (`str`): Installation of maven which should be used.
      Not setting ``maven-name`` appears to use the first maven install
      defined in the global jenkins config.
    * **private-repository** ('str'): Whether to use a private maven repository
      Possible values are `default`, `local-to-workspace` and
      `local-to-executor`.
    * **ignore-upstream-changes** (`bool`): Do not start a build whenever
      a SNAPSHOT dependency is built or not. (default true)
    * **incremental-build** (`bool`): Activate incremental build - only build
      changed modules (default false).
    * **automatic-archiving** (`bool`): Activate automatic artifact archiving
      (default true).
    * **automatic-site-archiving** (`bool`): Activate automatic site
      documentation artifact archiving (default true).
    * **automatic-fingerprinting** (`bool`): Activate automatic fingerprinting
      of consumed and produced artifacts (default true).
    * **per-module-email** (`bool`): Send an e-mail for each failed module
      (default true).
    * **parallel-build-modules** (`bool`): Build modules in parallel
      (default false)
    * **resolve-dependencies** (`bool`): Resolve Dependencies during Pom
      parsing (default false).
    * **run-headless** (`bool`): Run headless (default false).
    * **disable-downstream** (`bool`): Disable triggering of downstream
      projects (default false).
    * **process-plugins** (`bool`): Process Plugins during Pom parsing
      (default false).
    * **custom-workspace** (`str`): Path to the custom workspace. If no path is
      provided, custom workspace is not used. (optional)
    * **settings** (`str`): Path to custom maven settings file. If settings
      type is 'file' then this is a Path. Otherwise it is the id for
      ConfigFileProvider. (optional)
    * **settings-type** (`str`): Type of settings file file|cfp.
      (default file)
    * **global-settings** (`str`): Path to custom maven global settings file.
      If settings type is 'file' then this is a Path. Otherwise it is the id
      for ConfigFileProvider. (optional)
    * **global-settings-type** (`str`): Type of settings file file|cfp.
      (default file)
    * **post-step-run-condition** (`str`): Run the post-build steps only if the
      build succeeds ('SUCCESS'), build succeeds or is unstable ('UNSTABLE'),
      regardless of build result ('FAILURE'). (default 'FAILURE').

Requires the Jenkins :jenkins-plugins:`Config File Provider Plugin
<config-file-provider>`
for the Config File Provider "settings" and "global-settings" config.

Example:

    .. literalinclude:: /../../tests/general/fixtures/project-maven001.yaml

CFP Example:

    .. literalinclude:: /../../tests/general/fixtures/project-maven003.yaml
"""
import pkg_resources
import xml.etree.ElementTree as XML

from jenkins_jobs.errors import InvalidAttributeError
import jenkins_jobs.modules.base
import jenkins_jobs.modules.helpers as helpers
from jenkins_jobs.modules import hudson_model


class Maven(jenkins_jobs.modules.base.Base):
    sequence = 0

    choices_private_repo = {
        "default": "hudson.maven.local_repo.DefaultLocalRepositoryLocator",
        "local-to-workspace": "hudson.maven.local_repo.PerJobLocalRepositoryLocator",
        "local-to-executor": "hudson.maven.local_repo.PerExecutorLocalRepositoryLocator",
    }

    def root_xml(self, data):
        xml_parent = XML.Element("maven2-moduleset")
        if "maven" not in data:
            return xml_parent

        # determine version of plugin
        plugin_info = self.registry.get_plugin_info("Maven Integration plugin")
        version = pkg_resources.parse_version(plugin_info.get("version", "0"))

        if "root-module" in data["maven"]:
            root_module = XML.SubElement(xml_parent, "rootModule")
            XML.SubElement(root_module, "groupId").text = data["maven"]["root-module"][
                "group-id"
            ]
            XML.SubElement(root_module, "artifactId").text = data["maven"][
                "root-module"
            ]["artifact-id"]
        XML.SubElement(xml_parent, "goals").text = data["maven"]["goals"]

        maven_opts = data["maven"].get("maven-opts")
        if maven_opts:
            XML.SubElement(xml_parent, "mavenOpts").text = maven_opts

        maven_name = data["maven"].get("maven-name")
        if maven_name:
            XML.SubElement(xml_parent, "mavenName").text = maven_name

        private_repo = data["maven"].get("private-repository")
        if private_repo:
            if private_repo not in self.choices_private_repo.keys():
                raise ValueError(
                    'Not a valid private-repository "%s", '
                    'must be one of "%s"'
                    % (private_repo, ", ".join(self.choices_private_repo.keys()))
                )
            XML.SubElement(
                xml_parent,
                "localRepository",
                attrib={"class": self.choices_private_repo[private_repo]},
            )

        XML.SubElement(xml_parent, "ignoreUpstremChanges").text = str(
            data["maven"].get("ignore-upstream-changes", True)
        ).lower()

        XML.SubElement(xml_parent, "rootPOM").text = data["maven"].get(
            "root-pom", "pom.xml"
        )
        XML.SubElement(xml_parent, "aggregatorStyleBuild").text = str(
            not data["maven"].get("parallel-build-modules", False)
        ).lower()
        XML.SubElement(xml_parent, "incrementalBuild").text = str(
            data["maven"].get("incremental-build", False)
        ).lower()
        XML.SubElement(xml_parent, "siteArchivingDisabled").text = str(
            not data["maven"].get("automatic-site-archiving", True)
        ).lower()
        XML.SubElement(xml_parent, "fingerprintingDisabled").text = str(
            not data["maven"].get("automatic-fingerprinting", True)
        ).lower()
        if version > pkg_resources.parse_version(
            "0"
        ) and version < pkg_resources.parse_version("2.0.1"):
            XML.SubElement(xml_parent, "perModuleEmail").text = str(
                data.get("per-module-email", True)
            ).lower()
        XML.SubElement(xml_parent, "archivingDisabled").text = str(
            not data["maven"].get("automatic-archiving", True)
        ).lower()
        XML.SubElement(xml_parent, "resolveDependencies").text = str(
            data["maven"].get("resolve-dependencies", False)
        ).lower()
        XML.SubElement(xml_parent, "processPlugins").text = str(
            data["maven"].get("process-plugins", False)
        ).lower()
        XML.SubElement(xml_parent, "mavenValidationLevel").text = "-1"
        XML.SubElement(xml_parent, "runHeadless").text = str(
            data["maven"].get("run-headless", False)
        ).lower()
        XML.SubElement(xml_parent, "disableTriggerDownstreamProjects").text = str(
            data["maven"].get("disable-downstream", False)
        ).lower()
        if "custom-workspace" in data["maven"]:
            XML.SubElement(xml_parent, "customWorkspace").text = str(
                data["maven"].get("custom-workspace")
            )
        helpers.config_file_provider_settings(xml_parent, data["maven"])

        run_post_steps = XML.SubElement(xml_parent, "runPostStepsIfResult")
        run_conditions = ["SUCCESS", "UNSTABLE", "FAILURE"]
        run_condition = data["maven"].get("post-step-run-condition", "FAILURE")
        if run_condition not in run_conditions:
            raise InvalidAttributeError(
                "post-step-run-condition", run_condition, run_conditions
            )
        cond_dict = hudson_model.THRESHOLDS[run_condition]
        XML.SubElement(run_post_steps, "name").text = cond_dict["name"]
        XML.SubElement(run_post_steps, "ordinal").text = cond_dict["ordinal"]
        XML.SubElement(run_post_steps, "color").text = cond_dict["color"]

        return xml_parent