Wiki source code of Using Maven Artifact Repositories in Jenkins
Last modified by DevOps-as-a-Service Operator on 2025/02/05 11:33
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | DevOps-as-a-Service offers the functionality Artifact Repository, which is used to store artifacts built by Jenkins delivery pipelines. | ||
2 | |||
3 | {{toc/}} | ||
4 | |||
5 | = Jenkins configuration = | ||
6 | |||
7 | Both for pushing and pulling from the Maven repository associated with your project, you will need to set up a Maven settings.xml in your Jenkins Project. This needs to be done just once per project. Afterward, all delivery pipelines in the project can use it. | ||
8 | |||
9 | == Configure Credentials == | ||
10 | |||
11 | The [[doc:Jenkins.Automatically provided Credentials.WebHome]] should be used to push or pull from a repository of the //same //project. //This is the normal case.// | ||
12 | |||
13 | If you want to use the repository of //another //project, you have to do some prerequisites first: | ||
14 | |||
15 | 1. Create a technical user for Gitea or Nexus in the DevOps Portal. | ||
16 | 1. If you just want to pull artifacts, add a VIEWER role for the matching project to the previously created technical user. | ||
17 | 1. If you want to push artifacts, add a DEVELOPER role for the matching project to the previously created technical user. | ||
18 | 1. Now add the credentials (username and password) of the technical user to the Jenkins project, which will perform the push or pull. Use the link //Project Credentials// in the Jenkins tile of the dashboard in the DevOps Portal to get to the right place. | ||
19 | |||
20 | == Add Config File == | ||
21 | |||
22 | Go to the home of the DevOps Portal and click on //Project Folder// in the Jenkins tile. | ||
23 | |||
24 | [[image:attach:image-2024-3-15_20-39-14.png||data-xwiki-image-style-border="true" height="182" width="357"]] | ||
25 | |||
26 | The Jenkins project will be opened in a new tab of your browser. Now click on //Config Files //in the menu on the left-hand-side. | ||
27 | |||
28 | [[image:attach:image-2023-2-21_9-59-26.png||data-xwiki-image-style-border="true" queryparams="effects=drop-shadow" thumbnail="true" height="47" width="136"]] | ||
29 | |||
30 | Now click on //Add a new Config// and select the type //Maven settings.xml//. Replace the provided ID with something more meaningful, like //maven-settings//. | ||
31 | |||
32 | [[image:attach:image-2023-2-21_10-2-51.png||data-xwiki-image-style-border="true" queryparams="effects=drop-shadow" height="968" width="1000"]] | ||
33 | |||
34 | Then click NEXT. | ||
35 | |||
36 | It's suggested to use the ID also for the Name of the config file. Now click the Add button in the Server Credentials section. In the example below two servers are defined. | ||
37 | |||
38 | * For the one with the id {{code language="none"}}gitea-pull{{/code}} the [[doc:Jenkins.Automatically provided Credentials.WebHome]] {{code language="none"}}sdcloud-jenkins-pull-gitea{{/code}} is selected. | ||
39 | * For the one with the id {{code language="none"}}gitea-push{{/code}} the [[doc:Jenkins.Automatically provided Credentials.WebHome]] {{code language="none"}}sdcloud-jenkins-push-gitea{{/code}} is selected. | ||
40 | |||
41 | You can choose yourself what ServerIds you want to use. The important thing is, that the same ServerIds are used later in your pom.xml. | ||
42 | |||
43 | [[image:attach:image-2024-3-15_20-50-31.png||data-xwiki-image-style-border="true" queryparams="effects=drop-shadow" height="1088" width="1000"]] | ||
44 | |||
45 | The example content for the settings.xml provided by Jenkins can be adjusted to your needs, but it's not required. It's enough that you have added server credentials in the step before. Jenkins will automatically overwrite the {{code language="none"}}<servers>{{/code}} section in the settings.xml for you. | ||
46 | |||
47 | That's all you have to configure here. The URLs for the defined servers will be set somewhere else. Therefore, click now //Submit// to save your changes. | ||
48 | |||
49 | == Adapt Jenkinsfile == | ||
50 | |||
51 | Now, you have to make sure that the {{code language="none"}}maven-settings{{/code}} defined above are used when Jenkins is calling maven. | ||
52 | |||
53 | When using the [[doc:Jenkins.Jenkins Shared Library.WebHome]] adapt it like this: | ||
54 | |||
55 | {{code language="groovy" title="**Jenkinsfile** | ||
56 | "}} | ||
57 | @Library('sdcloud') _ | ||
58 | |||
59 | sdcPipeline( | ||
60 | mvnCommand: 'mvn clean deploy', // Overwrite the default of mvn verify to let maven really push something to nexus | ||
61 | mavenSettingsConfig: 'maven-settings' // The ID of the Config File which was set up above | ||
62 | ) | ||
63 | {{/code}} | ||
64 | |||
65 | If you are not using the [[doc:Jenkins.Jenkins Shared Library.WebHome]] you can call maven yourself like in the example below: | ||
66 | |||
67 | {{code language="groovy" title="**Jenkinsfile** | ||
68 | "}} | ||
69 | stage('deploy') { | ||
70 | withMaven( | ||
71 | maven: 'maven', | ||
72 | mavenSettingsConfig: 'maven-settings') // The ID of the Config File which was set up above. | ||
73 | { | ||
74 | sh 'mvn clean deploy' | ||
75 | } | ||
76 | } | ||
77 | {{/code}} | ||
78 | |||
79 | (% style="text-align: left;" %) | ||
80 | = Pushing Maven Artifacts to a Project Repository = | ||
81 | |||
82 | The following changes are required in the git repository of your maven project. | ||
83 | |||
84 | == Adapt pom.xml == | ||
85 | |||
86 | Set up the URL of the Project repository for in the pom.xml of your Maven Java project like shown below by adding a {{code language="none"}}<distributionManagement>{{/code}} section. | ||
87 | |||
88 | PKEY is the "Project Key" as defined in the DevOps Portal for the project repository you want to push to. Please note that this sub string is case-sensitive. So, include the project key with uppercase letters. | ||
89 | |||
90 | ==== Example for Gitea ==== | ||
91 | |||
92 | {{code language="xml" title="**pom.xml - distributionManagement** | ||
93 | "}} | ||
94 | <distributionManagement> | ||
95 | <repository> | ||
96 | <id>gitea-push</id> <!-- The repository id has to match the server id in your settings.xml. --> | ||
97 | <url>https://gitea-CUSTOMER.devops.t-systems.net/api/packages/PKEY/maven</url> <!-- Replace CUSTOMER and PKEY with the right values --> | ||
98 | </repository> | ||
99 | <snapshotRepository> | ||
100 | <id>gitea-push</id> <!-- The repository id has to match the server id in your settings.xml. --> | ||
101 | <url>https://gitea-CUSTOMER.devops.t-systems.net/api/packages/PKEY/maven</url> <!-- Replace CUSTOMER and PKEY with the right values --> | ||
102 | </snapshotRepository> | ||
103 | </distributionManagement> | ||
104 | {{/code}} | ||
105 | |||
106 | ==== (% id="cke_bm_81633S" style="display:none" %) (%%)Example for Nexus ==== | ||
107 | |||
108 | {{code language="xml" title="**pom.xml - distributionManagement** | ||
109 | "}} | ||
110 | <distributionManagement> | ||
111 | <repository> | ||
112 | <id>nexus</id> <!-- The repository id has to match the server id in your settings.xml. --> | ||
113 | <url>https://CUSTOMER.devops.t-systems.net/nexus/repository/PKEY/</url> <!-- Replace CUSTOMER and PKEY with the right values --> | ||
114 | </repository> | ||
115 | <snapshotRepository> | ||
116 | <id>nexus</id> <!-- The repository id has to match the server id in your settings.xml. --> | ||
117 | <url>https://CUSTOMER.devops.t-systems.net/nexus/repository/PKEY/</url> <!-- Replace CUSTOMER and PKEY with the right values --> | ||
118 | </snapshotRepository> | ||
119 | </distributionManagement> | ||
120 | {{/code}} | ||
121 | |||
122 | If you use {{code language="none"}}mvn deploy{{/code}} for a release version like {{code language="none"}}1.0.0{{/code}} it will use the URL defined in <{{code language="none"}}repositoryRepository>{{/code}}. | ||
123 | |||
124 | If you use {{code language="none"}}mvn deploy{{/code}} for a snapshot version like {{code language="none"}}1.0.0-SNAPSHOT{{/code}} it will use the URL defined in {{code language="none"}}<snapshotRepository>{{/code}}. | ||
125 | |||
126 | As you can see in the example, it's not a problem to use the same URL for both. | ||
127 | |||
128 | = Pulling Maven Artifacts from a Project Repository = | ||
129 | |||
130 | The following changes are required in the git repository of your maven project. | ||
131 | |||
132 | == Adapt pom.xml == | ||
133 | |||
134 | Set up the URL of the Project repository in the pom.xml of your Maven Java project like shown below by adding a {{code language="none"}}<repository>{{/code}} element to the {{code language="none"}}<repositories>{{/code}} section. | ||
135 | |||
136 | PKEY is the "Project Key" as defined in the DevOps Portal for the project repository you want to push to. Please note that this sub string is case-sensitive. So, include the project key with uppercase letters. | ||
137 | |||
138 | ==== Example for Gitea ==== | ||
139 | |||
140 | {{code language="xml" title="**pom.xml - repositories** | ||
141 | "}} | ||
142 | <repositories> | ||
143 | <repository> | ||
144 | <id>gitea-pull</id> <!-- The repository id has to match the server id in your settings.xml. --> | ||
145 | <url>https://gitea-CUSTOMER.devops.t-systems.net/api/packages/PKEY/maven</url> <!-- Replace CUSTOMER and PKEY with the right values --> | ||
146 | </repository> | ||
147 | </repositories> | ||
148 | |||
149 | {{/code}} | ||
150 | |||
151 | ==== (% id="cke_bm_83158S" style="display:none" %) (%%)Example for Nexus ==== | ||
152 | |||
153 | {{code language="xml" title="**pom.xml - repositories** | ||
154 | "}} | ||
155 | <repositories> | ||
156 | <repository> | ||
157 | <id>nexus</id> <!-- The repository id has to match the server id in your settings.xml. --> | ||
158 | <url>https://CUSTOMER.devops.t-systems.net/nexus/repository/PKEY/</url> <!-- Replace CUSTOMER and PKEY with the right values --> | ||
159 | </repository> | ||
160 | </repositories> | ||
161 | {{/code}} | ||
162 | |||
163 | |||
164 | The repository id has to match the server id in your settings.xml. | ||
165 | |||
166 | For pulling a dependency which was pushed before, you will need to also add a <dependency> to the <dependencies> section. See below for an example that you have to adapt to the real groupId, artifactId and version you used during pushing. | ||
167 | |||
168 | {{code language="xml" title="**pom.xml - dependencies** | ||
169 | "}} | ||
170 | <dependency> | ||
171 | <groupId>com.tsystems.devops</groupId> | ||
172 | <artifactId>maven-test</artifactId> | ||
173 | <version>1.0.0-SNAPSHOT</version> | ||
174 | </dependency> | ||
175 | {{/code}} | ||
176 | |||
177 | For using a dependency, it is not required to run {{code language="none"}}mvn deploy. mvn verify{{/code}} or {{code language="none"}}mvn install{{/code}} will also do it. | ||
178 | |||
179 |