• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

devonfw / IDEasy / 13160073324

05 Feb 2025 02:52PM UTC coverage: 68.252% (-0.1%) from 68.379%
13160073324

Pull #1002

github

web-flow
Merge 61eb7829f into 62fa12bac
Pull Request #1002: #786: Upgrade commandlet

2901 of 4667 branches covered (62.16%)

Branch coverage included in aggregate %.

7504 of 10578 relevant lines covered (70.94%)

3.09 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.58
cli/src/main/java/com/devonfw/tools/ide/tool/mvn/MvnArtifact.java
1
package com.devonfw.tools.ide.tool.mvn;
2

3
import java.util.Objects;
4
import java.util.regex.Matcher;
5
import java.util.regex.Pattern;
6

7
import com.devonfw.tools.ide.tool.repository.MavenRepository;
8

9
/**
10
 * Simple type representing a maven artifact.
11
 */
12
public final class MvnArtifact {
13

14
  /** {@link #getGroupId() Group ID} of IDEasy. */
15
  public static final String GROUP_ID_IDEASY = "com.devonfw.tools.IDEasy";
16

17
  /** {@link #getArtifactId() Artifact ID} of IDEasy command line interface. */
18
  public static final String ARTIFACT_ID_IDEASY_CLI = "ide-cli";
19

20
  /** {@link #getClassifier() Classifier} of source code. */
21
  public static final String CLASSIFER_SOURCES = "sources";
22

23
  /** {@link #getType() Type} of JAR file. */
24
  public static final String TYPE_JAR = "jar";
25

26
  /** {@link #getType() Type} of POM XML file. */
27
  public static final String TYPE_POM = "pom";
28

29
  /** {@link #getFilename() Filename} for artifact metadata with version information. */
30
  public static final String MAVEN_METADATA_XML = "maven-metadata.xml";
31

32
  private static final Pattern SNAPSHOT_VERSION_PATTERN = Pattern.compile("-\\d{8}\\.\\d{6}-\\d+");
4✔
33

34
  private final String groupId;
35

36
  private final String artifactId;
37

38
  private final String version;
39

40
  private final String classifier;
41

42
  private final String type;
43

44
  private final String filename;
45

46
  private String path;
47

48
  private String key;
49

50
  private String downloadUrl;
51

52
  /**
53
   * The constructor.
54
   *
55
   * @param groupId the {@link #getGroupId() group ID}.
56
   * @param artifactId the {@link #getArtifactId() artifact ID}.
57
   * @param version the {@link #getVersion() version}.
58
   */
59
  public MvnArtifact(String groupId, String artifactId, String version) {
60
    this(groupId, artifactId, version, TYPE_JAR);
6✔
61
  }
1✔
62

63
  /**
64
   * The constructor.
65
   *
66
   * @param groupId the {@link #getGroupId() group ID}.
67
   * @param artifactId the {@link #getArtifactId() artifact ID}.
68
   * @param version the {@link #getVersion() version}.
69
   * @param type the {@link #getType() type}.
70
   */
71
  public MvnArtifact(String groupId, String artifactId, String version, String type) {
72
    this(groupId, artifactId, version, type, "");
7✔
73
  }
1✔
74

75
  /**
76
   * The constructor.
77
   *
78
   * @param groupId the {@link #getGroupId() group ID}.
79
   * @param artifactId the {@link #getArtifactId() artifact ID}.
80
   * @param version the {@link #getVersion() version}.
81
   * @param type the {@link #getType() type}.
82
   * @param classifier the {@link #getClassifier() classifier}.
83
   */
84
  public MvnArtifact(String groupId, String artifactId, String version, String type, String classifier) {
85
    this(groupId, artifactId, version, type, classifier, null);
8✔
86
  }
1✔
87

88
  MvnArtifact(String groupId, String artifactId, String version, String type, String classifier, String filename) {
89
    super();
2✔
90
    this.groupId = requireNotEmpty(groupId, "groupId");
5✔
91
    this.artifactId = requireNotEmpty(artifactId, "artifactId");
5✔
92
    this.version = requireNotEmpty(version, "version");
5✔
93
    this.classifier = notNull(classifier);
4✔
94
    this.type = requireNotEmpty(type, "type");
5✔
95
    this.filename = filename;
3✔
96
  }
1✔
97

98
  /**
99
   * @return the group ID (e.g. {@link #GROUP_ID_IDEASY}).
100
   */
101
  public String getGroupId() {
102
    return this.groupId;
3✔
103
  }
104

105
  /**
106
   * @return the artifact ID (e.g. {@link #ARTIFACT_ID_IDEASY_CLI}).
107
   */
108
  public String getArtifactId() {
109
    return this.artifactId;
3✔
110
  }
111

112
  /**
113
   * @return the version.
114
   * @see com.devonfw.tools.ide.version.VersionIdentifier
115
   */
116
  public String getVersion() {
117
    return this.version;
3✔
118
  }
119

120
  /**
121
   * @param newVersion the new value of {@link #getVersion()}.
122
   * @return a new {@link MvnArtifact} with the given version.
123
   */
124
  public MvnArtifact withVersion(String newVersion) {
125

126
    if (this.version.equals(newVersion)) {
5!
127
      return this;
×
128
    }
129
    return new MvnArtifact(this.groupId, this.artifactId, newVersion, this.type, this.classifier, this.filename);
15✔
130
  }
131

132
  /**
133
   * @return the classifier. Will be the empty {@link String} for no classifier.
134
   */
135
  public String getClassifier() {
136
    return this.classifier;
3✔
137
  }
138

139
  /**
140
   * @param newClassifier the new value of {@link #getClassifier()}.
141
   * @return a new {@link MvnArtifact} with the given classifier.
142
   */
143
  public MvnArtifact withClassifier(String newClassifier) {
144

145
    if (this.classifier.equals(newClassifier)) {
5!
146
      return this;
×
147
    }
148
    return new MvnArtifact(this.groupId, this.artifactId, this.version, this.type, newClassifier, this.filename);
15✔
149
  }
150

151
  /**
152
   * @return the type (e.g. #TYPE_JAR}
153
   */
154
  public String getType() {
155
    return type;
3✔
156
  }
157

158
  /**
159
   * @param newType the new value of {@link #getType()}.
160
   * @return a new {@link MvnArtifact} with the given type.
161
   */
162
  public MvnArtifact withType(String newType) {
163

164
    if (this.type.equals(newType)) {
5!
165
      return this;
×
166
    }
167
    return new MvnArtifact(this.groupId, this.artifactId, this.version, newType, this.classifier, this.filename);
15✔
168
  }
169

170
  /**
171
   * @return the filename of the artifact.
172
   */
173
  public String getFilename() {
174

175
    if (this.filename == null) {
3✔
176
      String infix = "";
2✔
177
      if (!this.classifier.isEmpty()) {
4!
178
        infix = "-" + this.classifier;
4✔
179
      }
180
      return this.artifactId + "-" + this.version + infix + "." + this.type;
9✔
181
    }
182
    return this.filename;
3✔
183
  }
184

185
  /**
186
   * @param newFilename the new value of {@link #getFilename()}.
187
   * @return a new {@link MvnArtifact} with the given filename.
188
   */
189
  public MvnArtifact withFilename(String newFilename) {
190

191
    if (Objects.equals(this.filename, newFilename)) {
5!
192
      return this;
×
193
    }
194
    return new MvnArtifact(this.groupId, this.artifactId, this.version, this.type, this.classifier, newFilename);
15✔
195
  }
196

197
  /**
198
   * @return a new {@link MvnArtifact} for {@link #MAVEN_METADATA_XML}.
199
   */
200
  public MvnArtifact withMavenMetadata() {
201

202
    return withType("xml").withFilename(MAVEN_METADATA_XML);
6✔
203
  }
204

205
  /**
206
   * @return the {@link String} with the path to the specified artifact relative to the maven repository base path or URL. For snapshots, includes the
207
   *     timestamped version in the artifact filename.
208
   */
209
  public String getPath() {
210
    if (this.path == null) {
3✔
211
      StringBuilder sb = new StringBuilder();
4✔
212
      // Common path start: groupId/artifactId/version
213
      sb.append(this.groupId.replace('.', '/')).append('/')
11✔
214
          .append(this.artifactId).append('/');
4✔
215

216
      if (!this.version.startsWith("*")) {
5✔
217
        sb.append(getBaseVersion()).append('/');
7✔
218
      }
219
      sb.append(getFilename());
5✔
220
      this.path = sb.toString();
4✔
221
    }
222
    return this.path;
3✔
223
  }
224

225
  /**
226
   * @return the artifact key as unique identifier.
227
   */
228
  String getKey() {
229
    if (this.key == null) {
3✔
230
      int capacity = this.groupId.length() + this.artifactId.length() + this.version.length() + type.length() + classifier.length() + 4;
22✔
231
      StringBuilder sb = new StringBuilder(capacity);
5✔
232
      sb.append(this.groupId).append(':').append(this.artifactId).append(':').append(this.version).append(':').append(this.type);
20✔
233
      if (!this.classifier.isEmpty()) {
4✔
234
        sb.append(':').append(this.classifier);
7✔
235
      }
236
      this.key = sb.toString();
4✔
237
      assert (this.key.length() <= capacity);
6!
238
    }
239
    return this.key;
3✔
240
  }
241

242
  /**
243
   * Checks if the current artifact version is a snapshot version.
244
   *
245
   * @return true if this is a snapshot version, false otherwise
246
   */
247
  public boolean isSnapshot() {
248
    return this.version.endsWith("-SNAPSHOT") || SNAPSHOT_VERSION_PATTERN.matcher(this.version).find();
15✔
249
  }
250

251
  /**
252
   * Gets the base version without snapshot timestamp. For snapshot versions like "2024.04.001-beta-20240419.123456-1", returns "2024.04.001-beta-SNAPSHOT". For
253
   * release versions, returns the version as is.
254
   *
255
   * @return the base version
256
   */
257
  public String getBaseVersion() {
258
    Matcher matcher = SNAPSHOT_VERSION_PATTERN.matcher(this.version);
5✔
259
    if (matcher.find()) {
3✔
260
      return matcher.replaceAll("-SNAPSHOT");
4✔
261
    }
262
    return this.version;
3✔
263
  }
264

265
  /**
266
   * @return the download URL to download the artifact from the maven repository.
267
   */
268
  public String getDownloadUrl() {
269
    if (this.downloadUrl == null) {
3!
270
      String baseUrl = isSnapshot() ? MavenRepository.MAVEN_SNAPSHOTS : MavenRepository.MAVEN_CENTRAL;
7✔
271
      this.downloadUrl = baseUrl + "/" + getPath();
6✔
272
    }
273
    return this.downloadUrl;
3✔
274
  }
275

276
  @Override
277
  public int hashCode() {
278
    return Objects.hash(this.groupId, this.artifactId, this.version);
19✔
279
  }
280

281
  @Override
282
  public boolean equals(Object obj) {
283
    if (obj == this) {
3!
284
      return true;
×
285
    } else if (obj instanceof MvnArtifact other) {
6!
286
      return this.groupId.equals(other.groupId) && this.artifactId.equals(other.artifactId) && this.version.equals(other.version)
23!
287
          && this.classifier.equals(other.classifier) && this.type.equals(other.type) && Objects.equals(this.filename, other.filename);
16!
288
    }
289
    return false;
×
290
  }
291

292
  @Override
293
  public String toString() {
294
    return getKey();
3✔
295
  }
296

297
  private static String notNull(String value) {
298

299
    if (value == null) {
2!
300
      return "";
×
301
    }
302
    return value;
2✔
303
  }
304

305
  private static String requireNotEmpty(String value, String propertyName) {
306

307
    if (isEmpty(value)) {
3!
308
      throw new IllegalArgumentException("Maven artifact property " + propertyName + " must not be empty");
×
309
    }
310
    return value;
2✔
311
  }
312

313
  private static boolean isEmpty(String value) {
314

315
    return ((value == null) || value.isEmpty());
7!
316
  }
317

318
  /**
319
   * @param artifactId the {@link #getArtifactId() artifact ID}.
320
   * @param version the {@link #getVersion() version}.
321
   * @param type the {@link #getType() type}.
322
   * @param classifier the {@link #getClassifier() classifier}.
323
   * @return the IDEasy {@link MvnArtifact}.
324
   */
325
  public static MvnArtifact ofIdeasy(String artifactId, String version, String type, String classifier) {
326

327
    return new MvnArtifact(GROUP_ID_IDEASY, artifactId, version, type, classifier);
9✔
328
  }
329

330
  /**
331
   * @param version the {@link #getVersion() version}.
332
   * @param type the {@link #getType() type}.
333
   * @param classifier the {@link #getClassifier() classifier}.
334
   * @return the IDEasy {@link MvnArtifact}.
335
   */
336
  public static MvnArtifact ofIdeasyCli(String version, String type, String classifier) {
337

338
    return ofIdeasy(ARTIFACT_ID_IDEASY_CLI, version, type, classifier);
6✔
339
  }
340
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc