Skip to content

Commit e9e1929

Browse files
committed
chore: integrating dependencybuilder into the dependency class
Signed-off-by: Maximillian Arruda <[email protected]>
1 parent 1f0abff commit e9e1929

File tree

2 files changed

+108
-28
lines changed

2 files changed

+108
-28
lines changed

src/main/java/br/org/soujava/pomeditor/api/Dependency.java

Lines changed: 96 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,115 @@
2727
public final class Dependency {
2828

2929
/**
30+
* Returns a {@link DependencyBuilder} instance
3031
* @return a {@link DependencyBuilder}
3132
*/
3233
public static DependencyBuilder builder() {
3334
return new DependencyBuilder();
3435
}
3536

37+
/**
38+
* Returns a {@link DependencyBuilder} based on a GAV - groupId:artifactId:version - parameter
39+
* @return a {@link DependencyBuilder}
40+
*/
41+
public static DependencyBuilder ofGav(String gav) {
42+
DependencyBuilder builder = builder();
43+
var gavValues = Arrays.stream(gav.split(":"))
44+
.filter(Objects::nonNull)
45+
.filter(item -> !item.isBlank())
46+
.map(String::trim)
47+
.collect(Collectors.toList());
48+
if (gavValues.size() >= 1)
49+
builder.withGroupId(gavValues.get(0));
50+
if (gavValues.size() >= 2)
51+
builder.withArtifactId(gavValues.get(1));
52+
if (gavValues.size() >= 3)
53+
builder.withVersion(gavValues.get(2));
54+
return builder;
55+
}
56+
57+
/**
58+
* Dependency builder
59+
*/
60+
public static class DependencyBuilder {
61+
62+
private String groupId;
63+
private String artifactId;
64+
private String version;
65+
private String type;
66+
private String classifier;
67+
private String scope;
68+
69+
/**
70+
* @param groupId groupId
71+
* @return the same {@link DependencyBuilder} instance
72+
*/
73+
public DependencyBuilder withGroupId(String groupId) {
74+
this.groupId = groupId;
75+
return this;
76+
}
77+
78+
/**
79+
* @param artifactId artifactId
80+
* @return the same {@link DependencyBuilder} instance
81+
*/
82+
public DependencyBuilder withArtifactId(String artifactId) {
83+
this.artifactId = artifactId;
84+
return this;
85+
}
86+
87+
/**
88+
* @param version version
89+
* @return the same {@link DependencyBuilder} instance
90+
*/
91+
public DependencyBuilder withVersion(String version) {
92+
this.version = version;
93+
return this;
94+
}
95+
96+
/**
97+
* @param type type
98+
* @return the same {@link DependencyBuilder} instance
99+
*/
100+
public DependencyBuilder withType(String type) {
101+
this.type = type;
102+
return this;
103+
}
104+
105+
/**
106+
* @param classifier classifier
107+
* @return the same {@link DependencyBuilder} instance
108+
*/
109+
public DependencyBuilder withClassifier(String classifier) {
110+
this.classifier = classifier;
111+
return this;
112+
}
113+
114+
/**
115+
* @param scope scope
116+
* @return the same {@link DependencyBuilder} instance
117+
*/
118+
public DependencyBuilder withScope(String scope) {
119+
this.scope = scope;
120+
return this;
121+
}
122+
123+
/**
124+
* @return a {@link Dependency} instance
125+
*/
126+
public Dependency build() {
127+
return new Dependency(groupId, artifactId, version, type, classifier, scope);
128+
}
129+
}
130+
36131
private final String groupId;
37132
private final String artifactId;
38133
private final String version;
39134
private final String type;
40135
private final String classifier;
41136
private final String scope;
42137

43-
Dependency(String groupId, String artifactId, String version, String type, String classifier, String scope) {
138+
private Dependency(String groupId, String artifactId, String version, String type, String classifier, String scope) {
44139
if (groupId == null || groupId.isBlank())
45140
throw new IllegalArgumentException("groupId must be provided");
46141

@@ -55,23 +150,6 @@ public static DependencyBuilder builder() {
55150
this.scope = scope;
56151
}
57152

58-
public static DependencyBuilder ofGav(String gav) {
59-
DependencyBuilder builder = new DependencyBuilder();
60-
var gavValues = Arrays.stream(gav.split(":"))
61-
.filter(Objects::nonNull)
62-
.filter(item -> !item.isBlank())
63-
.map(String::trim)
64-
.collect(Collectors.toList());
65-
if (gavValues.size() >= 1)
66-
builder.setGroupId(gavValues.get(0));
67-
if (gavValues.size() >= 2)
68-
builder.setArtifactId(gavValues.get(1));
69-
if (gavValues.size() >= 3)
70-
builder.setVersion(gavValues.get(2));
71-
return builder;
72-
}
73-
74-
75153
/**
76154
* @return the groupId
77155
*/

src/main/java/br/org/soujava/pomeditor/mojo/AddDependencyMojo.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
package br.org.soujava.pomeditor.mojo;
1919

20-
import br.org.soujava.pomeditor.api.Dependency;
2120
import br.org.soujava.pomeditor.api.AddDependency;
21+
import br.org.soujava.pomeditor.api.Dependency;
2222
import br.org.soujava.pomeditor.api.PomChange;
2323
import org.apache.maven.plugin.AbstractMojo;
2424
import org.apache.maven.plugin.MojoExecutionException;
@@ -67,11 +67,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
6767
try {
6868
getLog().info(String.format("trying to add the dependency: %s to the \"%s\" file...", dependency, pomFile));
6969

70-
transactionFor(pomFile)
71-
.execute(() -> Optional
72-
.ofNullable(this.addDependencyCommand)
73-
.orElse(AddDependency::execute)
74-
.accept(pomFile, dependency));
70+
change(pomFile).execute(() -> dependencyCommand().accept(pomFile, dependency));
7571

7672
getLog().info(String.format("added the dependency: %s to the \"%s\" file.", dependency, pomFile));
7773
} catch (Throwable ex) {
@@ -82,7 +78,13 @@ public void execute() throws MojoExecutionException, MojoFailureException {
8278
}
8379
}
8480

85-
private PomChange transactionFor(Path pomFile) {
81+
private BiConsumer<Path, Dependency> dependencyCommand() {
82+
return Optional
83+
.ofNullable(this.addDependencyCommand)
84+
.orElse(AddDependency::execute);
85+
}
86+
87+
private PomChange change(Path pomFile) {
8688
return PomChange
8789
.builder()
8890
.withLogger(getLog()::info)
@@ -96,9 +98,9 @@ private Dependency buildDependency() throws MojoExecutionException {
9698
try {
9799
return Dependency
98100
.ofGav(gav)
99-
.setType(type)
100-
.setClassifier(classifier)
101-
.setScope(scope)
101+
.withType(type)
102+
.withClassifier(classifier)
103+
.withScope(scope)
102104
.build();
103105
} catch (RuntimeException ex) {
104106
throw new MojoExecutionException(ex.getMessage(), ex);

0 commit comments

Comments
 (0)