Polish "Add support for Maven defaultGoal"

See gh-1134
This commit is contained in:
Stephane Nicoll
2020-10-09 09:25:08 +02:00
parent db0b33df42
commit c29df3e83c
4 changed files with 40 additions and 47 deletions

View File

@@ -47,14 +47,14 @@ public class MavenBuildSettings extends BuildSettings {
private final MavenScm scm;
private final String defaultGoal;
private final String finalName;
private final String sourceDirectory;
private final String testSourceDirectory;
private final String defaultGoal;
protected MavenBuildSettings(Builder builder) {
super(builder);
this.parent = builder.parent;
@@ -64,10 +64,10 @@ public class MavenBuildSettings extends BuildSettings {
this.licenses = Collections.unmodifiableList(new ArrayList<>(builder.licenses));
this.developers = Collections.unmodifiableList(new ArrayList<>(builder.developers));
this.scm = builder.scm.build();
this.defaultGoal = builder.defaultGoal;
this.finalName = builder.finalName;
this.sourceDirectory = builder.sourceDirectory;
this.testSourceDirectory = builder.testSourceDirectory;
this.defaultGoal = builder.defaultGoal;
}
/**
@@ -128,6 +128,14 @@ public class MavenBuildSettings extends BuildSettings {
return this.scm;
}
/**
* Return the default goal or phase to execute if none is given.
* @return the default goal or {@code null} to use the default
*/
public String getDefaultGoal() {
return this.defaultGoal;
}
/**
* Return the final name of the artifact.
* @return the final name or {@code null} to use the default
@@ -154,14 +162,6 @@ public class MavenBuildSettings extends BuildSettings {
return this.testSourceDirectory;
}
/**
* Return the default goal to execute when none is specified for the project.
* @return the default goal or {@code null} to use the default
*/
public String getDefaultGoal() {
return this.defaultGoal;
}
/**
* Builder for a Maven dependency.
*
@@ -183,14 +183,14 @@ public class MavenBuildSettings extends BuildSettings {
private final MavenScm.Builder scm = new MavenScm.Builder();
private String defaultGoal;
private String finalName;
private String sourceDirectory;
private String testSourceDirectory;
private String defaultGoal;
public Builder() {
}
@@ -287,6 +287,16 @@ public class MavenBuildSettings extends BuildSettings {
return self();
}
/**
* Set the default goal or phase to execute if none is given.
* @param defaultGoal the default goal or {@code null} to use the default
* @return this for method chaining
*/
public Builder defaultGoal(String defaultGoal) {
this.defaultGoal = defaultGoal;
return self();
}
/**
* Set the the location of main source code. Can use Maven properties such as
* {@code ${basedir}}.
@@ -311,16 +321,6 @@ public class MavenBuildSettings extends BuildSettings {
return self();
}
/**
* Set the default goal to execute when none is specified for the project.
* @param defaultGoal the default goal or {@code null} to use the default
* @return this for method chaining
*/
public Builder defaultGoal(String defaultGoal) {
this.defaultGoal = defaultGoal;
return self();
}
@Override
public MavenBuildSettings build() {
return new MavenBuildSettings(this);

View File

@@ -304,8 +304,8 @@ public class MavenBuildWriter {
private void writeBuild(IndentingWriter writer, MavenBuild build) {
MavenBuildSettings settings = build.getSettings();
if (settings.getFinalName() == null && settings.getSourceDirectory() == null
&& settings.getTestSourceDirectory() == null && settings.getDefaultGoal() == null
if (settings.getDefaultGoal() == null && settings.getFinalName() == null
&& settings.getSourceDirectory() == null && settings.getTestSourceDirectory() == null
&& build.resources().isEmpty() && build.testResources().isEmpty() && build.plugins().isEmpty()) {
return;
}

View File

@@ -139,11 +139,4 @@ class MavenBuildTests {
.hasOnlyOneElementSatisfying((testPlugin) -> assertThat(testPlugin.isExtensions()).isTrue());
}
@Test
void mavenDefaultGoalCanBeCustomized() {
MavenBuild build = new MavenBuild();
build.settings().defaultGoal("clean package");
assertThat(build.getSettings().getDefaultGoal()).isEqualTo("clean package");
}
}

View File

@@ -672,6 +672,21 @@ class MavenBuildWriterTests {
});
}
@Test
void pomWithNoDefaultGoal() {
MavenBuild build = new MavenBuild();
build.settings().coordinates("com.example.demo", "demo").build();
generatePom(build, (pom) -> assertThat(pom.nodeAtPath("/project/build/defaultGoal")).isNull());
}
@Test
void pomWithDefaultGoal() {
MavenBuild build = new MavenBuild();
build.settings().coordinates("com.example.demo", "demo").defaultGoal("clean package");
generatePom(build,
(pom) -> assertThat(pom).textAtPath("/project/build/defaultGoal").isEqualTo("clean package"));
}
@Test
void pomWithNoFinalName() {
MavenBuild build = new MavenBuild();
@@ -802,21 +817,6 @@ class MavenBuildWriterTests {
});
}
@Test
void pomWithNoDefaultGoal() {
MavenBuild build = new MavenBuild();
build.settings().coordinates("com.example.demo", "demo").build();
generatePom(build, (pom) -> assertThat(pom.nodeAtPath("/project/build/defaultGoal")).isNull());
}
@Test
void pomWithDefaultGoal() {
MavenBuild build = new MavenBuild();
build.settings().coordinates("com.example.demo", "demo").defaultGoal("clean package");
generatePom(build,
(pom) -> assertThat(pom).textAtPath("/project/build/defaultGoal").isEqualTo("clean package"));
}
private void generatePom(MavenBuild mavenBuild, Consumer<NodeAssert> consumer) {
MavenBuildWriter writer = new MavenBuildWriter();
StringWriter out = new StringWriter();