Skip to content

Commit e85b0f6

Browse files
committed
Introduced GAV.optional flag
1 parent abce8ac commit e85b0f6

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

Diff for: restx-build/src/main/java/restx/build/GAV.java

+25-12
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,49 @@
66
* Time: 2:21 PM
77
*/
88
public class GAV {
9+
private static final String OPTIONAL_SUFFIX = "!optional";
910
public static GAV parse(String gav) {
1011
String[] parts = gav.split(":");
1112
if (parts.length < 3 || parts.length > 5) {
1213
throw new IllegalArgumentException("can't parse '" + gav + "' as a module coordinates (GAV). " +
1314
"It must have at least 3 parts separated by columns. (4th and 5th are optional and correspond to artifact type and classifier)");
1415
}
16+
boolean optional = false;
17+
if(parts[parts.length-1].endsWith(OPTIONAL_SUFFIX)) {
18+
optional = true;
19+
parts[parts.length-1] = parts[parts.length-1].substring(0, parts[parts.length-1].length()-OPTIONAL_SUFFIX.length());
20+
}
1521
if(parts.length == 3) {
16-
return new GAV(parts[0], parts[1], parts[2]);
22+
return new GAV(parts[0], parts[1], parts[2], optional);
1723
}
1824
if(parts.length == 4) {
19-
return new GAV(parts[0], parts[1], parts[2], parts[3]);
25+
return new GAV(parts[0], parts[1], parts[2], parts[3], optional);
2026
}
21-
return new GAV(parts[0], parts[1], parts[2], parts[3], parts[4]);
27+
return new GAV(parts[0], parts[1], parts[2], parts[3], parts[4], optional);
2228
}
2329

2430
private final String groupId;
2531
private final String artifactId;
2632
private final String version;
2733
private final String type;
2834
private final String classifier;
35+
private final boolean optional;
2936

30-
public GAV(String groupId, String artifactId, String version) {
31-
this(groupId, artifactId, version, null, null);
37+
public GAV(String groupId, String artifactId, String version, final boolean optional) {
38+
this(groupId, artifactId, version, null, null, optional);
3239
}
3340

34-
public GAV(String groupId, String artifactId, String version, String type) {
35-
this(groupId, artifactId, version, type, null);
41+
public GAV(String groupId, String artifactId, String version, String type, final boolean optional) {
42+
this(groupId, artifactId, version, type, null, optional);
3643
}
3744

38-
public GAV(final String groupId, final String artifactId, final String version, final String type, final String classifier) {
45+
public GAV(final String groupId, final String artifactId, final String version, final String type, final String classifier, final boolean optional) {
3946
this.groupId = groupId;
4047
this.artifactId = artifactId;
4148
this.version = version;
4249
this.type = type;
4350
this.classifier = classifier;
51+
this.optional = optional;
4452
}
4553

4654
public String getGroupId() {
@@ -62,7 +70,11 @@ public String getType() {
6270
public String getClassifier() {
6371
return classifier;
6472
}
65-
73+
74+
public boolean isOptional() {
75+
return optional;
76+
}
77+
6678
@Override
6779
public String toString() {
6880
return groupId + ":" + artifactId + ":" + version;
@@ -74,12 +86,13 @@ public String toString() {
7486
* through GAV.parse()
7587
*/
7688
public String toParseableString(){
89+
String suffix = optional?OPTIONAL_SUFFIX:"";
7790
if (type == null){
78-
return groupId + ":" + artifactId + ":" + version;
91+
return groupId + ":" + artifactId + ":" + version + suffix;
7992
}
8093
if(classifier == null) {
81-
return groupId + ":" + artifactId + ":" + version + ":" + type;
94+
return groupId + ":" + artifactId + ":" + version + ":" + type + suffix;
8295
}
83-
return groupId + ":" + artifactId + ":" + version + ":" + type + ":" + classifier;
96+
return groupId + ":" + artifactId + ":" + version + ":" + type + ":" + classifier + suffix;
8497
}
8598
}

Diff for: restx-build/src/main/java/restx/build/MavenSupport.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ private GAV getGav(JSONObject jsonObject, String typeKey) {
7878
jsonObject.getString("artifactId"),
7979
String.valueOf(jsonObject.get("version")),
8080
typeKey==null?null:jsonObject.has(typeKey)?jsonObject.getString(typeKey):null,
81-
jsonObject.has("classifier")?jsonObject.getString("classifier"):null);
81+
jsonObject.has("classifier")?jsonObject.getString("classifier"):null,
82+
false);
8283
}
8384
}
8485
static class Generator {

Diff for: restx-build/src/test/java/restx/build/GAVTest.java

+7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ public void should_parse_classifier_artifact(){
2323
shouldBeConsistent("restx:restx-ui:0.2:jar:jdk8");
2424
}
2525

26+
@Test
27+
public void should_parse_optional_artifact(){
28+
shouldBeConsistent("restx:restx-ui:0.2!optional");
29+
shouldBeConsistent("restx:restx-ui:0.2:zip!optional");
30+
shouldBeConsistent("restx:restx-ui:0.2:jar:jdk8!optional");
31+
}
32+
2633
@Test
2734
public void should_parse_artifact_with_type() throws Exception {
2835
shouldBeConsistent("restx:restx-ui:0.2:zip");

0 commit comments

Comments
 (0)