This repository has been archived by the owner on Jun 7, 2021. It is now read-only.
/
MavenArtifactDescriptor.java
192 lines (147 loc) · 4.96 KB
/
MavenArtifactDescriptor.java
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
package org.wildfly.swarm.bootstrap.util;
import java.io.File;
import java.io.IOException;
/**
* @author Bob McWhirter
*/
public class MavenArtifactDescriptor {
public class Builder {
public Builder groupId(String groupId) {
MavenArtifactDescriptor.this.groupId = groupId;
return this;
}
public Builder artifactId(String artifactId) {
MavenArtifactDescriptor.this.artifactId = artifactId;
return this;
}
public Builder version(String version) {
MavenArtifactDescriptor.this.version = version;
return this;
}
public Builder type(String type) {
MavenArtifactDescriptor.this.type = type;
return this;
}
public Builder classifier(String classifier) {
MavenArtifactDescriptor.this.classifier = classifier;
return this;
}
public MavenArtifactDescriptor build() {
return MavenArtifactDescriptor.this;
}
}
private String groupId;
private String artifactId;
private String version;
private String classifier;
private String type;
public static Builder build() {
return new MavenArtifactDescriptor().builder().type("jar");
}
private MavenArtifactDescriptor() {
}
private Builder builder() {
return new Builder();
}
public static MavenArtifactDescriptor fromMscGav(String gav) throws IOException {
String[] parts = gav.split(":");
if ( parts.length == 3 ) {
return new MavenArtifactDescriptor( parts[0], parts[1], parts[2] );
} else if (parts.length ==4) {
return new MavenArtifactDescriptor( parts[0], parts[1], "jar", parts[3], parts[2] );
} else {
throw new IOException("Invalid gav: " + gav );
}
}
public static MavenArtifactDescriptor fromMavenGav(String gav) throws IOException {
String[] parts = gav.split(":");
if ( parts.length == 3 ) {
return new MavenArtifactDescriptor( parts[0], parts[1], parts[2] );
} else if (parts.length ==4 ) {
return new MavenArtifactDescriptor( parts[0], parts[1], parts[2], null, parts[3] );
} else if ( parts.length == 5 ) {
return new MavenArtifactDescriptor( parts[0], parts[1], parts[2], parts[3], parts[4] );
} else {
throw new IOException("Invalid gav: " + gav );
}
}
public MavenArtifactDescriptor(String groupId, String artifactId, String version) {
this(groupId, artifactId, "jar", null, version);
}
public MavenArtifactDescriptor(String groupId, String artifactId, String type, String classifier, String version) {
this.groupId = groupId;
this.artifactId = artifactId;
this.version = version;
this.type = type;
if ( classifier != null && ! classifier.trim().equals( "" ) ) {
this.classifier = classifier;
}
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof MavenArtifactDescriptor)) {
return false;
}
return this.mavenGav().equals(((MavenArtifactDescriptor) obj).mavenGav());
}
@Override
public int hashCode() {
return mavenGav().hashCode();
}
public String groupId() {
return this.groupId;
}
public String artifactId() {
return this.artifactId;
}
public String version() {
return this.version;
}
public String classifier() {
return this.classifier;
}
public String type() {
return this.type;
}
public String mscGav() {
return this.groupId + ":" +
this.artifactId + ":" +
this.version +
(this.classifier == null ? "" : ":" + this.classifier);
}
public String mavenGav() {
return this.groupId + ":" +
this.artifactId + ":" +
(this.type == null ? "jar" : this.type) + ":" +
(this.classifier == null ? "" : this.classifier + ":") +
this.version;
}
public String repoPath(boolean forJar) {
char delim = File.separatorChar;
if (forJar) {
delim = '/';
}
String[] groupParts = this.groupId.split("\\.");
StringBuffer p = new StringBuffer();
for (String groupPart : groupParts) {
p.append(groupPart)
.append(delim);
}
p.append(this.artifactId)
.append(delim);
p.append(this.version)
.append(delim);
p.append( this.artifactId)
.append('-')
.append( this.version );
if ( this.classifier != null ) {
p.append( '-' )
.append( this.classifier );
}
p.append( '.' ).append( this.type );
return p.toString();
}
}